Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,95 @@
|
|
| 1 |
-
|
| 2 |
-
from modules import api_manager, health_ai, database, image_gen
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
database.init_db()
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# 1
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# 2
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# 3
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
|
| 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 |
-
|
| 106 |
-
|
| 107 |
-
|
| 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__ ==
|
| 115 |
-
|
|
|
|
|
|
| 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)
|