Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,149 +2,96 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
-
import
|
| 6 |
from PIL import Image
|
| 7 |
-
|
| 8 |
-
from datasets import load_dataset
|
| 9 |
-
from torch.nn import functional as F
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
print(
|
| 16 |
-
try:
|
| 17 |
-
model = AutoModel.from_pretrained(MODEL_ID)
|
| 18 |
-
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"β Model Error: {e}")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
#
|
| 27 |
df = pd.read_parquet(DATA_FILE)
|
| 28 |
-
valid_indices = df.index.tolist() # Assuming you preserved the original indices in the dataframe index
|
| 29 |
-
# OR if you reset the index in the notebook, we just check the length:
|
| 30 |
-
num_embeddings = len(df)
|
| 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 |
-
return []
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
scores, indices = torch.topk(similarity, k=top_k)
|
| 71 |
|
|
|
|
| 72 |
results = []
|
| 73 |
-
for idx, score in zip(
|
| 74 |
idx = idx.item()
|
|
|
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
img_data =
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
img_data.save(save_path)
|
| 85 |
-
|
| 86 |
-
label = df.iloc[idx]['label_name']
|
| 87 |
-
|
| 88 |
-
# 4. Return the PATH (string), NOT the image object
|
| 89 |
-
results.append((save_path, f"{label} ({score:.2f})"))
|
| 90 |
|
| 91 |
return results
|
| 92 |
|
| 93 |
-
# ---
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
inputs = processor(images=input_image, return_tensors="pt")
|
| 97 |
-
with torch.no_grad():
|
| 98 |
-
features = model.get_image_features(**inputs)
|
| 99 |
-
return find_best_matches(features)
|
| 100 |
-
|
| 101 |
-
def search_by_text(input_text):
|
| 102 |
-
if not input_text: return []
|
| 103 |
-
inputs = processor(text=[input_text], return_tensors="pt", padding="max_length")
|
| 104 |
-
with torch.no_grad():
|
| 105 |
-
features = model.get_text_features(**inputs)
|
| 106 |
-
return find_best_matches(features)
|
| 107 |
-
|
| 108 |
-
# --- 6. BUILD UI (Clean & Centered) ---
|
| 109 |
-
custom_css = """
|
| 110 |
-
.gradio-container { width: 100%; max-width: 1000px; margin: 0 auto !important; }
|
| 111 |
-
h1 { text-align: center; color: #E67E22; }
|
| 112 |
-
"""
|
| 113 |
-
|
| 114 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="Food Matcher AI") as demo:
|
| 115 |
|
| 116 |
with gr.Row():
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
gr.HTML('<div style="display:flex; justify-content:center;"><iframe width="560" height="315" src="https://www.youtube.com/embed/IXeIxYHi0Es" frameborder="0" allowfullscreen></iframe></div>')
|
| 123 |
-
|
| 124 |
-
with gr.Tab("πΌοΈ Search by Image"):
|
| 125 |
-
with gr.Row():
|
| 126 |
-
with gr.Column(scale=1):
|
| 127 |
-
img_input = gr.Image(type="pil", label="Your Photo", height=300)
|
| 128 |
-
btn_img = gr.Button("π Find Matches", variant="primary", size="lg")
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
btn_img.click(search_by_image, inputs=img_input, outputs=img_gallery)
|
| 134 |
-
|
| 135 |
-
with gr.Tab("π Search by Text"):
|
| 136 |
-
with gr.Row():
|
| 137 |
-
with gr.Column(scale=1):
|
| 138 |
-
txt_input = gr.Textbox(label="Describe it", placeholder="e.g. 'Spicy Tacos'", lines=4)
|
| 139 |
-
btn_txt = gr.Button("π Search", variant="primary", size="lg")
|
| 140 |
-
|
| 141 |
-
with gr.Column(scale=2):
|
| 142 |
-
txt_gallery = gr.Gallery(label="Similar Dishes", columns=3, height=350, object_fit="contain")
|
| 143 |
-
|
| 144 |
-
btn_txt.click(search_by_text, inputs=txt_input, outputs=txt_gallery)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
|
| 149 |
-
#
|
| 150 |
-
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
+
from transformers import AutoModel, AutoProcessor
|
| 6 |
from PIL import Image
|
| 7 |
+
import io
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# --- CONFIGURATION ---
|
| 10 |
+
# β οΈ IMPORTANT: Change this if 'MetaCLIP' or 'OpenAI CLIP' won your notebook battle!
|
| 11 |
+
MODEL_ID = "google/siglip-base-patch16-224"
|
| 12 |
+
DATA_FILE = "food_embeddings_best.parquet"
|
| 13 |
|
| 14 |
+
print("β³ Loading Model & Data...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# 1. Load Model (Only once)
|
| 17 |
+
model = AutoModel.from_pretrained(MODEL_ID)
|
| 18 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 19 |
|
| 20 |
+
# 2. Load the "Memory" (Parquet file)
|
| 21 |
df = pd.read_parquet(DATA_FILE)
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# 3. Prepare the Database Vectors
|
| 24 |
+
# Convert the dataframe column into a PyTorch Tensor
|
| 25 |
+
all_vectors = np.stack(df['embedding'].to_numpy())
|
| 26 |
+
db_features = torch.tensor(all_vectors)
|
| 27 |
+
|
| 28 |
+
# (Optional: If your notebook didn't normalize, uncomment this.
|
| 29 |
+
# But your notebook code already did, so we skip it to be fast!)
|
| 30 |
+
# db_features = db_features / db_features.norm(p=2, dim=-1, keepdim=True)
|
| 31 |
+
|
| 32 |
+
print("β
System Ready!")
|
| 33 |
+
|
| 34 |
+
def search(text_query, image_query):
|
| 35 |
+
# A. Decide: Is this a Text search or Image search?
|
| 36 |
+
if image_query:
|
| 37 |
+
# Process Image
|
| 38 |
+
inputs = processor(images=image_query, return_tensors="pt")
|
| 39 |
+
get_feat_func = model.get_image_features
|
| 40 |
+
elif text_query:
|
| 41 |
+
# Process Text
|
| 42 |
+
inputs = processor(text=[text_query], return_tensors="pt", padding=True)
|
| 43 |
+
get_feat_func = model.get_text_features
|
| 44 |
+
else:
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
# B. Run Model (Inference)
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
query_vec = get_feat_func(**inputs)
|
| 50 |
|
| 51 |
+
# C. Search Logic (Pure Math)
|
| 52 |
+
# 1. Normalize Query (Math requirement: Vector / Magnitude)
|
| 53 |
+
query_vec = query_vec / query_vec.norm(p=2, dim=-1, keepdim=True)
|
|
|
|
| 54 |
|
| 55 |
+
# 2. Dot Product (Similarity)
|
| 56 |
+
scores = torch.mm(query_vec, db_features.T)
|
| 57 |
|
| 58 |
+
# 3. Get Top 5
|
| 59 |
+
top_scores, top_indices = torch.topk(scores, k=5)
|
|
|
|
| 60 |
|
| 61 |
+
# D. Fetch Results
|
| 62 |
results = []
|
| 63 |
+
for idx, score in zip(top_indices[0], top_scores[0]):
|
| 64 |
idx = idx.item()
|
| 65 |
+
row = df.iloc[idx]
|
| 66 |
|
| 67 |
+
# Handle Image Loading (Parquet saves images as binary/dict)
|
| 68 |
+
img_data = row['image']
|
| 69 |
+
if isinstance(img_data, dict) and 'bytes' in img_data:
|
| 70 |
+
img = Image.open(io.BytesIO(img_data['bytes']))
|
| 71 |
+
else:
|
| 72 |
+
img = img_data # It might already be a PIL object
|
| 73 |
+
|
| 74 |
+
results.append((img, f"{row['label_name']} ({score.item():.2f})"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
return results
|
| 77 |
|
| 78 |
+
# --- INTERFACE ---
|
| 79 |
+
with gr.Blocks(title="AI Food Search") as demo:
|
| 80 |
+
gr.Markdown("# π AI Food Search")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
+
# Left: Inputs
|
| 84 |
+
with gr.Column():
|
| 85 |
+
txt_input = gr.Textbox(label="Search by Text", placeholder="e.g. 'spicy pepperoni pizza'")
|
| 86 |
+
img_input = gr.Image(type="pil", label="Or Search by Image")
|
| 87 |
+
btn = gr.Button("Search", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Right: Output Gallery
|
| 90 |
+
with gr.Column():
|
| 91 |
+
gallery = gr.Gallery(label="Top Matches")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Connect buttons
|
| 94 |
+
btn.click(fn=search, inputs=[txt_input, img_input], outputs=gallery)
|
| 95 |
|
| 96 |
+
# Force bind to 0.0.0.0 for Spaces
|
| 97 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|