Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,114 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from modules import api_manager, health_ai, database
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Initialize
|
| 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
|
| 39 |
|
| 40 |
with gr.Tabs():
|
| 41 |
|
| 42 |
-
# TAB 1:
|
| 43 |
-
with gr.Tab("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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="
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
# TAB
|
| 67 |
-
with gr.Tab("π
|
| 68 |
-
search_input = gr.Textbox(label="Search by Name or Location
|
| 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
|
| 74 |
-
with gr.Tab("π¨βπ³ Owner
|
| 75 |
-
gr.
|
| 76 |
-
|
| 77 |
-
|
| 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__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from modules import api_manager, health_ai, database, image_gen
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Initialize Database on startup
|
| 6 |
database.init_db()
|
| 7 |
|
| 8 |
# --- LOGIC FUNCTIONS ---
|
| 9 |
|
| 10 |
+
# 1. Menu Analysis Logic
|
| 11 |
def process_menu_upload(image, language):
|
| 12 |
if image is None:
|
| 13 |
return "No image provided", "", {}, "Error"
|
|
|
|
|
|
|
| 14 |
extracted_text, ocr_source = api_manager.perform_ocr(image)
|
|
|
|
|
|
|
| 15 |
translated_text, trans_source = api_manager.translate_text(extracted_text, language)
|
|
|
|
|
|
|
| 16 |
health_data = health_ai.analyze_health(translated_text)
|
|
|
|
|
|
|
| 17 |
api_log = f"OCR Source: {ocr_source}\nTranslation Source: {trans_source}"
|
|
|
|
| 18 |
return extracted_text, translated_text, health_data, api_log
|
| 19 |
|
| 20 |
+
# 2. 3D Image Generation Logic
|
| 21 |
+
def generate_dish_visualization(dish_name):
|
| 22 |
+
if not dish_name:
|
| 23 |
+
return None, "Please enter a dish name."
|
| 24 |
+
image, status = image_gen.generate_food_image(dish_name)
|
| 25 |
+
return image, status
|
| 26 |
+
|
| 27 |
+
# 3. Auth & Database Logic
|
| 28 |
def register_rest_ui(name, loc, owner):
|
| 29 |
return database.add_restaurant(name, loc, owner)
|
|
|
|
| 30 |
def search_rest_ui(query):
|
| 31 |
return database.search_restaurants(query)
|
| 32 |
+
def signup_ui(email, password, role):
|
| 33 |
+
return database.create_user(email, password, role)
|
| 34 |
+
def login_ui(email, password):
|
| 35 |
+
return database.verify_login(email, password)
|
| 36 |
|
| 37 |
# --- UI LAYOUT ---
|
| 38 |
|
| 39 |
with gr.Blocks(theme=gr.themes.Soft(), title="MenuVision AI") as demo:
|
| 40 |
gr.Markdown("# π½οΈ MenuVision AI")
|
| 41 |
+
gr.Markdown("### Intelligent Restaurant Menu & 3D Visualization Platform")
|
| 42 |
|
| 43 |
with gr.Tabs():
|
| 44 |
|
| 45 |
+
# --- TAB 1: LOGIN & SIGNUP ---
|
| 46 |
+
with gr.Tab("π Login & Signup"):
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
gr.Markdown("### π Create Account")
|
| 50 |
+
su_email = gr.Textbox(label="Email")
|
| 51 |
+
su_pass = gr.Textbox(label="Password", type="password")
|
| 52 |
+
su_role = gr.Radio(["User", "Restaurant Owner"], label="Account Type", value="User")
|
| 53 |
+
su_btn = gr.Button("Sign Up", variant="primary")
|
| 54 |
+
su_msg = gr.Textbox(label="Status")
|
| 55 |
+
su_btn.click(signup_ui, inputs=[su_email, su_pass, su_role], outputs=su_msg)
|
| 56 |
+
|
| 57 |
+
with gr.Column():
|
| 58 |
+
gr.Markdown("### π Login")
|
| 59 |
+
li_email = gr.Textbox(label="Email")
|
| 60 |
+
li_pass = gr.Textbox(label="Password", type="password")
|
| 61 |
+
li_btn = gr.Button("Login")
|
| 62 |
+
li_msg = gr.Textbox(label="Login Status")
|
| 63 |
+
li_btn.click(login_ui, inputs=[li_email, li_pass], outputs=li_msg)
|
| 64 |
+
|
| 65 |
+
# --- TAB 2: MENU SCANNER ---
|
| 66 |
+
with gr.Tab("πΈ Menu Scanner"):
|
| 67 |
+
gr.Markdown("Upload a menu to extract text, translate it, and check health stats.")
|
| 68 |
with gr.Row():
|
| 69 |
with gr.Column():
|
| 70 |
img_input = gr.Image(type="pil", label="Upload Menu Image")
|
| 71 |
lang_input = gr.Dropdown(
|
| 72 |
+
["es", "fr", "de", "ur", "zh", "ar", "hi", "ru", "pt"],
|
| 73 |
+
label="Translate to", value="es"
|
|
|
|
| 74 |
)
|
| 75 |
analyze_btn = gr.Button("Analyze Menu", variant="primary")
|
|
|
|
| 76 |
with gr.Column():
|
| 77 |
+
ocr_output = gr.Textbox(label="Extracted Text", interactive=False, lines=4)
|
| 78 |
trans_output = gr.Textbox(label="Translated Text", interactive=False, lines=4)
|
| 79 |
health_output = gr.JSON(label="Health Insights")
|
| 80 |
+
api_status = gr.Textbox(label="Logs", interactive=False)
|
| 81 |
+
analyze_btn.click(process_menu_upload, inputs=[img_input, lang_input], outputs=[ocr_output, trans_output, health_output, api_status])
|
| 82 |
|
| 83 |
+
# --- TAB 3: 3D FOOD VISUALIZER ---
|
| 84 |
+
with gr.Tab("π 3D Food Visualizer"):
|
| 85 |
+
gr.Markdown("### See it before you order!")
|
| 86 |
+
gr.Markdown("Type a dish name to generate a realistic **3D image on a restaurant table**.")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column():
|
| 90 |
+
dish_input = gr.Textbox(label="Enter Dish Name (e.g., 'Spicy Pepperoni Pizza')")
|
| 91 |
+
gen_btn = gr.Button("Generate 3D Image", variant="primary")
|
| 92 |
+
with gr.Column():
|
| 93 |
+
gen_output = gr.Image(label="AI Generated Preview")
|
| 94 |
+
gen_status = gr.Textbox(label="Status", interactive=False)
|
| 95 |
+
|
| 96 |
+
gen_btn.click(generate_dish_visualization, inputs=dish_input, outputs=[gen_output, gen_status])
|
| 97 |
|
| 98 |
+
# --- TAB 4: SEARCH ---
|
| 99 |
+
with gr.Tab("π Search Restaurants"):
|
| 100 |
+
search_input = gr.Textbox(label="Search by Name or Location")
|
| 101 |
search_btn = gr.Button("Search")
|
| 102 |
search_output = gr.Dataframe(label="Results")
|
| 103 |
search_btn.click(search_rest_ui, inputs=search_input, outputs=search_output)
|
| 104 |
|
| 105 |
+
# --- TAB 5: OWNER DASHBOARD ---
|
| 106 |
+
with gr.Tab("π¨βπ³ Owner Dashboard"):
|
| 107 |
+
r_name = gr.Textbox(label="Restaurant Name")
|
| 108 |
+
r_loc = gr.Textbox(label="Location")
|
| 109 |
+
r_owner = gr.Textbox(label="Owner Name")
|
|
|
|
|
|
|
|
|
|
| 110 |
reg_btn = gr.Button("Register Restaurant")
|
| 111 |
reg_msg = gr.Textbox(label="Status")
|
|
|
|
| 112 |
reg_btn.click(register_rest_ui, inputs=[r_name, r_loc, r_owner], outputs=reg_msg)
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|