Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from modules import api_manager, health_ai, database
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Initialize DB on startup
|
| 6 |
+
database.init_db()
|
| 7 |
+
|
| 8 |
+
# --- LOGIC FUNCTIONS ---
|
| 9 |
+
|
| 10 |
+
def process_menu_upload(image, language):
|
| 11 |
+
if image is None:
|
| 12 |
+
return "No image provided", "", {}, "Error"
|
| 13 |
+
|
| 14 |
+
# 1. OCR Step
|
| 15 |
+
extracted_text, ocr_source = api_manager.perform_ocr(image)
|
| 16 |
+
|
| 17 |
+
# 2. Translation Step
|
| 18 |
+
translated_text, trans_source = api_manager.translate_text(extracted_text, language)
|
| 19 |
+
|
| 20 |
+
# 3. Health Analysis Step
|
| 21 |
+
health_data = health_ai.analyze_health(translated_text)
|
| 22 |
+
|
| 23 |
+
# Logs
|
| 24 |
+
api_log = f"OCR Source: {ocr_source}\nTranslation Source: {trans_source}"
|
| 25 |
+
|
| 26 |
+
return extracted_text, translated_text, health_data, api_log
|
| 27 |
+
|
| 28 |
+
def register_rest_ui(name, loc, owner):
|
| 29 |
+
return database.add_restaurant(name, loc, owner)
|
| 30 |
+
|
| 31 |
+
def search_rest_ui(query):
|
| 32 |
+
return database.search_restaurants(query)
|
| 33 |
+
|
| 34 |
+
# --- UI LAYOUT ---
|
| 35 |
+
|
| 36 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="MenuVision AI") as demo:
|
| 37 |
+
gr.Markdown("# 🍽️ MenuVision AI")
|
| 38 |
+
gr.Markdown("### Intelligent Restaurant Menu Platform (Dual-API Architecture)")
|
| 39 |
+
|
| 40 |
+
with gr.Tabs():
|
| 41 |
+
|
| 42 |
+
# TAB 1: MENU ANALYSIS
|
| 43 |
+
with gr.Tab("📸 Guest: Analyze Menu"):
|
| 44 |
+
with gr.Row():
|
| 45 |
+
with gr.Column():
|
| 46 |
+
img_input = gr.Image(type="pil", label="Upload Menu Image")
|
| 47 |
+
lang_input = gr.Dropdown(
|
| 48 |
+
["es", "fr", "de", "ur", "zh", "ar"],
|
| 49 |
+
label="Translate to",
|
| 50 |
+
value="es"
|
| 51 |
+
)
|
| 52 |
+
analyze_btn = gr.Button("Analyze Menu", variant="primary")
|
| 53 |
+
|
| 54 |
+
with gr.Column():
|
| 55 |
+
ocr_output = gr.Textbox(label="Extracted Text (OCR)", interactive=False, lines=4)
|
| 56 |
+
trans_output = gr.Textbox(label="Translated Text", interactive=False, lines=4)
|
| 57 |
+
health_output = gr.JSON(label="Health Insights")
|
| 58 |
+
api_status = gr.Textbox(label="System Logs", interactive=False)
|
| 59 |
+
|
| 60 |
+
analyze_btn.click(
|
| 61 |
+
process_menu_upload,
|
| 62 |
+
inputs=[img_input, lang_input],
|
| 63 |
+
outputs=[ocr_output, trans_output, health_output, api_status]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# TAB 2: SEARCH RESTAURANTS
|
| 67 |
+
with gr.Tab("🔍 Guest: Search Restaurants"):
|
| 68 |
+
search_input = gr.Textbox(label="Search by Name or Location (e.g., 'Pizza')")
|
| 69 |
+
search_btn = gr.Button("Search")
|
| 70 |
+
search_output = gr.Dataframe(label="Results")
|
| 71 |
+
search_btn.click(search_rest_ui, inputs=search_input, outputs=search_output)
|
| 72 |
+
|
| 73 |
+
# TAB 3: OWNER DASHBOARD
|
| 74 |
+
with gr.Tab("👨🍳 Owner: Register"):
|
| 75 |
+
gr.Markdown("Add your restaurant to the public database.")
|
| 76 |
+
with gr.Row():
|
| 77 |
+
r_name = gr.Textbox(label="Restaurant Name")
|
| 78 |
+
r_loc = gr.Textbox(label="Location")
|
| 79 |
+
r_owner = gr.Textbox(label="Owner Name")
|
| 80 |
+
|
| 81 |
+
reg_btn = gr.Button("Register Restaurant")
|
| 82 |
+
reg_msg = gr.Textbox(label="Status")
|
| 83 |
+
|
| 84 |
+
reg_btn.click(register_rest_ui, inputs=[r_name, r_loc, r_owner], outputs=reg_msg)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|