Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,13 @@ import gradio as gr
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 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,7 +16,6 @@ def get_recommendation(quantity, food_type, expiry_hours):
|
|
| 22 |
else:
|
| 23 |
base = "NGOs / Refugee Centers"
|
| 24 |
|
| 25 |
-
# Prompt for AI explanation
|
| 26 |
prompt = f"""
|
| 27 |
A restaurant has:
|
| 28 |
- {quantity} meals
|
|
@@ -32,40 +25,42 @@ A restaurant has:
|
|
| 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-
|
| 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
|
| 53 |
-
explanation = "❌
|
| 54 |
|
| 55 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 56 |
|
| 57 |
-
# Gradio UI
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch()
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Groq client
|
| 6 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
if not api_key:
|
| 8 |
+
raise ValueError("❌ GROQ_API_KEY not found in environment variables")
|
|
|
|
| 9 |
client = Groq(api_key=api_key)
|
| 10 |
|
| 11 |
def get_recommendation(quantity, food_type, expiry_hours):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if quantity < 20:
|
| 13 |
base = "Elderly Individuals"
|
| 14 |
elif quantity < 100:
|
|
|
|
| 16 |
else:
|
| 17 |
base = "NGOs / Refugee Centers"
|
| 18 |
|
|
|
|
| 19 |
prompt = f"""
|
| 20 |
A restaurant has:
|
| 21 |
- {quantity} meals
|
|
|
|
| 25 |
The system selected: {base}
|
| 26 |
|
| 27 |
Explain why this is the best recipient in 2-3 lines.
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
response = client.chat.completions.create(
|
| 31 |
+
model="llama-2-7b-chat",
|
| 32 |
messages=[{"role": "user", "content": prompt}]
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
explanation = response.choices[0].message.content
|
| 35 |
+
except:
|
| 36 |
+
explanation = "❌ AI explanation not available."
|
| 37 |
|
| 38 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 39 |
|
| 40 |
+
# Gradio Blocks UI
|
| 41 |
+
with gr.Blocks(title="🍽️ NourishNet AI") as demo:
|
| 42 |
+
gr.Markdown(
|
| 43 |
+
"""
|
| 44 |
+
# 🍽️ NourishNet AI
|
| 45 |
+
AI-powered food surplus redistribution system
|
| 46 |
+
|
| 47 |
+
Enter meal count, type, and expiry hours, and get a recommended recipient with AI reasoning.
|
| 48 |
+
"""
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
quantity_input = gr.Number(label="Number of Meals", value=10)
|
| 53 |
+
food_type_input = gr.Dropdown(["halal", "soft", "packaged"], label="Food Type")
|
| 54 |
+
expiry_input = gr.Number(label="Hours Until Expiry", value=5)
|
| 55 |
+
|
| 56 |
+
output_text = gr.Textbox(label="Recommendation & AI Reasoning")
|
| 57 |
+
|
| 58 |
+
submit_btn = gr.Button("Get Recommendation")
|
| 59 |
+
submit_btn.click(
|
| 60 |
+
fn=get_recommendation,
|
| 61 |
+
inputs=[quantity_input, food_type_input, expiry_input],
|
| 62 |
+
outputs=output_text
|
| 63 |
+
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|