Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import folium
|
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
# --- CONFIGURATION ---
|
| 10 |
-
# Replace with your actual token
|
| 11 |
HF_TOKEN = "hf_..."
|
| 12 |
|
| 13 |
# Initialize Tools
|
|
@@ -22,6 +22,11 @@ def analyze_food(food_name, language, api_key):
|
|
| 22 |
return None, "Please select a food item.", ""
|
| 23 |
|
| 24 |
token = api_key if api_key else HF_TOKEN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
client = InferenceClient(token=token)
|
| 26 |
|
| 27 |
# 1. Generate Image (Visual)
|
|
@@ -32,6 +37,7 @@ def analyze_food(food_name, language, api_key):
|
|
| 32 |
"unreal engine 5 render style, depth of field."
|
| 33 |
)
|
| 34 |
|
|
|
|
| 35 |
try:
|
| 36 |
print(f"Generating image for {food_name}...")
|
| 37 |
generated_image = client.text_to_image(
|
|
@@ -42,7 +48,6 @@ def analyze_food(food_name, language, api_key):
|
|
| 42 |
)
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Image Error: {e}")
|
| 45 |
-
generated_image = None
|
| 46 |
|
| 47 |
# 2. Generate Health Info (Text) in Selected Language
|
| 48 |
text_prompt = (
|
|
@@ -54,6 +59,7 @@ def analyze_food(food_name, language, api_key):
|
|
| 54 |
"Keep it concise and bulleted."
|
| 55 |
)
|
| 56 |
|
|
|
|
| 57 |
try:
|
| 58 |
response = client.text_generation(
|
| 59 |
prompt=text_prompt,
|
|
@@ -75,11 +81,16 @@ def get_map_html(location_name="Bahawalpur"):
|
|
| 75 |
# Default coordinates (Bahawalpur)
|
| 76 |
start_coords = [29.3544, 71.6911]
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
|
|
|
| 80 |
start_coords = [33.6844, 73.0479]
|
| 81 |
-
elif
|
| 82 |
start_coords = [31.5497, 74.3436]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Create Map
|
| 85 |
m = folium.Map(location=start_coords, zoom_start=13)
|
|
@@ -98,20 +109,23 @@ def scan_menu(image):
|
|
| 98 |
if image is None:
|
| 99 |
return "Please upload an image.", []
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# --- UI LAYOUT ---
|
| 109 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="gray")) as demo:
|
| 110 |
|
| 111 |
-
gr.Markdown("# π₯ MenuVision AI: Health &
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
-
api_input = gr.Textbox(label="Hugging Face Token (
|
| 115 |
language_drop = gr.Dropdown(label="π Select Language", choices=["English", "Urdu", "French", "Spanish", "Arabic"], value="English")
|
| 116 |
|
| 117 |
with gr.Tabs():
|
|
@@ -151,8 +165,13 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="gray"))
|
|
| 151 |
with gr.TabItem("π¨βπ» About Developer"):
|
| 152 |
with gr.Row():
|
| 153 |
with gr.Column(scale=1):
|
| 154 |
-
#
|
| 155 |
-
gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
with gr.Column(scale=3):
|
| 158 |
gr.Markdown("""
|
|
|
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
# --- CONFIGURATION ---
|
| 10 |
+
# Replace with your actual token if you don't want to paste it in the UI every time
|
| 11 |
HF_TOKEN = "hf_..."
|
| 12 |
|
| 13 |
# Initialize Tools
|
|
|
|
| 22 |
return None, "Please select a food item.", ""
|
| 23 |
|
| 24 |
token = api_key if api_key else HF_TOKEN
|
| 25 |
+
|
| 26 |
+
# Check if token is present
|
| 27 |
+
if not token or token.startswith("hf_..."):
|
| 28 |
+
return None, "Error: Please enter a valid Hugging Face Token in the box above."
|
| 29 |
+
|
| 30 |
client = InferenceClient(token=token)
|
| 31 |
|
| 32 |
# 1. Generate Image (Visual)
|
|
|
|
| 37 |
"unreal engine 5 render style, depth of field."
|
| 38 |
)
|
| 39 |
|
| 40 |
+
generated_image = None
|
| 41 |
try:
|
| 42 |
print(f"Generating image for {food_name}...")
|
| 43 |
generated_image = client.text_to_image(
|
|
|
|
| 48 |
)
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Image Error: {e}")
|
|
|
|
| 51 |
|
| 52 |
# 2. Generate Health Info (Text) in Selected Language
|
| 53 |
text_prompt = (
|
|
|
|
| 59 |
"Keep it concise and bulleted."
|
| 60 |
)
|
| 61 |
|
| 62 |
+
health_info = ""
|
| 63 |
try:
|
| 64 |
response = client.text_generation(
|
| 65 |
prompt=text_prompt,
|
|
|
|
| 81 |
# Default coordinates (Bahawalpur)
|
| 82 |
start_coords = [29.3544, 71.6911]
|
| 83 |
|
| 84 |
+
# Simple coordinate lookup for demo (You can add more cities)
|
| 85 |
+
loc_lower = location_name.lower()
|
| 86 |
+
if "islamabad" in loc_lower:
|
| 87 |
start_coords = [33.6844, 73.0479]
|
| 88 |
+
elif "lahore" in loc_lower:
|
| 89 |
start_coords = [31.5497, 74.3436]
|
| 90 |
+
elif "karachi" in loc_lower:
|
| 91 |
+
start_coords = [24.8607, 67.0011]
|
| 92 |
+
elif "multan" in loc_lower:
|
| 93 |
+
start_coords = [30.1575, 71.5249]
|
| 94 |
|
| 95 |
# Create Map
|
| 96 |
m = folium.Map(location=start_coords, zoom_start=13)
|
|
|
|
| 109 |
if image is None:
|
| 110 |
return "Please upload an image.", []
|
| 111 |
|
| 112 |
+
try:
|
| 113 |
+
results = reader.readtext(image)
|
| 114 |
+
# Filter for food-like text (longer than 3 chars, not numbers)
|
| 115 |
+
detected_items = [res[1] for res in results if len(res[1]) > 3 and not res[1].isdigit()]
|
| 116 |
+
|
| 117 |
+
status = f"β
Found {len(detected_items)} items!"
|
| 118 |
+
return status, gr.update(choices=detected_items, value=detected_items[0] if detected_items else None)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return f"Error scanning: {str(e)}", []
|
| 121 |
|
| 122 |
# --- UI LAYOUT ---
|
| 123 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="gray")) as demo:
|
| 124 |
|
| 125 |
+
gr.Markdown("# π₯ MenuVision AI: Health & Visual Analyzer")
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
+
api_input = gr.Textbox(label="Hugging Face Token (Required)", type="password", placeholder="Paste your Access Token here")
|
| 129 |
language_drop = gr.Dropdown(label="π Select Language", choices=["English", "Urdu", "French", "Spanish", "Arabic"], value="English")
|
| 130 |
|
| 131 |
with gr.Tabs():
|
|
|
|
| 165 |
with gr.TabItem("π¨βπ» About Developer"):
|
| 166 |
with gr.Row():
|
| 167 |
with gr.Column(scale=1):
|
| 168 |
+
# FIXED LINE: Removed 'show_download_button' to fix your error
|
| 169 |
+
gr.Image(
|
| 170 |
+
value="https://cdn-icons-png.flaticon.com/512/4140/4140048.png",
|
| 171 |
+
width=200,
|
| 172 |
+
show_label=False,
|
| 173 |
+
interactive=False
|
| 174 |
+
)
|
| 175 |
|
| 176 |
with gr.Column(scale=3):
|
| 177 |
gr.Markdown("""
|