Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,45 +9,38 @@ import random
|
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 11 |
|
| 12 |
-
# --- 1.
|
| 13 |
IMAGE_DIR = "extracted_images"
|
| 14 |
-
DISHES_SUBDIR = "Dishes_Images" # 讛转讬拽讬讬讛 砖爪讬讬谞转
|
| 15 |
-
|
| 16 |
if os.path.exists('images.zip'):
|
| 17 |
with zipfile.ZipFile('images.zip', 'r') as zip_ref:
|
| 18 |
zip_ref.extractall(IMAGE_DIR)
|
| 19 |
|
| 20 |
def find_dish_image(idx):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
files = glob.glob(path_to_search)
|
| 24 |
-
|
| 25 |
-
# 讙讬讘讜讬: 讗诐 诇讗 诪爪讗 讘谞转讬讘 讛诪讚讜讬拽, 诪讞驻砖 讘讻诇 诪拽讜诐 讘转讜讱 讛讞讬诇讜抓
|
| 26 |
if not files:
|
| 27 |
files = glob.glob(os.path.join(IMAGE_DIR, "**", f"dish_{idx}_*.jpg"), recursive=True)
|
| 28 |
-
|
| 29 |
if files:
|
| 30 |
return f"file/{files[0]}"
|
| 31 |
return "https://via.placeholder.com/400x400?text=BiteWise+Dish"
|
| 32 |
|
| 33 |
-
# --- 2. 讟注讬谞
|
| 34 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 35 |
|
| 36 |
-
def
|
| 37 |
df = pd.read_csv('bitewise_clean_dataset.csv').fillna("N/A")
|
| 38 |
dish_emb = np.load('BiteWise_Dish_Embeddings.npy')
|
| 39 |
with open('BiteWise_User_Embeddings.pkl', 'rb') as f:
|
| 40 |
user_emb = pickle.load(f)
|
| 41 |
if isinstance(user_emb, list): user_emb = np.array(user_emb)
|
| 42 |
-
|
| 43 |
min_l = min(len(df), len(dish_emb), len(user_emb))
|
| 44 |
df = df.iloc[:min_l].reset_index(drop=True)
|
| 45 |
return df, dish_emb[:min_l], user_emb[:min_l]
|
| 46 |
|
| 47 |
-
main_df, dish_embeddings, user_embeddings =
|
| 48 |
NAMES = ["James Miller", "Sarah Johnson", "Michael Brown", "Emily Davis", "Robert Wilson"]
|
| 49 |
|
| 50 |
-
# --- 3. 诪谞讜注 讛讞讬驻讜砖
|
| 51 |
def run_discovery(query, origin, hobbies, style):
|
| 52 |
q_vec = model.encode([str(query)])
|
| 53 |
u_dna = f"Origin: {origin}, Hobbies: {hobbies}, Style: {style}"
|
|
@@ -58,8 +51,8 @@ def run_discovery(query, origin, hobbies, style):
|
|
| 58 |
final_scores = (dish_sim * 0.7) + (user_sim * 0.3)
|
| 59 |
|
| 60 |
res = main_df.copy()
|
| 61 |
-
res['
|
| 62 |
-
top_candidates = res.sort_values('
|
| 63 |
|
| 64 |
final_results = []
|
| 65 |
seen_users = set()
|
|
@@ -72,22 +65,22 @@ def run_discovery(query, origin, hobbies, style):
|
|
| 72 |
|
| 73 |
html_output = ""
|
| 74 |
for original_idx, row, u_name in final_results:
|
| 75 |
-
pct = f"{min(99.0, 85 + (row['
|
| 76 |
img_url = find_dish_image(original_idx)
|
| 77 |
|
| 78 |
html_output += f"""
|
| 79 |
-
<div style="border: 1px solid #C4A484; border-radius: 4px; padding: 25px; margin-bottom: 30px; background: #FFF9F5; border-left: 10px solid #3E2723; display: flex; gap: 25px;">
|
| 80 |
-
<img src="{img_url}" style="width: 250px; height: 250px; object-fit: cover; border:
|
| 81 |
<div style="flex: 1;">
|
| 82 |
<div style="display: flex; justify-content: space-between; align-items: center;">
|
| 83 |
-
<h2 style="margin: 0; color: #3E2723; font-family: 'Playfair Display', serif; font-size:
|
| 84 |
<span style="background: #3E2723; color: white; padding: 2px 12px; font-weight: bold; border-radius: 20px; font-size: 0.85em;">{pct} MATCH</span>
|
| 85 |
</div>
|
| 86 |
<p style="color: #7F4F24; margin: 8px 0; font-family: 'Courier New', monospace; font-weight: bold;">馃搷 {row['restaurant_name']} | {row.get('cuisine_type', 'Gourmet')}</p>
|
| 87 |
-
<p style="font-family: 'Playfair Display', serif; font-style: italic; color: #4E342E; margin: 15px 0; font-size: 1.
|
| 88 |
|
| 89 |
-
<div style="display: flex; gap: 10px; margin-bottom: 15px;
|
| 90 |
-
<span style="background: #EEDDCC; color: #3E2723; padding: 4px 10px; font-size: 0.75em; border: 1px solid #D2B48C; font-family: 'Courier New', monospace;">馃挵 {row.get('price_range', '$$')}</span>
|
| 91 |
<span style="background: #EEDDCC; color: #3E2723; padding: 4px 10px; font-size: 0.75em; border: 1px solid #D2B48C; font-family: 'Courier New', monospace;">馃懃 BEST FOR: {row.get('best_for', 'Friends')}</span>
|
| 92 |
</div>
|
| 93 |
|
|
@@ -99,14 +92,13 @@ def run_discovery(query, origin, hobbies, style):
|
|
| 99 |
"""
|
| 100 |
return html_output
|
| 101 |
|
| 102 |
-
def
|
| 103 |
-
return "### Thank you for your review! \nYour discovery has been successfully shared with the BiteWise archive."
|
| 104 |
|
| 105 |
-
# --- 4. 讛诪诪砖拽 讛诪
|
| 106 |
custom_css = """
|
| 107 |
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,700;1,400&display=swap');
|
| 108 |
-
.gradio-container { background-color: #FDFCF8 !important;
|
| 109 |
-
button.primary { background: #3E2723 !important; border-radius: 0px !important; font-family: 'Courier New', monospace; text-transform: uppercase; letter-spacing: 2px;
|
| 110 |
input, .dropdown { border-radius: 0px !important; border: 1px solid #D2B48C !important; background: white !important; }
|
| 111 |
"""
|
| 112 |
|
|
@@ -136,8 +128,8 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 136 |
gr.Textbox(label="DISH")
|
| 137 |
gr.Textbox(label="ESTABLISHMENT")
|
| 138 |
gr.Textbox(label="REVIEW", lines=3)
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
|
| 143 |
demo.launch(allowed_paths=["."])
|
|
|
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 11 |
|
| 12 |
+
# --- 1. 讛讻谞转 转诪讜谞讜转 (住谞讻专讜谉 转讬拽讬讬讛 驻谞讬诪讬转) ---
|
| 13 |
IMAGE_DIR = "extracted_images"
|
|
|
|
|
|
|
| 14 |
if os.path.exists('images.zip'):
|
| 15 |
with zipfile.ZipFile('images.zip', 'r') as zip_ref:
|
| 16 |
zip_ref.extractall(IMAGE_DIR)
|
| 17 |
|
| 18 |
def find_dish_image(idx):
|
| 19 |
+
pattern = os.path.join(IMAGE_DIR, "Dishes_Images", f"dish_{idx}_*.jpg")
|
| 20 |
+
files = glob.glob(pattern)
|
|
|
|
|
|
|
|
|
|
| 21 |
if not files:
|
| 22 |
files = glob.glob(os.path.join(IMAGE_DIR, "**", f"dish_{idx}_*.jpg"), recursive=True)
|
|
|
|
| 23 |
if files:
|
| 24 |
return f"file/{files[0]}"
|
| 25 |
return "https://via.placeholder.com/400x400?text=BiteWise+Dish"
|
| 26 |
|
| 27 |
+
# --- 2. 讟注讬谞讛 讜住谞讻专讜谉 讘专讝诇 ---
|
| 28 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 29 |
|
| 30 |
+
def load_data():
|
| 31 |
df = pd.read_csv('bitewise_clean_dataset.csv').fillna("N/A")
|
| 32 |
dish_emb = np.load('BiteWise_Dish_Embeddings.npy')
|
| 33 |
with open('BiteWise_User_Embeddings.pkl', 'rb') as f:
|
| 34 |
user_emb = pickle.load(f)
|
| 35 |
if isinstance(user_emb, list): user_emb = np.array(user_emb)
|
|
|
|
| 36 |
min_l = min(len(df), len(dish_emb), len(user_emb))
|
| 37 |
df = df.iloc[:min_l].reset_index(drop=True)
|
| 38 |
return df, dish_emb[:min_l], user_emb[:min_l]
|
| 39 |
|
| 40 |
+
main_df, dish_embeddings, user_embeddings = load_data()
|
| 41 |
NAMES = ["James Miller", "Sarah Johnson", "Michael Brown", "Emily Davis", "Robert Wilson"]
|
| 42 |
|
| 43 |
+
# --- 3. 诪谞讜注 讛讞讬驻讜砖 (Discovery) ---
|
| 44 |
def run_discovery(query, origin, hobbies, style):
|
| 45 |
q_vec = model.encode([str(query)])
|
| 46 |
u_dna = f"Origin: {origin}, Hobbies: {hobbies}, Style: {style}"
|
|
|
|
| 51 |
final_scores = (dish_sim * 0.7) + (user_sim * 0.3)
|
| 52 |
|
| 53 |
res = main_df.copy()
|
| 54 |
+
res['similarity_score'] = final_scores
|
| 55 |
+
top_candidates = res.sort_values('similarity_score', ascending=False).head(10)
|
| 56 |
|
| 57 |
final_results = []
|
| 58 |
seen_users = set()
|
|
|
|
| 65 |
|
| 66 |
html_output = ""
|
| 67 |
for original_idx, row, u_name in final_results:
|
| 68 |
+
pct = f"{min(99.0, 85 + (row['similarity_score'] * 15)):.1f}%"
|
| 69 |
img_url = find_dish_image(original_idx)
|
| 70 |
|
| 71 |
html_output += f"""
|
| 72 |
+
<div style="border: 1px solid #C4A484; border-radius: 4px; padding: 25px; margin-bottom: 30px; background: #FFF9F5; border-left: 10px solid #3E2723; display: flex; gap: 25px; color: #3E2723; box-shadow: 5px 5px 15px rgba(0,0,0,0.05);">
|
| 73 |
+
<img src="{img_url}" style="width: 250px; height: 250px; object-fit: cover; border: 1px solid #D2B48C; padding: 4px; background: white;">
|
| 74 |
<div style="flex: 1;">
|
| 75 |
<div style="display: flex; justify-content: space-between; align-items: center;">
|
| 76 |
+
<h2 style="margin: 0; color: #3E2723; font-family: 'Playfair Display', serif; font-size: 2em;">{row['dish_name']}</h2>
|
| 77 |
<span style="background: #3E2723; color: white; padding: 2px 12px; font-weight: bold; border-radius: 20px; font-size: 0.85em;">{pct} MATCH</span>
|
| 78 |
</div>
|
| 79 |
<p style="color: #7F4F24; margin: 8px 0; font-family: 'Courier New', monospace; font-weight: bold;">馃搷 {row['restaurant_name']} | {row.get('cuisine_type', 'Gourmet')}</p>
|
| 80 |
+
<p style="font-family: 'Playfair Display', serif; font-style: italic; color: #4E342E; margin: 15px 0; font-size: 1.1em; line-height: 1.4;">"{row['taste_review']}"</p>
|
| 81 |
|
| 82 |
+
<div style="display: flex; gap: 10px; margin-bottom: 15px;">
|
| 83 |
+
<span style="background: #EEDDCC; color: #3E2723; padding: 4px 10px; font-size: 0.75em; border: 1px solid #D2B48C; font-family: 'Courier New', monospace;">馃挵 PRICE: {row.get('price_range', '$$')}</span>
|
| 84 |
<span style="background: #EEDDCC; color: #3E2723; padding: 4px 10px; font-size: 0.75em; border: 1px solid #D2B48C; font-family: 'Courier New', monospace;">馃懃 BEST FOR: {row.get('best_for', 'Friends')}</span>
|
| 85 |
</div>
|
| 86 |
|
|
|
|
| 92 |
"""
|
| 93 |
return html_output
|
| 94 |
|
| 95 |
+
def archive_msg(): return "### Thank you! \nYour review has been successfully shared with the BiteWise archive."
|
|
|
|
| 96 |
|
| 97 |
+
# --- 4. 讛诪诪砖拽 讛诪注讜爪讘 (3 诪住讻讬诐) ---
|
| 98 |
custom_css = """
|
| 99 |
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,700;1,400&display=swap');
|
| 100 |
+
.gradio-container { background-color: #FDFCF8 !important; }
|
| 101 |
+
button.primary { background: #3E2723 !important; color: white !important; border-radius: 0px !important; font-family: 'Courier New', monospace; text-transform: uppercase; letter-spacing: 2px; }
|
| 102 |
input, .dropdown { border-radius: 0px !important; border: 1px solid #D2B48C !important; background: white !important; }
|
| 103 |
"""
|
| 104 |
|
|
|
|
| 128 |
gr.Textbox(label="DISH")
|
| 129 |
gr.Textbox(label="ESTABLISHMENT")
|
| 130 |
gr.Textbox(label="REVIEW", lines=3)
|
| 131 |
+
s_btn = gr.Button("SUBMIT & SHARE", variant="primary")
|
| 132 |
+
s_msg = gr.Markdown()
|
| 133 |
+
s_btn.click(archive_msg, None, s_msg)
|
| 134 |
|
| 135 |
demo.launch(allowed_paths=["."])
|