traopia commited on
Commit
85e469d
·
1 Parent(s): 9632b77
Files changed (4) hide show
  1. app.py +109 -113
  2. app_old.py +118 -0
  3. gradio_app1.py +110 -0
  4. search.py +22 -0
app.py CHANGED
@@ -1,118 +1,114 @@
1
  import gradio as gr
2
- #example just for fun
3
- from src.visual_qa import main_text_retrieve_images
4
- from src.generate_queries_alternative import main_generate_queries
5
- import time
6
  import pandas as pd
7
-
8
- import spacy
9
-
10
- # Try to load the model, and download it if missing
11
- try:
12
- nlp = spacy.load("en_core_web_sm")
13
- except OSError:
14
- from spacy.cli import download
15
- download("en_core_web_sm")
16
- nlp = spacy.load("en_core_web_sm")
17
-
18
-
19
-
20
- def handle_structured_query(question, sort_by=""):
21
- if not question:
22
- return "Please ask something 🙂", pd.DataFrame(), []
23
-
24
- try:
25
- start = time.time()
26
- result_query, sparql_query = main_generate_queries(question)
27
- elapsed = round(time.time() - start, 2)
28
- except Exception as e:
29
- return f"⚠️ Query failed: {e}", pd.DataFrame(), []
30
-
31
- if isinstance(result_query, str):
32
- return result_query, pd.DataFrame(), []
33
-
34
- if not result_query:
35
- return f"No results for '{question}'. Try rephrasing. (⏱ {elapsed}s)", pd.DataFrame(), []
36
-
37
- df = pd.DataFrame(result_query)
38
- if sort_by and sort_by in df.columns:
39
- df = df.sort_values(by=sort_by)
40
-
41
-
42
- if "image_url" in df.columns:
43
- columns_of_interest = ["image_url", "year","fashion_collectionLabel", "reference_URL"]
44
- df = df[columns_of_interest]
45
- # Create a gallery: each item is (image_url, metadata string)
46
- gallery_items = []
47
- for _, row in df.iterrows():
48
- image_url = row.get("image_url")
49
- if not image_url:
50
- continue
51
- # Caption from other fields
52
- caption = " | ".join(f"{k}: {v}" for k, v in row.items() if k != "image_url" and pd.notnull(v))
53
- gallery_items.append((image_url, caption))
54
- return f"Query returned {len(gallery_items)} image(s) in {elapsed} seconds.", pd.DataFrame(), gallery_items
55
-
56
- return f"Query returned a table with {len(df)} row(s) in {elapsed} seconds.", df, []
57
-
58
-
59
-
60
-
61
- def handle_image_query(text):
62
- if not text:
63
- return []
64
-
65
- try:
66
- records = main_text_retrieve_images(text)
67
- print(f"Retrieved {len(records)} records for query: {text}")
68
- print(records)
69
- except Exception as e:
70
- return [("https://via.placeholder.com/300x200?text=Error", f"Error: {e}")]
71
-
72
- gallery_items = []
73
- for item in records:
74
- image_url = item.get("image_url")
75
- if not image_url:
76
- continue
77
- # Build a simple caption from the remaining fields
78
- caption = " | ".join(f"{k}: {v}" for k, v in item.items() if k != "image_url")
79
- gallery_items.append((image_url, caption))
80
-
81
- return gallery_items
82
-
83
- # --- UI --- #
84
  with gr.Blocks() as demo:
