Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,19 @@ import gradio as gr
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 7 |
client = Groq(api_key=api_key)
|
| 8 |
|
| 9 |
def get_recommendation(quantity, food_type, expiry_hours):
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if quantity < 20:
|
| 12 |
base = "Elderly Individuals"
|
| 13 |
elif quantity < 100:
|
|
@@ -15,35 +22,45 @@ def get_recommendation(quantity, food_type, expiry_hours):
|
|
| 15 |
else:
|
| 16 |
base = "NGOs / Refugee Centers"
|
| 17 |
|
| 18 |
-
# AI explanation
|
| 19 |
prompt = f"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 38 |
|
| 39 |
-
|
| 40 |
# Gradio UI
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=get_recommendation,
|
| 43 |
inputs=[
|
| 44 |
-
gr.Number(label="Number of Meals"),
|
| 45 |
gr.Dropdown(["halal", "soft", "packaged"], label="Food Type"),
|
| 46 |
-
gr.Number(label="Hours Until Expiry")
|
| 47 |
],
|
| 48 |
outputs="text",
|
| 49 |
title="🍽️ NourishNet AI",
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Get Groq API key from environment
|
| 6 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Initialize Groq client
|
| 10 |
client = Groq(api_key=api_key)
|
| 11 |
|
| 12 |
def get_recommendation(quantity, food_type, expiry_hours):
|
| 13 |
+
"""
|
| 14 |
+
Returns a recommended recipient for surplus food along with AI explanation.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
# Basic routing logic
|
| 18 |
if quantity < 20:
|
| 19 |
base = "Elderly Individuals"
|
| 20 |
elif quantity < 100:
|
|
|
|
| 22 |
else:
|
| 23 |
base = "NGOs / Refugee Centers"
|
| 24 |
|
| 25 |
+
# Prompt for AI explanation
|
| 26 |
prompt = f"""
|
| 27 |
+
A restaurant has:
|
| 28 |
+
- {quantity} meals
|
| 29 |
+
- Food type: {food_type}
|
| 30 |
+
- Expiry in {expiry_hours} hours
|
| 31 |
|
| 32 |
+
The system selected: {base}
|
| 33 |
|
| 34 |
+
Explain why this is the best recipient in 2-3 lines.
|
| 35 |
+
Output format:
|
| 36 |
+
Recommended recipient: <recipient>
|
| 37 |
+
Reason: <AI explanation>
|
| 38 |
+
"""
|
| 39 |
|
| 40 |
+
# Call Groq AI with retry logic
|
| 41 |
+
try:
|
| 42 |
+
response = client.chat.completions.create(
|
| 43 |
+
model="llama-3.3-70b-versatile",
|
| 44 |
+
messages=[{"role": "user", "content": prompt}]
|
| 45 |
+
)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"❌ AI request failed:\n{str(e)}"
|
| 48 |
|
| 49 |
+
# Safely extract AI explanation
|
| 50 |
+
try:
|
| 51 |
+
explanation = response.choices[0].message.content
|
| 52 |
+
except (AttributeError, IndexError):
|
| 53 |
+
explanation = "❌ No AI explanation returned."
|
| 54 |
|
| 55 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 56 |
|
|
|
|
| 57 |
# Gradio UI
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=get_recommendation,
|
| 60 |
inputs=[
|
| 61 |
+
gr.Number(label="Number of Meals", value=10),
|
| 62 |
gr.Dropdown(["halal", "soft", "packaged"], label="Food Type"),
|
| 63 |
+
gr.Number(label="Hours Until Expiry", value=5)
|
| 64 |
],
|
| 65 |
outputs="text",
|
| 66 |
title="🍽️ NourishNet AI",
|