Wall06 commited on
Commit
b435483
·
verified ·
1 Parent(s): ce2c71b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -106
app.py CHANGED
@@ -1,115 +1,95 @@
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__":
115
- demo.launch()
 
 
1
+ # app.py
 
2
  import os
3
+ from flask import Flask, render_template, request, jsonify, send_from_directory
4
+ from graphviz import Digraph
5
+ import random
6
 
7
+ app = Flask(__name__)
 
8
 
9
+ # CONFIGURATION
10
+ UPLOAD_FOLDER = 'static/uploads'
11
+ DIAGRAM_FOLDER = 'static/diagrams'
12
+ MODEL_FOLDER = 'static/models'
13
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
14
+ os.makedirs(DIAGRAM_FOLDER, exist_ok=True)
15
 
16
+ # --- FEATURE 1: AI CODE VISUALIZATION ENGINE ---
17
+ @app.route('/generate_code_diagram', methods=['POST'])
18
+ def generate_code_diagram():
19
+ """
20
+ Takes code/logic text and creates a visual flowchart image.
21
+ In a real app, you would use an LLM to parse complex code.
22
+ Here, we simulate the visualization of logic flow.
23
+ """
24
+ data = request.json
25
+ code_text = data.get('code', '')
26
+
27
+ # Create a visual graph (The "Code Image")
28
+ dot = Digraph(comment='Code Flow', format='png')
29
+ dot.attr(rankdir='TB', size='8,5')
30
+
31
+ # Logic to turn text into nodes (Simulated AI parsing)
32
+ dot.node('A', 'Start: User Input')
33
+ dot.node('B', 'AI Analysis')
34
+ dot.node('C', 'Generate 3D Asset')
35
+ dot.node('D', 'AR Deployment')
36
+
37
+ dot.edge('A', 'B', label='Upload Image')
38
+ dot.edge('B', 'C', label='Identify Food')
39
+ dot.edge('C', 'D', label='Render GLB')
40
+
41
+ # Save the diagram
42
+ filename = f"flow_{random.randint(1000,9999)}"
43
+ filepath = os.path.join(DIAGRAM_FOLDER, filename)
44
+ dot.render(filepath)
45
+
46
+ return jsonify({'diagram_url': f"/{filepath}.png"})
47
 
48
+ # --- FEATURE 2: AI FOOD ANALYSIS & 3D SELECTOR ---
49
+ @app.route('/analyze_food', methods=['POST'])
50
+ def analyze_food():
51
+ """
52
+ 1. Receives food image.
53
+ 2. 'AI' identifies it (Simulated for this demo).
54
+ 3. Returns the correct 3D model file for the table.
55
+ """
56
+ if 'image' not in request.files:
57
+ return jsonify({'error': 'No image uploaded'}), 400
58
+
59
+ file = request.files['image']
60
+ # Save file logic here...
61
+
62
+ # MOCK AI RECOGNITION LOGIC
63
+ # In a real app, use TensorFlow/YOLO here to detect "Pizza" or "Burger"
64
+ # For demo, we randomly detect one to show the switching capability.
65
+ detected_food = random.choice(['burger', 'pizza'])
66
+
67
+ response_data = {
68
+ 'food_detected': detected_food,
69
+ 'confidence': '98%',
70
+ 'model_url': f"/static/models/{detected_food}.glb", # Returns the 3D file path
71
+ 'calories': '450 kcal'
72
+ }
73
+ return jsonify(response_data)
74
 
75
+ # --- FEATURE 3: AI GUIDE CHAT ---
76
+ @app.route('/chat_guide', methods=['POST'])
77
+ def chat_guide():
78
+ user_msg = request.json.get('message', '').lower()
79
+
80
+ if "price" in user_msg:
81
+ reply = "This dish costs $12.99 based on the portion size shown."
82
+ elif "spicy" in user_msg:
83
+ reply = "This dish is rated 2/5 on the spice scale."
84
+ else:
85
+ reply = "I am your MenuVision Assistant. Upload a photo to see it in 3D on your table!"
 
 
 
 
 
 
86
 
87
+ return jsonify({'reply': reply})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
+ @app.route('/')
90
+ def index():
91
+ return render_template('index.html')
 
 
 
 
 
92
 
93
+ if __name__ == '__main__':
94
+ # Run on 0.0.0.0 so you can access it from your phone
95
+ app.run(host='0.0.0.0', port=5000, debug=True)