minemaster01 commited on
Commit
76c5b42
·
verified ·
1 Parent(s): c4adefd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -100
app.py CHANGED
@@ -15,41 +15,38 @@ HF_TOKEN = os.environ.get("HF_TOKEN") # You'll need to set this as a secret in
15
  DATASET_NAME = "srishtiy/se-culture-dataset-results" # Change to your username/dataset-name
16
  DATASET_CREATED = False
17
 
18
- # Define states/provinces for each country
19
  states_by_country = {
20
  "India": [
21
- "Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa",
22
- "Gujarat", "Haryana", "Himachal Pradesh", "Jharkhand", "Karnataka", "Kerala",
23
- "Madhya Pradesh", "Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland",
24
- "Odisha", "Punjab", "Rajasthan", "Sikkim", "Tamil Nadu", "Telangana", "Tripura",
25
- "Uttar Pradesh", "Uttarakhand", "West Bengal", "Andaman and Nicobar Islands",
26
- "Chandigarh", "Dadra and Nagar Haveli and Daman and Diu", "Delhi", "Jammu and Kashmir",
27
- "Ladakh", "Lakshadweep", "Puducherry"
28
  ],
29
  "Pakistan": [
30
- "Balochistan", "Khyber Pakhtunkhwa", "Punjab", "Sindh", "Islamabad Capital Territory",
31
  "Azad Jammu and Kashmir", "Gilgit-Baltistan"
32
  ],
33
  "Bangladesh": [
34
  "Barisal", "Chittagong", "Dhaka", "Khulna", "Mymensingh", "Rajshahi", "Rangpur", "Sylhet"
35
  ],
36
  "Afghanistan": [
37
- "Badakhshan", "Badghis", "Baghlan", "Balkh", "Bamyan", "Daykundi", "Farah", "Faryab",
38
- "Ghazni", "Ghor", "Helmand", "Herat", "Jowzjan", "Kabul", "Kandahar", "Kapisa", "Khost",
39
- "Kunar", "Kunduz", "Laghman", "Logar", "Nangarhar", "Nimruz", "Nuristan", "Paktia",
40
- "Paktika", "Panjshir", "Parwan", "Samangan", "Sar-e Pol", "Takhar", "Uruzgan", "Wardak", "Zabul"
41
  ],
42
  "Bhutan": [
43
- "Bumthang", "Chukha", "Dagana", "Gasa", "Haa", "Lhuentse", "Mongar", "Paro", "Pemagatshel",
44
- "Punakha", "Samdrup Jongkhar", "Samtse", "Sarpang", "Thimphu", "Trashigang", "Trashiyangtse",
45
- "Trongsa", "Tsirang", "Wangdue Phodrang", "Zhemgang"
46
  ],
47
  "Nepal": [
48
  "Bagmati", "Gandaki", "Karnali", "Koshi", "Lumbini", "Madhesh", "Sudurpashchim"
49
  ],
50
  "Sri Lanka": [
51
- "Central", "Eastern", "North Central", "Northern", "North Western", "Sabaragamuwa",
52
- "Southern", "Uva", "Western"
53
  ]
54
  }
55
 
@@ -72,12 +69,11 @@ def setup_hf_dataset():
72
  elif not HF_TOKEN:
73
  print("Warning: HF_TOKEN not set. Data will be stored locally only.")
74
 
75
- def update_states(country):
76
- """Update the states dropdown based on the selected country"""
77
  if country in states_by_country:
78
- return gr.Dropdown.update(choices=states_by_country[country], visible=True)
79
- else:
80
- return gr.Dropdown.update(choices=[], value=None, visible=False)
81
 
82
  def process_submission(input_img, text_answer, country, state, city, se_asia_relevance, culture_knowledge, native_caption, english_caption):
83
  # Generate unique ID for this submission
