Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ if os.path.exists('images.zip'):
|
|
| 15 |
with zipfile.ZipFile('images.zip', 'r') as zip_ref:
|
| 16 |
zip_ref.extractall(IMAGE_DIR)
|
| 17 |
|
| 18 |
-
# --- 2. טעינה וסנכרון ברזל (הע
|
| 19 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 20 |
|
| 21 |
def load_and_sync_data():
|
|
@@ -26,7 +26,7 @@ def load_and_sync_data():
|
|
| 26 |
user_emb = pickle.load(f)
|
| 27 |
if isinstance(user_emb, list): user_emb = np.array(user_emb)
|
| 28 |
|
| 29 |
-
# חיתוך אחיד למניעת הסטה - המבורגר
|
| 30 |
min_l = min(len(df), len(dish_emb), len(user_emb))
|
| 31 |
df = df.iloc[:min_l].reset_index(drop=True)
|
| 32 |
dish_emb = dish_emb[:min_l]
|
|
@@ -34,11 +34,9 @@ def load_and_sync_data():
|
|
| 34 |
return df, dish_emb, user_emb
|
| 35 |
|
| 36 |
main_df, dish_embeddings, user_embeddings = load_and_sync_data()
|
| 37 |
-
|
| 38 |
-
# שמות אמריקאיים לגיבוי ממליצים
|
| 39 |
AMERICAN_NAMES = ["James Miller", "Sarah Johnson", "Michael Brown", "Emily Davis", "Robert Wilson", "Sophia Martinez"]
|
| 40 |
|
| 41 |
-
# --- 3. מנוע החיפוש (Discovery) ---
|
| 42 |
def run_discovery(query, origin, hobbies, style):
|
| 43 |
alpha = 0.7
|
| 44 |
q_vec = model.encode([str(query)])
|
|
@@ -53,15 +51,14 @@ def run_discovery(query, origin, hobbies, style):
|
|
| 53 |
res = main_df.copy()
|
| 54 |
res['similarity_score'] = final_scores
|
| 55 |
|
| 56 |
-
# סינון
|
| 57 |
-
|
| 58 |
-
top_candidates = res[res['rating'] >= 4.0].sort_values('similarity_score', ascending=False)
|
| 59 |
|
| 60 |
final_results = []
|
| 61 |
seen_users = set()
|
| 62 |
|
| 63 |
for idx, row in top_candidates.iterrows():
|
| 64 |
-
#
|
| 65 |
u_name = row['user_name']
|
| 66 |
if u_name == "N/A" or not u_name:
|
| 67 |
u_name = random.choice(AMERICAN_NAMES)
|
|
@@ -75,11 +72,12 @@ def run_discovery(query, origin, hobbies, style):
|
|
| 75 |
for original_idx, row, u_name in final_results:
|
| 76 |
pct = f"{min(99.0, 85 + (row['similarity_score'] * 15)):.1f}%"
|
| 77 |
|
| 78 |
-
# חיפוש תמונה בתוך
|
| 79 |
pattern = os.path.join(IMAGE_DIR, "**", f"dish_{original_idx}_*.jpg")
|
| 80 |
img_files = glob.glob(pattern, recursive=True)
|
| 81 |
img_url = f"file/{img_files[0]}" if img_files else "https://via.placeholder.com/400x400?text=BiteWise+Dish"
|
| 82 |
|
|
|
|
| 83 |
html_output += f"""
|
| 84 |
<div style="border: 1px solid #C4A484; border-radius: 4px; padding: 25px; margin-bottom: 30px; background: #FFF9F5; box-shadow: 5px 5px 15px rgba(0,0,0,0.05); border-left: 10px solid #3E2723; display: flex; gap: 25px;">
|
| 85 |
<img src="{img_url}" style="width: 250px; height: 250px; object-fit: cover; border: 4px solid #FAF9F6; outline: 1px solid #D2B48C;">
|
|
@@ -88,8 +86,15 @@ def run_discovery(query, origin, hobbies, style):
|
|
| 88 |
<h2 style="margin: 0; color: #3E2723; font-family: 'Playfair Display', serif; font-size: 2.2em;">{row['dish_name']}</h2>
|
| 89 |
<span style="background: #3E2723; color: white; padding: 2px 12px; font-weight: bold; border-radius: 20px; font-size: 0.85em;">{pct} MATCH</span>
|
| 90 |
</div>
|
| 91 |
-
<p style="color: #7F4F24; margin: 8px 0; font-family: 'Courier New', monospace; font-weight: bold;">📍 {row['restaurant_name']}</p>
|
| 92 |
<p style="font-family: 'Playfair Display', serif; font-style: italic; color: #4E342E; margin: 15px 0; font-size: 1.2em; line-height: 1.4;">"{row['taste_review']}"</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
<div style="background: #F5F1EE; padding: 12px; border-radius: 4px; font-size: 0.9em; color: #5D4037;">
|
| 94 |
<b>Twin:</b> {u_name} from {row['user_origin']} | <b>Rating:</b> {row['rating']}/5.0
|
| 95 |
</div>
|
|
@@ -98,7 +103,10 @@ def run_discovery(query, origin, hobbies, style):
|
|
| 98 |
"""
|
| 99 |
return html_output
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
custom_css = """
|
| 103 |
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,700;1,400&display=swap');
|
| 104 |
.gradio-container { background-color: #FDFCF8 !important; color: #3E2723; }
|
|
@@ -128,9 +136,12 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 128 |
|
| 129 |
with gr.Tab("03. ARCHIVE", id=2):
|
| 130 |
gr.Markdown("### ARCHIVE A NEW CULINARY DISCOVERY")
|
| 131 |
-
gr.
|
| 132 |
-
|
|
|
|
| 133 |
gr.Textbox(label="REVIEW", lines=3)
|
| 134 |
-
gr.Button("SUBMIT
|
|
|
|
|
|
|
| 135 |
|
| 136 |
demo.launch(allowed_paths=["."])
|
|
|
|
| 15 |
with zipfile.ZipFile('images.zip', 'r') as zip_ref:
|
| 16 |
zip_ref.extractall(IMAGE_DIR)
|
| 17 |
|
| 18 |
+
# --- 2. טעינה וסנכרון ברזל (מבטיח שהמידע מה-CSV יהיה מדויק) ---
|
| 19 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 20 |
|
| 21 |
def load_and_sync_data():
|
|
|
|
| 26 |
user_emb = pickle.load(f)
|
| 27 |
if isinstance(user_emb, list): user_emb = np.array(user_emb)
|
| 28 |
|
| 29 |
+
# חיתוך אחיד למניעת הסטה - מבטיח המבורגר = המבורגר
|
| 30 |
min_l = min(len(df), len(dish_emb), len(user_emb))
|
| 31 |
df = df.iloc[:min_l].reset_index(drop=True)
|
| 32 |
dish_emb = dish_emb[:min_l]
|
|
|
|
| 34 |
return df, dish_emb, user_emb
|
| 35 |
|
| 36 |
main_df, dish_embeddings, user_embeddings = load_and_sync_data()
|
|
|
|
|
|
|
| 37 |
AMERICAN_NAMES = ["James Miller", "Sarah Johnson", "Michael Brown", "Emily Davis", "Robert Wilson", "Sophia Martinez"]
|
| 38 |
|
| 39 |
+
# --- 3. מנוע החיפוש (Discovery) עם כל הפיצ'רים מהדאטה ---
|
| 40 |
def run_discovery(query, origin, hobbies, style):
|
| 41 |
alpha = 0.7
|
| 42 |
q_vec = model.encode([str(query)])
|
|
|
|
| 51 |
res = main_df.copy()
|
| 52 |
res['similarity_score'] = final_scores
|
| 53 |
|
| 54 |
+
# סינון ומיון (לוקחים טופ 10 כדי לוודא 3 תאומים שונים)
|
| 55 |
+
top_candidates = res.sort_values('similarity_score', ascending=False).head(10)
|
|
|
|
| 56 |
|
| 57 |
final_results = []
|
| 58 |
seen_users = set()
|
| 59 |
|
| 60 |
for idx, row in top_candidates.iterrows():
|
| 61 |
+
# שם הממליץ (מהדאטה או רנדומלי)
|
| 62 |
u_name = row['user_name']
|
| 63 |
if u_name == "N/A" or not u_name:
|
| 64 |
u_name = random.choice(AMERICAN_NAMES)
|
|
|
|
| 72 |
for original_idx, row, u_name in final_results:
|
| 73 |
pct = f"{min(99.0, 85 + (row['similarity_score'] * 15)):.1f}%"
|
| 74 |
|
| 75 |
+
# חיפוש תמונה - סורק את כל התיקיות בתוך הזיפ שחולץ
|
| 76 |
pattern = os.path.join(IMAGE_DIR, "**", f"dish_{original_idx}_*.jpg")
|
| 77 |
img_files = glob.glob(pattern, recursive=True)
|
| 78 |
img_url = f"file/{img_files[0]}" if img_files else "https://via.placeholder.com/400x400?text=BiteWise+Dish"
|
| 79 |
|
| 80 |
+
# תצוגת הכרטיסייה עם כל הנתונים מה-CSV (מחיר, עם מי לאכול, סוג מטבח)
|
| 81 |
html_output += f"""
|
| 82 |
<div style="border: 1px solid #C4A484; border-radius: 4px; padding: 25px; margin-bottom: 30px; background: #FFF9F5; box-shadow: 5px 5px 15px rgba(0,0,0,0.05); border-left: 10px solid #3E2723; display: flex; gap: 25px;">
|
| 83 |
<img src="{img_url}" style="width: 250px; height: 250px; object-fit: cover; border: 4px solid #FAF9F6; outline: 1px solid #D2B48C;">
|
|
|
|
| 86 |
<h2 style="margin: 0; color: #3E2723; font-family: 'Playfair Display', serif; font-size: 2.2em;">{row['dish_name']}</h2>
|
| 87 |
<span style="background: #3E2723; color: white; padding: 2px 12px; font-weight: bold; border-radius: 20px; font-size: 0.85em;">{pct} MATCH</span>
|
| 88 |
</div>
|
| 89 |
+
<p style="color: #7F4F24; margin: 8px 0; font-family: 'Courier New', monospace; font-weight: bold;">📍 {row['restaurant_name']} | {row.get('cuisine_type', 'Gourmet')}</p>
|
| 90 |
<p style="font-family: 'Playfair Display', serif; font-style: italic; color: #4E342E; margin: 15px 0; font-size: 1.2em; line-height: 1.4;">"{row['taste_review']}"</p>
|
| 91 |
+
|
| 92 |
+
<div style="display: flex; gap: 10px; margin-bottom: 15px; flex-wrap: wrap;">
|
| 93 |
+
<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>
|
| 94 |
+
<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>
|
| 95 |
+
<span style="background: #EEDDCC; color: #3E2723; padding: 4px 10px; font-size: 0.75em; border: 1px solid #D2B48C; font-family: 'Courier New', monospace;">✨ VIBE: {row.get('food_vibe', 'Cozy')}</span>
|
| 96 |
+
</div>
|
| 97 |
+
|
| 98 |
<div style="background: #F5F1EE; padding: 12px; border-radius: 4px; font-size: 0.9em; color: #5D4037;">
|
| 99 |
<b>Twin:</b> {u_name} from {row['user_origin']} | <b>Rating:</b> {row['rating']}/5.0
|
| 100 |
</div>
|
|
|
|
| 103 |
"""
|
| 104 |
return html_output
|
| 105 |
|
| 106 |
+
def archive_submission():
|
| 107 |
+
return "### Thank you for your review! \nYour discovery has been successfully synced and shared with the BiteWise archive."
|
| 108 |
+
|
| 109 |
+
# --- 4. ממשק בעיצוב Vintage Soul ---
|
| 110 |
custom_css = """
|
| 111 |
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,700;1,400&display=swap');
|
| 112 |
.gradio-container { background-color: #FDFCF8 !important; color: #3E2723; }
|
|
|
|
| 136 |
|
| 137 |
with gr.Tab("03. ARCHIVE", id=2):
|
| 138 |
gr.Markdown("### ARCHIVE A NEW CULINARY DISCOVERY")
|
| 139 |
+
with gr.Row():
|
| 140 |
+
gr.Textbox(label="DISH")
|
| 141 |
+
gr.Textbox(label="ESTABLISHMENT")
|
| 142 |
gr.Textbox(label="REVIEW", lines=3)
|
| 143 |
+
submit_btn = gr.Button("SUBMIT & SHARE", variant="primary")
|
| 144 |
+
status_msg = gr.Markdown()
|
| 145 |
+
submit_btn.click(archive_submission, None, status_msg)
|
| 146 |
|
| 147 |
demo.launch(allowed_paths=["."])
|