85
- gr.Markdown("# 🧵 FashionDB Interface")
86
-
87
-
88
- with gr.Tab("Structured Query"):
89
- gr.Markdown("Ask FashionDB anything and view results with images + metadata.")
90
-
91
- with gr.Row():
92
- query_input = gr.Textbox(label="Your question")
93
- sort_input = gr.Textbox(label="Sort by (optional column name)", placeholder="e.g. start_year")
94
-
95
- query_submit = gr.Button("Submit")
96
-
97
- query_text_output = gr.Textbox(label="Message", interactive=False)
98
- query_table_output = gr.Dataframe(label="Tabular Result", interactive=False)
99
- query_gallery_output = gr.Gallery(label="Image Gallery")
100
- query_submit.click(
101
- fn=handle_structured_query,
102
- inputs=[query_input, sort_input],
103
- outputs=[
104
- query_text_output,
105
- query_table_output,
106
- query_gallery_output
107
- ]
108
- )
109
-
110
- with gr.Tab("Image Retrieval"):
111
- gr.Markdown("Search for similar fashion show images based on a text description.")
112
- image_text = gr.Textbox(label="Describe the kind of images you're looking for")
113
- image_submit = gr.Button("Find Images")
114
- image_gallery = gr.Gallery(label="Retrieved Images")
115
-
116
- image_submit.click(handle_image_query, inputs=image_text, outputs=image_gallery)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  demo.launch()
 
1
  import gradio as gr
 
 
 
 
2
  import pandas as pd
3
+ import numpy as np
4
+ from search import search_images_by_text, get_similar_images
5
+ import requests
6
+ from io import BytesIO
7
+
8
+ import requests
9
+ from io import BytesIO
10
+ #@st.cache_data(show_spinner="Loading FashionDB...")
11
+ def load_data_hf():
12
+ # Load the Parquet file directly from Hugging Face
13
+ df_url = "https://huggingface.co/datasets/traopia/vogue_runway_small/resolve/main/VogueRunway.parquet"
14
+ df = pd.read_parquet(df_url)
15
+
16
+ # Load the .npy file using requests
17
+ npy_url = "https://huggingface.co/datasets/traopia/vogue_runway_small/resolve/main/VogueRunway_image.npy"
18
+ response = requests.get(npy_url)
19
+ response.raise_for_status() # Raise error if download fails
20
+ embeddings = np.load(BytesIO(response.content), mmap_mode="r")
21
+
22
+ return df, embeddings
23
+
24
+ df, embeddings = load_data_hf()
25
+
26
+ # Filter and search
27
+ def filter_and_search(fashion_house, category, season, year_range, query):
28
+ filtered = df.copy()
29
+
30
+ if fashion_house:
31
+ filtered = filtered[filtered['designer'].isin(fashion_house)]
32
+ if category:
33
+ filtered = filtered[filtered['category'].isin(category)]
34
+ if season:
35
+ filtered = filtered[filtered['season'].isin(season)]
36
+ filtered = filtered[(filtered['year'] >= year_range[0]) & (filtered['year'] <= year_range[1])]
37
+
38
+ if query:
39
+ results = search_images_by_text(query, filtered, embeddings)
40
+ else:
41
+ results = filtered.head(30)
42
+
43
+ image_urls = results["url"].tolist()
44
+ metadata = results.to_dict(orient="records")
45
+ return image_urls, metadata
46
+
47
+ # Display metadata and similar
48
+ def show_metadata(idx, metadata):
49
+ item = metadata[idx]
50
+ out = ""
51
+ for field in ["designer", "season", "year", "category"]:
52
+ if field in item and pd.notna(item[field]):
53
+ out += f"**{field.title()}**: {item[field]}\n"
54
+ if 'collection' in item and pd.notna(item['collection']):
55
+ out += f"\n[View Collection]({item['collection']})"
56
+ return out
57
+
58
+ def find_similar(idx, metadata):
59
+ key = metadata[idx]["key"]
60
+ similar_df = get_similar_images(df, key, embeddings, top_k=5)
61
+ return similar_df["url"].tolist(), similar_df.to_dict(orient="records")
62
+
63
+ # Gradio UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  with gr.Blocks() as demo:
65
+ gr.Markdown("# 👗 FashionDB Explorer")
66
+
67
+ with gr.Row():
68
+ fashion_house = gr.Dropdown(label="Fashion House", choices=sorted(df["designer"].dropna().unique()), multiselect=True)
69
+ category = gr.Dropdown(label="Category", choices=sorted(df["category"].dropna().unique()), multiselect=True)
70
+ season = gr.Dropdown(label="Season", choices=sorted(df["season"].dropna().unique()), multiselect=True)
71
+ year_range = gr.Slider(label="Year Range", minimum=int(df['year'].min()), maximum=int(df['year'].max()), value=(2000, 2025), step=1)
72
+
73
+ query = gr.Textbox(label="Search", placeholder="e.g., pink dress")
74
+ search_button = gr.Button("Search")
75
+
76
+ result_gallery = gr.Gallery(label="Search Results").style(grid=[5], height="auto")
77
+ metadata_output = gr.Markdown()
78
+ similar_gallery = gr.Gallery(label="Similar Images").style(grid=[5], height="auto")
79
+
80
+ metadata_state = gr.State([])
81
+ selected_idx = gr.Number(value=0, visible=False)
82
+
83
+ def handle_search(*args):
84
+ imgs, meta = filter_and_search(*args)
85
+ return imgs, meta, "", []
86
+
87
+ search_button.click(
88
+ handle_search,
89
+ inputs=[fashion_house, category, season, year_range, query],
90
+ outputs=[result_gallery, metadata_state, metadata_output, similar_gallery]
91
+ )
92
+
93
+ def handle_click(evt: gr.SelectData, metadata):
94
+ idx = evt.index
95
+ md = show_metadata(idx, metadata)
96
+ return idx, md
97
+
98
+ result_gallery.select(
99
+ handle_click,
100
+ inputs=[metadata_state],
101
+ outputs=[selected_idx, metadata_output]
102
+ )
103
+
104
+ def show_similar(idx, metadata):
105
+ return find_similar(int(idx), metadata)
106
+
107
+ show_similar_button = gr.Button("Show Similar Images")
108
+ show_similar_button.click(
109
+ show_similar,
110
+ inputs=[selected_idx, metadata_state],
111
+ outputs=[similar_gallery, metadata_state]
112
+ )
113
 
