copperwiring commited on
Commit
96e76ac
·
1 Parent(s): 37674bb

Add data persistence for uploaded images and form responses

Browse files
Files changed (1) hide show
  1. app.py +50 -4
app.py CHANGED
@@ -1,8 +1,54 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
2
 
3
  def process_submission(input_img, text_answer, multiple_choice, city, country, se_asia_relevance, culture_knowledge, native_caption, english_caption):
4
- # Just return the inputs to demonstrate they were received
5
- # No ML pipeline processing here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  return input_img, f"Your text response: {text_answer}", f"Your selected option: {multiple_choice}", f"Location: {city}, {country}", f"SE Asia relevance: {se_asia_relevance}", f"Cultural knowledge source: {culture_knowledge}", f"Native caption: {native_caption}", f"English caption: {english_caption}"
7
 
8
  gradio_app = gr.Interface(
@@ -10,8 +56,8 @@ gradio_app = gr.Interface(
10
  inputs=[
11
  gr.Image(label="Upload an image", sources=['upload', 'webcam'], type="pil"),
12
  gr.Textbox(label="The image portrays culturally-relevant information in:", placeholder="what culture does this image represent?"),
13
- # gr.Radio(choices=["North America", "South America", "Europe", "Africa", "Asia", "Australia"],
14
- # label="Which continent is most represented in this image?"),
15
  gr.Textbox(label="City where the image was taken:", placeholder="Enter city name"),
16
  gr.Textbox(label="Country where the image was taken:", placeholder="Enter country name"),
17
  gr.Radio(
 
1
  import gradio as gr
2
+ import os
3
+ import json
4
+ import uuid
5
+ from datetime import datetime
6
+ import shutil
7
+
8
+ # Create directories for data storage
9
+ os.makedirs("uploaded_images", exist_ok=True)
10
+ os.makedirs("submissions", exist_ok=True)
11
 
12
  def process_submission(input_img, text_answer, multiple_choice, city, country, se_asia_relevance, culture_knowledge, native_caption, english_caption):
13
+ # Generate unique ID for this submission
14
+ submission_id = str(uuid.uuid4())
15
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
16
+
17
+ # Save the image if provided
18
+ image_path = None
19
+ if input_img is not None:
20
+ # Create filename with submission ID
21
+ image_filename = f"{timestamp}_{submission_id}.jpg"
22
+ image_path = os.path.join("uploaded_images", image_filename)
23
+
24
+ # Save the image
25
+ if isinstance(input_img, str): # If it's a file path
26
+ shutil.copy(input_img, image_path)
27
+ else: # If it's a PIL Image
28
+ input_img.save(image_path)
29
+
30
+ # Create a data structure for the submission
31
+ submission_data = {
32
+ "id": submission_id,
33
+ "timestamp": timestamp,
34
+ "image_filename": image_path,
35
+ "cultural_relevance": text_answer,
36
+ "continent": multiple_choice,
37
+ "city": city,
38
+ "country": country,
39
+ "se_asia_relevance": se_asia_relevance,
40
+ "cultural_knowledge_source": culture_knowledge,
41
+ "native_caption": native_caption,
42
+ "english_caption": english_caption
43
+ }
44
+
45
+ # Save the data as JSON
46
+ json_filename = f"{timestamp}_{submission_id}.json"
47
+ json_path = os.path.join("submissions", json_filename)
48
+ with open(json_path, "w") as f:
49
+ json.dump(submission_data, f, indent=2)
50
+
51
+ # Return values to display in the interface
52
  return input_img, f"Your text response: {text_answer}", f"Your selected option: {multiple_choice}", f"Location: {city}, {country}", f"SE Asia relevance: {se_asia_relevance}", f"Cultural knowledge source: {culture_knowledge}", f"Native caption: {native_caption}", f"English caption: {english_caption}"
53
 
54
  gradio_app = gr.Interface(
 
56
  inputs=[
57
  gr.Image(label="Upload an image", sources=['upload', 'webcam'], type="pil"),
58
  gr.Textbox(label="The image portrays culturally-relevant information in:", placeholder="what culture does this image represent?"),
59
+ gr.Radio(choices=["North America", "South America", "Europe", "Africa", "Asia", "Australia"],
60
+ label="Which continent is most represented in this image?"),
61
  gr.Textbox(label="City where the image was taken:", placeholder="Enter city name"),
62
  gr.Textbox(label="Country where the image was taken:", placeholder="Enter country name"),
63
  gr.Radio(