Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,91 +2,109 @@ import gradio as gr
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
# Initialize Groq client
|
| 8 |
-
# -------------------------
|
| 9 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
if quantity < 20:
|
| 21 |
-
|
| 22 |
elif quantity < 100:
|
| 23 |
-
|
| 24 |
else:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
prompt = f"""
|
| 28 |
A restaurant has:
|
| 29 |
- {quantity} meals
|
| 30 |
- Food type: {food_type}
|
| 31 |
- Expiry in {expiry_hours} hours
|
| 32 |
-
|
| 33 |
-
The system selected: {base}
|
| 34 |
-
|
| 35 |
Explain why this is the best recipient in 2-3 lines.
|
| 36 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
explanation = response.choices[0].message.content
|
| 47 |
-
break
|
| 48 |
-
except Exception as e:
|
| 49 |
-
explanation = f"β AI request failed on attempt {attempt+1}: {e}"
|
| 50 |
-
time.sleep(2) # wait 2 seconds before retrying
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# π½οΈ NourishNet AI
|
| 61 |
-
AI-powered food surplus redistribution system
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
gr.HTML("""
|
| 67 |
<style>
|
| 68 |
-
body { font-family: 'Poppins', sans-serif; }
|
| 69 |
.gr-button { background: linear-gradient(90deg, #FF7A00, #FFB347); border-radius: 12px; color: white; font-weight: bold; }
|
| 70 |
.gr-textbox { border: 2px solid #FF7A00; border-radius: 10px; padding: 10px; background-color: #FFF8F0; }
|
| 71 |
</style>
|
| 72 |
""")
|
| 73 |
-
|
| 74 |
with gr.Row():
|
| 75 |
quantity_input = gr.Number(label="Number of Meals", value=10)
|
| 76 |
food_type_input = gr.Dropdown(["halal", "soft", "packaged"], label="Food Type")
|
| 77 |
expiry_input = gr.Number(label="Hours Until Expiry", value=5)
|
| 78 |
-
|
| 79 |
-
output_text = gr.Textbox(label="Recommendation & AI Reasoning", lines=7)
|
| 80 |
-
|
| 81 |
submit_btn = gr.Button("Get Recommendation")
|
| 82 |
submit_btn.click(
|
| 83 |
-
fn=
|
| 84 |
inputs=[quantity_input, food_type_input, expiry_input],
|
| 85 |
outputs=output_text
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# -------------------------
|
| 89 |
-
# Run the app
|
| 90 |
-
# -------------------------
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
+
import folium
|
| 6 |
+
from folium import Marker
|
| 7 |
+
from streamlit_folium import st_folium # optional if you switch to Streamlit for maps
|
| 8 |
+
from geopy.distance import geodesic
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
import base64
|
| 11 |
|
| 12 |
+
# Load Groq API key from environment
|
|
|
|
|
|
|
| 13 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 14 |
+
if not api_key:
|
| 15 |
+
print("β GROQ_API_KEY not found. AI reasoning will be disabled.")
|
| 16 |
+
client = None
|
| 17 |
+
else:
|
| 18 |
+
client = Groq(api_key=api_key)
|
| 19 |
|
| 20 |
+
# Sample recipients database
|
| 21 |
+
recipients = [
|
| 22 |
+
{"name": "Al Noor Elderly Home", "lat": 25.2048, "lon": 55.2708, "type": "Elderly Individuals", "contact": "+971501234567"},
|
| 23 |
+
{"name": "Refugee Center Dubai", "lat": 25.0657, "lon": 55.1713, "type": "NGOs / Refugee Centers", "contact": "+971509876543"},
|
| 24 |
+
{"name": "Care Home Sharjah", "lat": 25.3574, "lon": 55.4031, "type": "Care Homes", "contact": "+971506543210"},
|
| 25 |
+
]
|
| 26 |
|
| 27 |
+
# Function to select recipient
|
| 28 |
+
def select_recipient(quantity, food_type):
|
| 29 |
if quantity < 20:
|
| 30 |
+
type_needed = "Elderly Individuals"
|
| 31 |
elif quantity < 100:
|
| 32 |
+
type_needed = "Care Homes"
|
| 33 |
else:
|
| 34 |
+
type_needed = "NGOs / Refugee Centers"
|
| 35 |
+
# pick nearest recipient of that type (simulated)
|
| 36 |
+
filtered = [r for r in recipients if r["type"] == type_needed]
|
| 37 |
+
return filtered[0] if filtered else {"name": "No recipient available", "lat": 25.2048, "lon": 55.2708, "contact": "N/A", "type": type_needed}
|
| 38 |
|
| 39 |
+
# Function to generate AI reasoning
|
| 40 |
+
def get_ai_reasoning(quantity, food_type, expiry_hours, recipient_name, recipient_type):
|
| 41 |
+
if not client:
|
| 42 |
+
return "β AI explanation not available (API key missing)"
|
| 43 |
prompt = f"""
|
| 44 |
A restaurant has:
|
| 45 |
- {quantity} meals
|
| 46 |
- Food type: {food_type}
|
| 47 |
- Expiry in {expiry_hours} hours
|
| 48 |
+
The system selected: {recipient_name} ({recipient_type})
|
|
|
|
|
|
|
| 49 |
Explain why this is the best recipient in 2-3 lines.
|
| 50 |
"""
|
| 51 |
+
try:
|
| 52 |
+
response = client.chat.completions.create(
|
| 53 |
+
model="llama-3.3-70b-versatile",
|
| 54 |
+
messages=[{"role": "user", "content": prompt}]
|
| 55 |
+
)
|
| 56 |
+
return response.choices[0].message.content
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"β AI request failed: {str(e)}"
|
| 59 |
|
| 60 |
+
# Function to generate map
|
| 61 |
+
def generate_map(recipient):
|
| 62 |
+
m = folium.Map(location=[recipient["lat"], recipient["lon"]], zoom_start=12)
|
| 63 |
+
Marker([recipient["lat"], recipient["lon"]], popup=f"{recipient['name']} ({recipient['type']})\nContact: {recipient['contact']}").add_to(m)
|
| 64 |
+
# Convert map to HTML iframe
|
| 65 |
+
data = BytesIO()
|
| 66 |
+
m.save(data, close_file=False)
|
| 67 |
+
return data.getvalue().decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Function to simulate delivery progress
|
| 70 |
+
def simulate_delivery(quantity, food_type, expiry_hours):
|
| 71 |
+
recipient = select_recipient(quantity, food_type)
|
| 72 |
+
ai_reasoning = get_ai_reasoning(quantity, food_type, expiry_hours, recipient["name"], recipient["type"])
|
| 73 |
+
map_html = generate_map(recipient)
|
| 74 |
|
| 75 |
+
# Simulate progress steps
|
| 76 |
+
steps = ["π¦ Picked Up", "π En Route", "β
Delivered"]
|
| 77 |
+
progress_output = ""
|
| 78 |
+
for i, step in enumerate(steps):
|
| 79 |
+
progress_output += f"{step}\n"
|
| 80 |
+
time.sleep(0.5) # simulate real-time delay
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# Simulated gamification badge
|
| 83 |
+
badge = "π Zero Waste Champion" if quantity > 50 else "β Community Helper"
|
| 84 |
+
|
| 85 |
+
return f"β
Recommended: {recipient['name']} ({recipient['type']})\n\nπ€ AI Reasoning:\n{ai_reasoning}\n\nπ Recipient Map:\n{map_html}\n\nπ Delivery Status:\n{progress_output}\n\nποΈ Badge Earned: {badge}"
|
| 86 |
+
|
| 87 |
+
# Gradio UI with CSS
|
| 88 |
+
with gr.Blocks(title="π½οΈ NourishNet AI") as demo:
|
| 89 |
gr.HTML("""
|
| 90 |
<style>
|
| 91 |
+
body { font-family: 'Poppins', sans-serif; background-color: #fdf6f0; }
|
| 92 |
.gr-button { background: linear-gradient(90deg, #FF7A00, #FFB347); border-radius: 12px; color: white; font-weight: bold; }
|
| 93 |
.gr-textbox { border: 2px solid #FF7A00; border-radius: 10px; padding: 10px; background-color: #FFF8F0; }
|
| 94 |
</style>
|
| 95 |
""")
|
| 96 |
+
gr.Markdown("# π½οΈ NourishNet AI\nAI-powered food surplus redistribution system")
|
| 97 |
with gr.Row():
|
| 98 |
quantity_input = gr.Number(label="Number of Meals", value=10)
|
| 99 |
food_type_input = gr.Dropdown(["halal", "soft", "packaged"], label="Food Type")
|
| 100 |
expiry_input = gr.Number(label="Hours Until Expiry", value=5)
|
| 101 |
+
output_text = gr.Textbox(label="Recommendation, AI Reasoning, Map & Delivery Status", lines=20)
|
|
|
|
|
|
|
| 102 |
submit_btn = gr.Button("Get Recommendation")
|
| 103 |
submit_btn.click(
|
| 104 |
+
fn=simulate_delivery,
|
| 105 |
inputs=[quantity_input, food_type_input, expiry_input],
|
| 106 |
outputs=output_text
|
| 107 |
)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
if __name__ == "__main__":
|
| 110 |
demo.launch()
|