traopia commited on
Commit
817809c
·
1 Parent(s): 8df006a

Add application file

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ from src.generate_queries_alternative import main_generate_queries
5
+ import time
6
+ import pandas as pd
7
+
8
+ from src.visual_qa import main_text_retrieve_images
9
+
10
+ def handle_structured_query(question, sort_by=""):
11
+ if not question:
12
+ return "Please ask something 🙂", pd.DataFrame(), []
13
+
14
+ try:
15
+ start = time.time()
16
+ result_query, sparql_query = main_generate_queries(question)
17
+ elapsed = round(time.time() - start, 2)
18
+ except Exception as e:
19
+ return f"⚠️ Query failed: {e}", pd.DataFrame(), []
20
+
21
+ if isinstance(result_query, str):
22
+ return result_query, pd.DataFrame(), []
23
+
24
+ if not result_query:
25
+ return f"No results for '{question}'. Try rephrasing. (⏱ {elapsed}s)", pd.DataFrame(), []
26
+
27
+ df = pd.DataFrame(result_query)
28
+ if sort_by and sort_by in df.columns:
29
+ df = df.sort_values(by=sort_by)
30
+
31
+
32
+ if "image_url" in df.columns:
33
+ columns_of_interest = ["image_url", "year","fashion_collectionLabel", "reference_URL"]
34
+ df = df[columns_of_interest]
35
+ # Create a gallery: each item is (image_url, metadata string)
36
+ gallery_items = []
37
+ for _, row in df.iterrows():
38
+ image_url = row.get("image_url")
39
+ if not image_url:
40
+ continue
41
+ # Caption from other fields
42
+ caption = " | ".join(f"{k}: {v}" for k, v in row.items() if k != "image_url" and pd.notnull(v))
43
+ gallery_items.append((image_url, caption))
44
+ return f"Query returned {len(gallery_items)} image(s) in {elapsed} seconds.", pd.DataFrame(), gallery_items
45
+
46
+ return f"Query returned a table with {len(df)} row(s) in {elapsed} seconds.", df, []
47
+
48
+
49
+
50
+
51
+ def handle_image_query(text):
52
+ if not text:
53
+ return []
54
+
55
+ try:
56
+ records = main_text_retrieve_images(text)
57
+ except Exception as e:
58
+ return [("https://via.placeholder.com/300x200?text=Error", f"Error: {e}")]
59
+
60
+ gallery_items = []
61
+ for item in records:
62
+ image_url = item.get("image_url")
63
+ if not image_url:
64
+ continue
65
+ # Build a simple caption from the remaining fields
66
+ caption = " | ".join(f"{k}: {v}" for k, v in item.items() if k != "image_url")
67
+ gallery_items.append((image_url, caption))
68
+
69
+ return gallery_items
70
+
71
+ # --- UI --- #
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("# 🧵 FashionDB Interface")
74
+
75
+
76
+ with gr.Tab("Structured Query"):
77
+ gr.Markdown("Ask FashionDB anything and view results with images + metadata.")
78
+
79
+ with gr.Row():
80
+ query_input = gr.Textbox(label="Your question")
81
+ sort_input = gr.Textbox(label="Sort by (optional column name)", placeholder="e.g. start_year")
82
+
83
+ query_submit = gr.Button("Submit")
84
+
85
+ query_text_output = gr.Textbox(label="Message", interactive=False)
86
+ query_table_output = gr.Dataframe(label="Tabular Result", interactive=False)
87
+ query_gallery_output = gr.Gallery(label="Image Gallery")
88
+ query_submit.click(
89
+ fn=handle_structured_query,
90
+ inputs=[query_input, sort_input],
91
+ outputs=[
92
+ query_text_output,
93
+ query_table_output,
94
+ query_gallery_output
95
+ ]
96
+ )
97
+
98
+ with gr.Tab("Image Retrieval"):
99
+ gr.Markdown("Search for similar fashion show images based on a text description.")
100
+ image_text = gr.Textbox(label="Describe the kind of images you're looking for")
101
+ image_submit = gr.Button("Find Images")
102
+ image_gallery = gr.Gallery(label="Retrieved Images")
103
+
104
+ image_submit.click(handle_image_query, inputs=image_text, outputs=image_gallery)
105
+
106
+ demo.launch()