114
  demo.launch()
app_old.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ #example just for fun
3
+ from src.visual_qa import main_text_retrieve_images
4
+ from src.generate_queries_alternative import main_generate_queries
5
+ import time
6
+ import pandas as pd
7
+
8
+ import spacy
9
+
10
+ # Try to load the model, and download it if missing
11
+ try:
12
+ nlp = spacy.load("en_core_web_sm")
13
+ except OSError:
14
+ from spacy.cli import download
15
+ download("en_core_web_sm")
16
+ nlp = spacy.load("en_core_web_sm")
17
+
18
+
19
+
20
+ def handle_structured_query(question, sort_by=""):
21
+ if not question:
22
+ return "Please ask something 🙂", pd.DataFrame(), []
23
+
24
+ try:
25
+ start = time.time()
26
+ result_query, sparql_query = main_generate_queries(question)
27
+ elapsed = round(time.time() - start, 2)
28
+ except Exception as e:
29
+ return f"⚠️ Query failed: {e}", pd.DataFrame(), []
30
+
31
+ if isinstance(result_query, str):
32
+ return result_query, pd.DataFrame(), []
33
+
34
+ if not result_query:
35
+ return f"No results for '{question}'. Try rephrasing. (⏱ {elapsed}s)", pd.DataFrame(), []
36
+
37
+ df = pd.DataFrame(result_query)
38
+ if sort_by and sort_by in df.columns:
39
+ df = df.sort_values(by=sort_by)
40
+
41
+
42
+ if "image_url" in df.columns:
43
+ columns_of_interest = ["image_url", "year","fashion_collectionLabel", "reference_URL"]
44
+ df = df[columns_of_interest]
45
+ # Create a gallery: each item is (image_url, metadata string)
46
+ gallery_items = []
47
+ for _, row in df.iterrows():
48
+ image_url = row.get("image_url")
49
+ if not image_url:
50
+ continue
51
+ # Caption from other fields
52
+ caption = " | ".join(f"{k}: {v}" for k, v in row.items() if k != "image_url" and pd.notnull(v))
53
+ gallery_items.append((image_url, caption))
54
+ return f"Query returned {len(gallery_items)} image(s) in {elapsed} seconds.", pd.DataFrame(), gallery_items
55
+
56
+ return f"Query returned a table with {len(df)} row(s) in {elapsed} seconds.", df, []
57
+
58
+
59
+
60
+
61
+ def handle_image_query(text):
62
+ if not text:
63
+ return []
64
+
65
+ try:
66
+ records = main_text_retrieve_images(text)
67
+ print(f"Retrieved {len(records)} records for query: {text}")
68
+ print(records)
69
+ except Exception as e:
70
+ return [("https://via.placeholder.com/300x200?text=Error", f"Error: {e}")]
71
+
72
+ gallery_items = []
73
+ for item in records:
74
+ image_url = item.get("image_url")
75
+ if not image_url:
76
+ continue
77
+ # Build a simple caption from the remaining fields
78
+ caption = " | ".join(f"{k}: {v}" for k, v in item.items() if k != "image_url")
79
+ gallery_items.append((image_url, caption))
80
+
81
+ return gallery_items
82
+
83
+ # --- UI --- #
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("# 🧵 FashionDB Interface")
86
+
87
+
88
+ with gr.Tab("Structured Query"):
89
+ gr.Markdown("Ask FashionDB anything and view results with images + metadata.")
90
+
91
+ with gr.Row():
92
+ query_input = gr.Textbox(label="Your question")
93
+ sort_input = gr.Textbox(label="Sort by (optional column name)", placeholder="e.g. year")
94
+
95
+ query_submit = gr.Button("Submit")
96
+
97
+ query_text_output = gr.Textbox(label="Message", interactive=False)
98
+ query_table_output = gr.Dataframe(label="Tabular Result", interactive=False)
99
+ query_gallery_output = gr.Gallery(label="Image Gallery")
100
+ query_submit.click(
101
+ fn=handle_structured_query,
102
+ inputs=[query_input, sort_input],
103
+ outputs=[
104
+ query_text_output,
105
+ query_table_output,
106
+ query_gallery_output
107
+ ]
108
+ )
109
+
110
+ with gr.Tab("Image Retrieval"):
111
+ gr.Markdown("Search for similar fashion show images based on a text description.")
112
+ image_text = gr.Textbox(label="Describe the kind of images you're looking for")
113
+ image_submit = gr.Button("Find Images")
114
+ image_gallery = gr.Gallery(label="Retrieved Images")
115
+
116
+ image_submit.click(handle_image_query, inputs=image_text, outputs=image_gallery)
117
+
118
+ demo.launch()
gradio_app1.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # --- Handlers --- #
4
+ from src1.generate_queries_alternative import main_generate_queries
5
+ import time
6
+ import pandas as pd
7
+
8
+
9
+
10
+
11
+ def handle_structured_query(question, sort_by=""):
12
+ if not question:
13
+ return "Please ask something 🙂", pd.DataFrame(), []
14
+
15
+ try:
16
+ start = time.time()
17
+ result_query, sparql_query = main_generate_queries(question)
18
+ elapsed = round(time.time() - start, 2)
19
+ except Exception as e:
20
+ return f"⚠️ Query failed: {e}", pd.DataFrame(), []
21
+
22
+ if isinstance(result_query, str):
23
+ return result_query, pd.DataFrame(), []
24
+
25
+ if not result_query:
26
+ return f"No results for '{question}'. Try rephrasing. (⏱ {elapsed}s)", pd.DataFrame(), []
27
+
28
+ df = pd.DataFrame(result_query)
29
+ if sort_by and sort_by in df.columns:
30
+ df = df.sort_values(by=sort_by)
31
+
32
+
33
+ if "image_url" in df.columns:
34
+ columns_of_interest = ["image_url", "year","fashion_collectionLabel", "reference_URL"]
35
+ df = df[columns_of_interest]
36
+ # Create a gallery: each item is (image_url, metadata string)
37
+ gallery_items = []
38
+ for _, row in df.iterrows():
39
+ image_url = row.get("image_url")
40
+ if not image_url:
41
+ continue
42
+ # Caption from other fields
43
+ caption = " | ".join(f"{k}: {v}" for k, v in row.items() if k != "image_url" and pd.notnull(v))
44
+ gallery_items.append((image_url, caption))
45
+ return f"Query returned {len(gallery_items)} image(s) in {elapsed} seconds.", pd.DataFrame(), gallery_items
46
+
47
+ return f"Query returned a table with {len(df)} row(s) in {elapsed} seconds.", df, []
48
+
49
+
50
+
51
+
52
+
53
+ from src1.visual_qa import main_text_retrieve_images
54
+
55
+ def handle_image_query(text):
56
+ if not text:
57
+ return []
58
+
59
+ try:
60
+ records = main_text_retrieve_images(text)
61
+ except Exception as e:
62
+ return [("https://via.placeholder.com/300x200?text=Error", f"Error: {e}")]
63
+
64
+ gallery_items = []
65
+ for item in records:
66
+ image_url = item.get("image_url")
67
+ if not image_url:
68
+ continue
69
+ # Build a simple caption from the remaining fields
70
+ caption = " | ".join(f"{k}: {v}" for k, v in item.items() if k != "image_url")
71
+ gallery_items.append((image_url, caption))
72
+
73
+ return gallery_items
74
+
75
+ # --- UI --- #
76
+ with gr.Blocks() as demo:
77
+ gr.Markdown("# 🧵 FashionDB Interface")
78
+
79
+
80
+ with gr.Tab("Structured Query"):
81
+ gr.Markdown("Ask FashionDB anything and view results with images + metadata.")
82
+
83
+ with gr.Row():
84
+ query_input = gr.Textbox(label="Your question")
85
+ sort_input = gr.Textbox(label="Sort by (optional column name)", placeholder="e.g. start_year")
86
+
87
+ query_submit = gr.Button("Submit")
88
+
89
+ query_text_output = gr.Textbox(label="Message", interactive=False)
90
+ query_table_output = gr.Dataframe(label="Tabular Result", interactive=False)
91
+ query_gallery_output = gr.Gallery(label="Image Gallery")
92
+ query_submit.click(
93
+ fn=handle_structured_query,
94
+ inputs=[query_input, sort_input],
95
+ outputs=[
96
+ query_text_output,
97
+ query_table_output,
98
+ query_gallery_output
99
+ ]
100
+ )
101
+
102
+ with gr.Tab("Image Retrieval"):
103
+ gr.Markdown("Search for similar fashion show images based on a text description.")
104
+ image_text = gr.Textbox(label="Describe the kind of images you're looking for")
105
+ image_submit = gr.Button("Find Images")
106
+ image_gallery = gr.Gallery(label="Retrieved Images")
107
+
108
+ image_submit.click(handle_image_query, inputs=image_text, outputs=image_gallery)
109
+
110
+ demo.launch( share=True)
search.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import SentenceTransformer
2
+ from sklearn.metrics.pairwise import cosine_similarity
3
+ import numpy as np
4
+
5
+ # Use a compatible CLIP model
6
+ model = SentenceTransformer("clip-ViT-B-32")
7
+
8
+ def search_images_by_text(text, df, embeddings, top_k=30):
9
+ text_emb = model.encode([text])
10
+ filtered_embeddings = embeddings[df.index]
11
+ sims = cosine_similarity(text_emb, filtered_embeddings)[0]
12
+ top_indices = np.argsort(sims)[::-1][:top_k]
13
+ return df.iloc[top_indices]
14
+
15
+ def get_similar_images(df, image_id, embeddings, top_k=5):
16
+ index = int(image_id) # adjust based on your ID setup
17
+ query_emb = embeddings[index]
18
+ sims = cosine_similarity([query_emb], embeddings)[0]
19
+ top_indices = np.argsort(sims)[::-1][1:top_k+1]
20
+ return df.iloc[top_indices]
21
+
22
+