File size: 8,150 Bytes
caf00f4
 
 
 
0e693fb
0aa3216
 
0e693fb
0aa3216
882f4fa
0aa3216
0e693fb
0aa3216
caf00f4
0e693fb
0aa3216
 
b435483
0aa3216
b435483
0aa3216
 
 
 
882f4fa
 
 
 
 
0aa3216
 
 
 
 
 
 
 
 
 
882f4fa
caf00f4
0aa3216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
882f4fa
0aa3216
 
 
 
 
 
 
 
caf00f4
0aa3216
caf00f4
0aa3216
 
 
 
 
 
 
 
 
 
882f4fa
 
 
0aa3216
882f4fa
0aa3216
882f4fa
 
 
 
0aa3216
 
 
 
 
 
 
 
 
 
 
 
 
 
caf00f4
 
 
b435483
882f4fa
 
 
 
 
 
 
 
 
0e693fb
0aa3216
 
b435483
882f4fa
caf00f4
0aa3216
882f4fa
0aa3216
caf00f4
 
0e693fb
0aa3216
 
caf00f4
0aa3216
 
 
 
 
 
 
 
 
caf00f4
0aa3216
 
 
 
 
 
 
 
 
 
 
caf00f4
0aa3216
 
caf00f4
0aa3216
 
caf00f4
0aa3216
 
 
 
882f4fa
 
 
 
 
 
 
0aa3216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caf00f4
 
 
0aa3216
caf00f4
 
 
 
 
 
0aa3216
 
 
 
 
caf00f4
 
0aa3216
 
 
 
 
caf00f4
0e693fb
0aa3216
caf00f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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()