File size: 3,902 Bytes
817809c
 
 
 
 
 
 
d6929a0
034236c
 
 
 
 
 
 
 
d6929a0
817809c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import gradio as gr


from src.generate_queries_alternative import main_generate_queries
import time
import pandas as pd

import spacy

# Try to load the model, and download it if missing
try:
    nlp = spacy.load("en_core_web_sm")
except OSError:
    from spacy.cli import download
    download("en_core_web_sm")
    nlp = spacy.load("en_core_web_sm")

from src.visual_qa import main_text_retrieve_images

def handle_structured_query(question, sort_by=""):
    if not question:
        return "Please ask something 🙂", pd.DataFrame(), []

    try:
        start = time.time()
        result_query, sparql_query = main_generate_queries(question)
        elapsed = round(time.time() - start, 2)
    except Exception as e:
        return f"⚠️ Query failed: {e}", pd.DataFrame(), []

    if isinstance(result_query, str):
        return result_query, pd.DataFrame(), []

    if not result_query:
        return f"No results for '{question}'. Try rephrasing. (⏱ {elapsed}s)", pd.DataFrame(), []

    df = pd.DataFrame(result_query)
    if sort_by and sort_by in df.columns:
        df = df.sort_values(by=sort_by)


    if "image_url" in df.columns:
        columns_of_interest = ["image_url", "year","fashion_collectionLabel", "reference_URL"]
        df = df[columns_of_interest]
        # Create a gallery: each item is (image_url, metadata string)
        gallery_items = []
        for _, row in df.iterrows():
            image_url = row.get("image_url")
            if not image_url:
                continue
            # Caption from other fields
            caption = " | ".join(f"{k}: {v}" for k, v in row.items() if k != "image_url" and pd.notnull(v))
            gallery_items.append((image_url, caption))
        return f"Query returned {len(gallery_items)} image(s) in {elapsed} seconds.", pd.DataFrame(), gallery_items

    return f"Query returned a table with {len(df)} row(s) in {elapsed} seconds.", df, []




def handle_image_query(text):
    if not text:
        return []

    try:
        records = main_text_retrieve_images(text)
    except Exception as e:
        return [("https://via.placeholder.com/300x200?text=Error", f"Error: {e}")]

    gallery_items = []
    for item in records:
        image_url = item.get("image_url")
        if not image_url:
            continue
        # Build a simple caption from the remaining fields
        caption = " | ".join(f"{k}: {v}" for k, v in item.items() if k != "image_url")
        gallery_items.append((image_url, caption))

    return gallery_items

# --- UI --- #
with gr.Blocks() as demo:
    gr.Markdown("# 🧵 FashionDB Interface")


    with gr.Tab("Structured Query"):
        gr.Markdown("Ask FashionDB anything and view results with images + metadata.")

        with gr.Row():
            query_input = gr.Textbox(label="Your question")
            sort_input = gr.Textbox(label="Sort by (optional column name)", placeholder="e.g. start_year")

        query_submit = gr.Button("Submit")

        query_text_output = gr.Textbox(label="Message", interactive=False)
        query_table_output = gr.Dataframe(label="Tabular Result", interactive=False)
        query_gallery_output = gr.Gallery(label="Image Gallery")
        query_submit.click(
            fn=handle_structured_query,
            inputs=[query_input, sort_input],
            outputs=[
                query_text_output,
                query_table_output,
                query_gallery_output
            ]
        )

    with gr.Tab("Image Retrieval"):
        gr.Markdown("Search for similar fashion show images based on a text description.")
        image_text = gr.Textbox(label="Describe the kind of images you're looking for")
        image_submit = gr.Button("Find Images")
        image_gallery = gr.Gallery(label="Retrieved Images")

        image_submit.click(handle_image_query, inputs=image_text, outputs=image_gallery)

demo.launch()