Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import easyocr | |
| import numpy as np | |
| from PIL import Image | |
| import os | |
| import folium | |
| from huggingface_hub import InferenceClient | |
| # --- CONFIGURATION --- | |
| # Replace with your actual token if you don't want to paste it in the UI every time | |
| HF_TOKEN = "hf_..." | |
| # Initialize Tools | |
| reader = easyocr.Reader(['en']) | |
| # --- LOGIC 1: AI HEALTH ANALYZER & IMAGE GENERATOR --- | |
| def analyze_food(food_name, language, api_key): | |
| """ | |
| Generates a real-world image AND detailed health analysis in the selected language. | |
| """ | |
| if not food_name: | |
| return None, "Please select a food item.", "" | |
| token = api_key if api_key else HF_TOKEN | |
| # Check if token is present | |
| if not token or token.startswith("hf_..."): | |
| return None, "Error: Please enter a valid Hugging Face Token in the box above." | |
| client = InferenceClient(token=token) | |
| # 1. Generate Image (Visual) | |
| img_prompt = ( | |
| f"Professional food photography of {food_name}, " | |
| "8k resolution, hyperrealistic, cinematic lighting, " | |
| "macro details, steam rising, delicious, gourmet plating, " | |
| "unreal engine 5 render style, depth of field." | |
| ) | |
| generated_image = None | |
| try: | |
| print(f"Generating image for {food_name}...") | |
| generated_image = client.text_to_image( | |
| prompt=img_prompt, | |
| model="stabilityai/stable-diffusion-xl-base-1.0", | |
| negative_prompt="cartoon, drawing, anime, text, blurry, low quality", | |
| width=1024, height=1024 | |
| ) | |
| except Exception as e: | |
| print(f"Image Error: {e}") | |
| # 2. Generate Health Info (Text) in Selected Language | |
| text_prompt = ( | |
| f"Act as a nutritionist. Analyze the food item '{food_name}'. " | |
| f"Provide the response in {language} language. " | |
| "Format the response strictly with two sections:\n" | |
| "1. Health Benefits\n" | |
| "2. Potential Consequences or Cons (e.g., high calories, allergies).\n" | |
| "Keep it concise and bulleted." | |
| ) | |
| health_info = "" | |
| try: | |
| response = client.text_generation( | |
| prompt=text_prompt, | |
| model="tiiuae/falcon-7b-instruct", # Using a fast text model | |
| max_new_tokens=400, | |
| temperature=0.7 | |
| ) | |
| health_info = response | |
| except Exception as e: | |
| health_info = f"Could not retrieve health data: {e}" | |
| return generated_image, health_info | |
| # --- LOGIC 2: INTERACTIVE MAP (SCROLLABLE) --- | |
| def get_map_html(location_name="Bahawalpur"): | |
| """ | |
| Creates an interactive HTML map centered on a location. | |
| """ | |
| # Default coordinates (Bahawalpur) | |
| start_coords = [29.3544, 71.6911] | |
| # Simple coordinate lookup for demo (You can add more cities) | |
| loc_lower = location_name.lower() | |
| if "islamabad" in loc_lower: | |
| start_coords = [33.6844, 73.0479] | |
| elif "lahore" in loc_lower: | |
| start_coords = [31.5497, 74.3436] | |
| elif "karachi" in loc_lower: | |
| start_coords = [24.8607, 67.0011] | |
| elif "multan" in loc_lower: | |
| start_coords = [30.1575, 71.5249] | |
| # Create Map | |
| m = folium.Map(location=start_coords, zoom_start=13) | |
| # Add Marker | |
| folium.Marker( | |
| start_coords, | |
| popup=f"<i>{location_name}</i>", | |
| tooltip="Click me!" | |
| ).add_to(m) | |
| return m._repr_html_() | |
| # --- LOGIC 3: MENU SCANNING --- | |
| def scan_menu(image): | |
| if image is None: | |
| return "Please upload an image.", [] | |
| try: | |
| results = reader.readtext(image) | |
| # Filter for food-like text (longer than 3 chars, not numbers) | |
| detected_items = [res[1] for res in results if len(res[1]) > 3 and not res[1].isdigit()] | |
| status = f"β Found {len(detected_items)} items!" | |
| return status, gr.update(choices=detected_items, value=detected_items[0] if detected_items else None) | |
| except Exception as e: | |
| return f"Error scanning: {str(e)}", [] | |
| # --- UI LAYOUT --- | |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="gray")) as demo: | |
| gr.Markdown("# π₯ MenuVision AI: Health & Visual Analyzer") | |
| with gr.Row(): | |
| api_input = gr.Textbox(label="Hugging Face Token (Required)", type="password", placeholder="Paste your Access Token here") | |
| language_drop = gr.Dropdown(label="π Select Language", choices=["English", "Urdu", "French", "Spanish", "Arabic"], value="English") | |
| with gr.Tabs(): | |
| # --- TAB 1: SCAN & HEALTH ANALYSIS --- | |
| with gr.TabItem("πΈ Scan & Analyze"): | |
| with gr.Row(): | |
| # LEFT COLUMN: INPUT | |
| with gr.Column(scale=1): | |
| menu_input = gr.Image(type="numpy", label="1. Upload Menu Photo") | |
| scan_btn = gr.Button("π Scan Text", variant="secondary") | |
| gr.Markdown("---") | |
| status_output = gr.Textbox(label="Status", interactive=False) | |
| food_dropdown = gr.Dropdown(label="2. Select Detected Food", choices=[]) | |
| analyze_btn = gr.Button("β¨ Analyze Health & Generate Image", variant="primary") | |
| # RIGHT COLUMN: OUTPUT | |
| with gr.Column(scale=2): | |
| # 1. Real World Image | |
| result_image = gr.Image(label="Real-World Representation", type="pil", height=400) | |
| # 2. Health Columns | |
| gr.Markdown("### π©Ί Nutritional Analysis") | |
| health_output = gr.Textbox(label="Benefits & Consequences", lines=10) | |
| # --- TAB 2: INTERACTIVE MAPS --- | |
| with gr.TabItem("πΊοΈ Interactive Map"): | |
| with gr.Row(): | |
| place_search = gr.Textbox(label="Search Location (e.g., Islamabad)", placeholder="Type a city...") | |
| map_btn = gr.Button("Update Map") | |
| # This HTML component holds the interactive scrollable map | |
| map_html = gr.HTML(value=get_map_html(), label="Scrollable Map") | |
| # --- TAB 3: ABOUT ME --- | |
| with gr.TabItem("π¨βπ» About Developer"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| # FIXED LINE: Removed 'show_download_button' to fix your error | |
| gr.Image( | |
| value="https://cdn-icons-png.flaticon.com/512/4140/4140048.png", | |
| width=200, | |
| show_label=False, | |
| interactive=False | |
| ) | |
| with gr.Column(scale=3): | |
| gr.Markdown(""" | |
| ### π Hi, I'm Abdullah! | |
| **Computer Engineering Student | AI Enthusiast | Web Developer** | |
| I am currently an undergraduate student at **COMSATS University Islamabad**, specializing in Computer Engineering. | |
| I have a passion for merging **Embedded Systems** with **Generative AI** to create real-world solutions. | |
| * **Role:** Intern Web Developer at MyK Global Forwarding. | |
| * **Focus:** TinyML, IoT, and GenAI Applications. | |
| * **Location:** Bahawalpur / Islamabad. | |
| **About MenuVision AI:** | |
| This project was designed to help people make better dietary choices by visualizing food from plain text menus and understanding the health implications immediately. | |
| """) | |
| # --- EVENT HANDLERS --- | |
| # 1. Scan Button | |
| scan_btn.click( | |
| fn=scan_menu, | |
| inputs=menu_input, | |
| outputs=[status_output, food_dropdown] | |
| ) | |
| # 2. Analyze Button (Image + Health Info) | |
| analyze_btn.click( | |
| fn=analyze_food, | |
| inputs=[food_dropdown, language_drop, api_input], | |
| outputs=[result_image, health_output] | |
| ) | |
| # 3. Map Update | |
| map_btn.click( | |
| fn=get_map_html, | |
| inputs=place_search, | |
| outputs=map_html | |
| ) | |
| # Launch App | |
| demo.launch() |