Deva8 commited on
Commit
fc3e6f2
Β·
verified Β·
1 Parent(s): 9370fa0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +284 -0
app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from huggingface_hub import hf_hub_download
4
+ from PIL import Image
5
+ import zipfile
6
+ import os
7
+ import random
8
+
9
+ # Global variables
10
+ df = None
11
+ images_dir = None
12
+
13
+ def setup_dataset():
14
+ """Download and setup the dataset (called once on startup)"""
15
+ global df, images_dir
16
+
17
+ print("Loading metadata...")
18
+ # Load metadata
19
+ csv_path = hf_hub_download(
20
+ repo_id="Deva8/Generative-VQA-V2-Curated",
21
+ filename="main_metadata.csv",
22
+ repo_type="dataset"
23
+ )
24
+ df = pd.read_csv(csv_path)
25
+
26
+ print("Downloading images zip (this may take a few minutes)...")
27
+ # Download zip file
28
+ zip_path = hf_hub_download(
29
+ repo_id="Deva8/Generative-VQA-V2-Curated",
30
+ filename="gen_vqa_v2-images.zip",
31
+ repo_type="dataset"
32
+ )
33
+
34
+ # Extract images
35
+ images_dir = "./extracted_images"
36
+ if not os.path.exists(images_dir):
37
+ print("Extracting images...")
38
+ os.makedirs(images_dir, exist_ok=True)
39
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
40
+ zip_ref.extractall(images_dir)
41
+
42
+ print(f"βœ“ Dataset ready! {len(df)} examples loaded.")
43
+ return f"Dataset loaded successfully! {len(df):,} examples available."
44
+
45
+ def get_random_sample():
46
+ """Get a random sample from the dataset"""
47
+ if df is None:
48
+ return None, "Please wait, dataset is loading...", "", ""
49
+
50
+ # Get random row
51
+ sample = df.sample(1).iloc[0]
52
+
53
+ # Load image
54
+ img_path = os.path.join(images_dir, sample['file_name'])
55
+ img = Image.open(img_path)
56
+
57
+ question = sample['question']
58
+ answer = sample['answer']
59
+ metadata = f"Image ID: {sample['image_id']} | Question ID: {sample['question_id']}"
60
+
61
+ return img, question, answer, metadata
62
+
63
+ def search_by_question(query):
64
+ """Search for questions containing the query"""
65
+ if df is None:
66
+ return None, "Dataset not loaded yet", "", ""
67
+
68
+ if not query or len(query.strip()) < 3:
69
+ return None, "Please enter at least 3 characters to search", "", ""
70
+
71
+ # Search for matching questions
72
+ matches = df[df['question'].str.contains(query, case=False, na=False)]
73
+
74
+ if len(matches) == 0:
75
+ return None, f"No questions found containing '{query}'", "", ""
76
+
77
+ # Get random match
78
+ sample = matches.sample(1).iloc[0]
79
+
80
+ # Load image
81
+ img_path = os.path.join(images_dir, sample['file_name'])
82
+ img = Image.open(img_path)
83
+
84
+ question = sample['question']
85
+ answer = sample['answer']
86
+ metadata = f"Image ID: {sample['image_id']} | Question ID: {sample['question_id']} | Found {len(matches)} matches"
87
+
88
+ return img, question, answer, metadata
89
+
90
+ def search_by_answer(query):
91
+ """Search for specific answers"""
92
+ if df is None:
93
+ return None, "Dataset not loaded yet", "", ""
94
+
95
+ if not query or len(query.strip()) < 1:
96
+ return None, "Please enter an answer to search", "", ""
97
+
98
+ # Search for matching answers
99
+ matches = df[df['answer'].str.lower() == query.lower().strip()]
100
+
101
+ if len(matches) == 0:
102
+ return None, f"No examples found with answer '{query}'", "", ""
103
+
104
+ # Get random match
105
+ sample = matches.sample(1).iloc[0]
106
+
107
+ # Load image
108
+ img_path = os.path.join(images_dir, sample['file_name'])
109
+ img = Image.open(img_path)
110
+
111
+ question = sample['question']
112
+ answer = sample['answer']
113
+ metadata = f"Image ID: {sample['image_id']} | Question ID: {sample['question_id']} | Found {len(matches)} examples with this answer"
114
+
115
+ return img, question, answer, metadata
116
+
117
+ def get_statistics():
118
+ """Get dataset statistics"""
119
+ if df is None:
120
+ return "Dataset not loaded yet"
121
+
122
+ stats = f"""
123
+ # πŸ“Š Dataset Statistics
124
+
125
+ - **Total Examples**: {len(df):,}
126
+ - **Unique Images**: {df['image_id'].nunique():,}
127
+ - **Unique Answers**: {df['answer'].nunique():,}
128
+
129
+ ## Top 10 Most Common Answers:
130
+
131
+ """
132
+
133
+ top_answers = df['answer'].value_counts().head(10)
134
+ for i, (answer, count) in enumerate(top_answers.items(), 1):
135
+ stats += f"{i}. **{answer}** - {count} examples\n"
136
+
137
+ stats += f"\n## Question Length Distribution:\n"
138
+ stats += f"- Average: {df['question'].str.split().str.len().mean():.1f} words\n"
139
+ stats += f"- Min: {df['question'].str.split().str.len().min()} words\n"
140
+ stats += f"- Max: {df['question'].str.split().str.len().max()} words\n"
141
+
142
+ stats += f"\n## Answer Length Distribution:\n"
143
+ stats += f"- Average: {df['answer'].str.split().str.len().mean():.2f} words\n"
144
+ stats += f"- Single word answers: {(df['answer'].str.split().str.len() == 1).sum():,} ({(df['answer'].str.split().str.len() == 1).sum() / len(df) * 100:.1f}%)\n"
145
+
146
+ return stats
147
+
148
+ # Initialize dataset on startup
149
+ print("Starting dataset setup...")
150
+ setup_status = setup_dataset()
151
+ print(setup_status)
152
+
153
+ # Create Gradio interface
154
+ with gr.Blocks(title="Generative VQA v2 Dataset Explorer", theme=gr.themes.Soft()) as demo:
155
+ gr.Markdown("""
156
+ # 🎯 Generative VQA-V2-Curated Dataset Explorer
157
+
158
+ Explore the **Generative VQA v2 Curated** dataset - a balanced, cleaned version of VQA v2
159
+ optimized for generative visual question answering.
160
+
161
+ **Dataset**: [Deva8/Generative-VQA-V2-Curated](https://huggingface.co/datasets/Deva8/Generative-VQA-V2-Curated)
162
+
163
+ ---
164
+ """)
165
+
166
+ with gr.Tabs():
167
+ # Tab 1: Random Samples
168
+ with gr.Tab("🎲 Random Samples"):
169
+ gr.Markdown("### Click the button to see random examples from the dataset")
170
+
171
+ with gr.Row():
172
+ random_btn = gr.Button("πŸ”„ Get Random Sample", variant="primary", size="lg")
173
+
174
+ with gr.Row():
175
+ with gr.Column(scale=1):
176
+ random_image = gr.Image(label="Image", type="pil")
177
+
178
+ with gr.Column(scale=1):
179
+ random_question = gr.Textbox(label="❓ Question", lines=2)
180
+ random_answer = gr.Textbox(label="βœ… Answer", lines=1)
181
+ random_metadata = gr.Textbox(label="ℹ️ Metadata", lines=1)
182
+
183
+ random_btn.click(
184
+ fn=get_random_sample,
185
+ outputs=[random_image, random_question, random_answer, random_metadata]
186
+ )
187
+
188
+ # Tab 2: Search by Question
189
+ with gr.Tab("πŸ” Search Questions"):
190
+ gr.Markdown("### Search for questions containing specific keywords")
191
+
192
+ with gr.Row():
193
+ question_query = gr.Textbox(
194
+ label="Search Query",
195
+ placeholder="e.g., 'color', 'many', 'wearing', 'holding'",
196
+ lines=1
197
+ )
198
+ question_search_btn = gr.Button("πŸ”Ž Search", variant="primary")
199
+
200
+ with gr.Row():
201
+ with gr.Column(scale=1):
202
+ question_image = gr.Image(label="Image", type="pil")
203
+
204
+ with gr.Column(scale=1):
205
+ question_text = gr.Textbox(label="❓ Question", lines=2)
206
+ question_answer = gr.Textbox(label="βœ… Answer", lines=1)
207
+ question_metadata = gr.Textbox(label="ℹ️ Metadata", lines=1)
208
+
209
+ question_search_btn.click(
210
+ fn=search_by_question,
211
+ inputs=[question_query],
212
+ outputs=[question_image, question_text, question_answer, question_metadata]
213
+ )
214
+
215
+ # Tab 3: Search by Answer
216
+ with gr.Tab("🎯 Search Answers"):
217
+ gr.Markdown("### Find examples with specific answers")
218
+
219
+ with gr.Row():
220
+ answer_query = gr.Textbox(
221
+ label="Answer to Search",
222
+ placeholder="e.g., 'red', 'cat', '2', 'eating'",
223
+ lines=1
224
+ )
225
+ answer_search_btn = gr.Button("πŸ”Ž Search", variant="primary")
226
+
227
+ gr.Markdown("**Popular answers**: white, black, blue, red, 2, 3, brown, green, pizza, dog")
228
+
229
+ with gr.Row():
230
+ with gr.Column(scale=1):
231
+ answer_image = gr.Image(label="Image", type="pil")
232
+
233
+ with gr.Column(scale=1):
234
+ answer_question = gr.Textbox(label="❓ Question", lines=2)
235
+ answer_text = gr.Textbox(label="βœ… Answer", lines=1)
236
+ answer_metadata = gr.Textbox(label="ℹ️ Metadata", lines=1)
237
+
238
+ answer_search_btn.click(
239
+ fn=search_by_answer,
240
+ inputs=[answer_query],
241
+ outputs=[answer_image, answer_question, answer_text, answer_metadata]
242
+ )
243
+
244
+ # Tab 4: Statistics
245
+ with gr.Tab("πŸ“Š Statistics"):
246
+ gr.Markdown("### Dataset Statistics and Analysis")
247
+
248
+ stats_btn = gr.Button("πŸ“ˆ Show Statistics", variant="primary")
249
+ stats_output = gr.Markdown()
250
+
251
+ stats_btn.click(
252
+ fn=get_statistics,
253
+ outputs=[stats_output]
254
+ )
255
+
256
+ gr.Markdown("""
257
+ ---
258
+
259
+ ## About This Dataset
260
+
261
+ **Generative VQA-V2-Curated** is a cleaned and balanced version of VQA v2:
262
+
263
+ - βœ… Removed yes/no questions
264
+ - βœ… Balanced answer distribution (max 600 per answer)
265
+ - βœ… Filtered ambiguous questions
266
+ - βœ… 135,268 high-quality QA pairs
267
+ - βœ… 1,251 unique answer classes
268
+
269
+ **License**: CC BY 4.0 (COCO + VQA v2)
270
+
271
+ **Citation**:
272
+ ```bibtex
273
+ @misc{devarajan_genvqa_2026,
274
+ author = {Devarajan},
275
+ title = {Generative-VQA-V2-Curated},
276
+ year = {2026},
277
+ publisher = {Hugging Face},
278
+ }
279
+ ```
280
+ """)
281
+
282
+ # Launch the app
283
+ if __name__ == "__main__":
284
+ demo.launch()