@@ -104,7 +100,7 @@ def process_submission(input_img, text_answer, country, state, city, se_asia_rel
104
  "image_filename": os.path.basename(image_path) if image_path else None,
105
  "cultural_relevance": text_answer,
106
  "country": country,
107
- "state_province": state,
108
  "city": city,
109
  "se_asia_relevance": se_asia_relevance,
110
  "cultural_knowledge_source": culture_knowledge,
@@ -145,101 +141,118 @@ def process_submission(input_img, text_answer, country, state, city, se_asia_rel
145
 
146
  # Return values to display in the interface
147
  location_info = f"Location: {city}, {state}, {country}" if state else f"Location: {city}, {country}"
148
- return input_img, f"Your text response: {text_answer}", location_info, f"SE Asia relevance: {se_asia_relevance}", f"Cultural knowledge source: {culture_knowledge}", f"Native caption: {native_caption}", f"English caption: {english_caption}"
149
 
150
  # Initialize the dataset
151
  setup_hf_dataset()
152
 
153
- with gr.Blocks(title="South Asian Image Data Collection") as gradio_app:
154
  gr.Markdown("# South Asian Image Data Collection")
155
  gr.Markdown("Upload an image and answer questions about its cultural significance.")
156
 
157
  with gr.Row():
158
- with gr.Column(scale=1):
159
- input_img = gr.Image(label="Upload an image", sources=['upload', 'webcam'], type="pil")
160
-
161
- with gr.Column(scale=1):
162
- text_answer = gr.Textbox(
163
- label="The image portrays culturally-relevant information in:",
164
- placeholder="What culture does this image represent?"
165
- )
166
-
167
- # Location information with dynamic dropdowns
168
- country_dropdown = gr.Dropdown(
169
- choices=list(states_by_country.keys()),
170
- label="Country where the image was taken:",
171
- interactive=True
172
- )
173
-
174
- state_dropdown = gr.Dropdown(
175
- label="State/Province/District:",
176
- interactive=True,
177
- visible=False # Will be made visible when country is selected
178
- )
179
-
180
- city_textbox = gr.Textbox(
181
- label="City (optional):",
182
- placeholder="Enter city name"
183
- )
184
-
185
- se_asia_relevance = gr.Radio(
186
- choices=[
187
- "Yes. Unique to South Asia",
188
- "Yes, people will likely think of South Asia when seeing the picture, but it may have low degree of similarity to other cultures.",
189
- "Maybe, this culture did not originate from South Asia, but it's quite dominant in South Asia",
190
- "Not really. It has some affiliation to South Asia, but actually does not represent South Asia or has stronger affiliation to cultures outside South Asia",
191
- "No. Totally unrelated to South Asia"
192
- ],
193
- label="Is the image culturally relevant in South Asia?"
194
- )
195
-
196
- culture_knowledge = gr.Radio(
197
- choices=[
198
- "I'm from this country/culture",
199
- "I checked online resources (e.g., Wikipedia, articles, blogs)"
200
- ],
201
- label="How do you know about this culture?",
202
- info="Please do not consult LLMs (e.g., GPT-4o, Claude, Command-R, etc.)"
203
- )
204
-
205
- native_caption = gr.Textbox(
206
- label="Caption in Native Language:",
207
- placeholder="Enter caption in the native language of the culture depicted"
208
- )
209
-
210
- english_caption = gr.Textbox(
211
- label="English Caption:",
212
- placeholder="Enter caption in English"
213
- )
 
 
 
 
 
214
 
215
- # Connect the country dropdown to update the state dropdown
 
 
 
 
 
 
 
 
 
 
 
 
216
  country_dropdown.change(
217
- fn=update_states,
218
  inputs=country_dropdown,
219
  outputs=state_dropdown
220
  )
221
 
222
- submit_btn = gr.Button("Submit")
223
-
224
- with gr.Row():
225
- with gr.Column():
226
- output_img = gr.Image(label="Submitted Image")
227
- text_response = gr.Text(label="Text Response")
228
- location_info = gr.Text(label="Location Information")
229
- relevance_output = gr.Text(label="South Asia Cultural Relevance")
230
- knowledge_source = gr.Text(label="Cultural Knowledge Source")
231
- native_caption_output = gr.Text(label="Native Language Caption")
232
- english_caption_output = gr.Text(label="English Caption")
233
-
234
  submit_btn.click(
235
  fn=process_submission,
236
  inputs=[
237
- input_img, text_answer, country_dropdown, state_dropdown, city_textbox,
238
- se_asia_relevance, culture_knowledge, native_caption, english_caption
 
 
 
 
 
 
 
239
  ],
240
  outputs=[
241
- output_img, text_response, location_info, relevance_output,
242
- knowledge_source, native_caption_output, english_caption_output
 
 
 
 
 
243
  ]
244
  )
245
 
 
15
  DATASET_NAME = "srishtiy/se-culture-dataset-results" # Change to your username/dataset-name
16
  DATASET_CREATED = False
17
 
18
+ # States by country dictionary
19
  states_by_country = {
20
  "India": [
21
+ "Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa", "Gujarat", "Haryana",
22
+ "Himachal Pradesh", "Jharkhand", "Karnataka", "Kerala", "Madhya Pradesh", "Maharashtra", "Manipur",
23
+ "Meghalaya", "Mizoram", "Nagaland", "Odisha", "Punjab", "Rajasthan", "Sikkim", "Tamil Nadu", "Telangana",
24
+ "Tripura", "Uttar Pradesh", "Uttarakhand", "West Bengal", "Andaman and Nicobar Islands", "Chandigarh",
25
+ "Dadra and Nagar Haveli and Daman and Diu", "Delhi", "Jammu and Kashmir", "Ladakh", "Lakshadweep", "Puducherry"
 
 
26
  ],
27
  "Pakistan": [
28
+ "Balochistan", "Khyber Pakhtunkhwa", "Punjab", "Sindh", "Islamabad Capital Territory",
29
  "Azad Jammu and Kashmir", "Gilgit-Baltistan"
30
  ],
31
  "Bangladesh": [
32
  "Barisal", "Chittagong", "Dhaka", "Khulna", "Mymensingh", "Rajshahi", "Rangpur", "Sylhet"
33
  ],
34
  "Afghanistan": [
35
+ "Badakhshan", "Badghis", "Baghlan", "Balkh", "Bamyan", "Daykundi", "Farah", "Faryab", "Ghazni", "Ghor",
36
+ "Helmand", "Herat", "Jowzjan", "Kabul", "Kandahar", "Kapisa", "Khost", "Kunar", "Kunduz", "Laghman",
37
+ "Logar", "Nangarhar", "Nimruz", "Nuristan", "Paktia", "Paktika", "Panjshir", "Parwan", "Samangan",
38
+ "Sar-e Pol", "Takhar", "Uruzgan", "Wardak", "Zabul"
39
  ],
40
  "Bhutan": [
41
+ "Bumthang", "Chukha", "Dagana", "Gasa", "Haa", "Lhuentse", "Mongar", "Paro", "Pemagatshel", "Punakha",
42
+ "Samdrup Jongkhar", "Samtse", "Sarpang", "Thimphu", "Trashigang", "Trashiyangtse", "Trongsa", "Tsirang",
43
+ "Wangdue Phodrang", "Zhemgang"
44
  ],
45
  "Nepal": [
46
  "Bagmati", "Gandaki", "Karnali", "Koshi", "Lumbini", "Madhesh", "Sudurpashchim"
47
  ],
48
  "Sri Lanka": [
49
+ "Central", "Eastern", "North Central", "Northern", "North Western", "Sabaragamuwa", "Southern", "Uva", "Western"
 
50
  ]
51
  }
52
 
 
69
  elif not HF_TOKEN:
70
  print("Warning: HF_TOKEN not set. Data will be stored locally only.")
71
 
72
+ def update_state_dropdown(country):
73
+ """Update state dropdown based on selected country"""
74
  if country in states_by_country:
75
+ return gr.Dropdown(choices=states_by_country[country], label=f"State/Province in {country}:", interactive=True)
76
+ return gr.Dropdown(choices=[], label="State/Province:", interactive=True)
 
77
 
78
  def process_submission(input_img, text_answer, country, state, city, se_asia_relevance, culture_knowledge, native_caption, english_caption):
79
  # Generate unique ID for this submission
 
100
  "image_filename": os.path.basename(image_path) if image_path else None,
101
  "cultural_relevance": text_answer,
102
  "country": country,
103
+ "state": state,
104
  "city": city,
105
  "se_asia_relevance": se_asia_relevance,
106
  "cultural_knowledge_source": culture_knowledge,
 
141
 
142
  # Return values to display in the interface
143
  location_info = f"Location: {city}, {state}, {country}" if state else f"Location: {city}, {country}"
144
+ return input_img, f"Your text response: {text_answer}", f"Selected location: {location_info}", f"SE Asia relevance: {se_asia_relevance}", f"Cultural knowledge source: {culture_knowledge}", f"Native caption: {native_caption}", f"English caption: {english_caption}"
145
 
146
  # Initialize the dataset
147
  setup_hf_dataset()
148
 
149
+ with gr.Blocks() as gradio_app:
150
  gr.Markdown("# South Asian Image Data Collection")
151
  gr.Markdown("Upload an image and answer questions about its cultural significance.")
152
 
153
  with gr.Row():
154
+ input_img = gr.Image(label="Upload an image", sources=['upload', 'webcam'], type="pil")
155
+
156
+ with gr.Row():
157
+ text_answer = gr.Textbox(label="The image portrays culturally-relevant information in:", placeholder="what culture does this image represent?")
158
+
159
+ with gr.Row():
160
+ country_dropdown = gr.Dropdown(
161
+ choices=["India", "Pakistan", "Bangladesh", "Afghanistan", "Bhutan", "Nepal", "Sri Lanka"],
162
+ label="Country where the image was taken:",
163
+ interactive=True
164
+ )
165
+
166
+ with gr.Row():
167
+ state_dropdown = gr.Dropdown(
168
+ choices=[],
169
+ label="State/Province:",
170
+ interactive=True
171
+ )
172
+
173
+ with gr.Row():
174
+ city_textbox = gr.Textbox(label="City where the image was taken:", placeholder="Enter city name")
175
+
176
+ with gr.Row():
177
+ se_asia_relevance = gr.Radio(
178
+ choices=[
179
+ "Yes. Unique to South Asia",
180
+ "Yes, people will likely think of South Asia when seeing the picture, but it may have low degree of similarity to other cultures.",
181
+ "Maybe, this culture did not originate from South Asia, but it's quite dominant in South Asia",
182
+ "Not really. It has some affiliation to South Asia, but actually does not represent South Asia or has stronger affiliation to cultures outside South Asia",
183
+ "No. Totally unrelated to South Asia"
184
+ ],
185
+ label="Is the image culturally relevant in South Asia?"
186
+ )
187
+
188
+ with gr.Row():
189
+ culture_knowledge = gr.Radio(
190
+ choices=[
191
+ "I'm from this country/culture",
192
+ "I checked online resources (e.g., Wikipedia, articles, blogs)"
193
+ ],
194
+ label="How do you know about this culture?",
195
+ info="Please do not consult LLMs (e.g., GPT-4o, Claude, Command-R, etc.)"
196
+ )
197
+
198
+ with gr.Row():
199
+ native_caption = gr.Textbox(label="Caption in Native Language:", placeholder="Enter caption in the native language of the culture depicted")
200
+
201
+ with gr.Row():
202
+ english_caption = gr.Textbox(label="English Caption:", placeholder="Enter caption in English")
203
+
204
+ with gr.Row():
205
+ submit_btn = gr.Button("Submit")
206
+
207
+ with gr.Row():
208
+ output_img = gr.Image(label="Submitted Image")
209
+
210
+ with gr.Row():
211
+ output_text = gr.Text(label="Text Response")
212
+
213
+ with gr.Row():
214
+ output_location = gr.Text(label="Location Information")
215
 
216
+ with gr.Row():
217
+ output_relevance = gr.Text(label="South Asia Cultural Relevance")
218
+
219
+ with gr.Row():
220
+ output_knowledge = gr.Text(label="Cultural Knowledge Source")
221
+
222
+ with gr.Row():
223
+ output_native = gr.Text(label="Native Language Caption")
224
+
225
+ with gr.Row():
226
+ output_english = gr.Text(label="English Caption")
227
+
228
+ # Set up event handlers
229
  country_dropdown.change(
230
+ fn=update_state_dropdown,
231
  inputs=country_dropdown,
232
  outputs=state_dropdown
233
  )
234
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  submit_btn.click(
236
  fn=process_submission,
237
  inputs=[
238
+ input_img,
239
+ text_answer,
240
+ country_dropdown,
241
+ state_dropdown,
242
+ city_textbox,
243
+ se_asia_relevance,
244
+ culture_knowledge,
245
+ native_caption,
246
+ english_caption
247
  ],
248
  outputs=[
249
+ output_img,
250
+ output_text,
251
+ output_location,
252
+ output_relevance,
253
+ output_knowledge,
254
+ output_native,
255
+ output_english
256
  ]
257
  )
258