Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,17 @@ import gradio as gr
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Groq
|
| 6 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
def get_recommendation(quantity, food_type, expiry_hours):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if quantity < 20:
|
| 10 |
base = "Elderly Individuals"
|
| 11 |
elif quantity < 100:
|
|
@@ -23,14 +30,20 @@ The system selected: {base}
|
|
| 23 |
|
| 24 |
Explain why this is the best recipient in 2-3 lines.
|
| 25 |
"""
|
|
|
|
|
|
|
|
|
|
| 26 |
response = client.chat.completions.create(
|
| 27 |
model="llama-2-7b-chat",
|
| 28 |
messages=[{"role": "user", "content": prompt}]
|
| 29 |
)
|
| 30 |
explanation = response.choices[0].message.content
|
|
|
|
|
|
|
| 31 |
|
| 32 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 33 |
|
|
|
|
| 34 |
# Gradio Blocks UI
|
| 35 |
with gr.Blocks(title="🍽️ NourishNet AI") as demo:
|
| 36 |
gr.Markdown(
|
|
|
|
| 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 |
+
# Initialize Groq client only if key exists
|
| 9 |
+
client = Groq(api_key=api_key) if api_key else None
|
| 10 |
+
|
| 11 |
def get_recommendation(quantity, food_type, expiry_hours):
|
| 12 |
+
if not client:
|
| 13 |
+
return "❌ GROQ_API_KEY not found. Please set your API key in environment variables or Secrets."
|
| 14 |
+
|
| 15 |
+
# Basic routing logic
|
| 16 |
if quantity < 20:
|
| 17 |
base = "Elderly Individuals"
|
| 18 |
elif quantity < 100:
|
|
|
|
| 30 |
|
| 31 |
Explain why this is the best recipient in 2-3 lines.
|
| 32 |
"""
|
| 33 |
+
|
| 34 |
+
# AI call
|
| 35 |
+
try:
|
| 36 |
response = client.chat.completions.create(
|
| 37 |
model="llama-2-7b-chat",
|
| 38 |
messages=[{"role": "user", "content": prompt}]
|
| 39 |
)
|
| 40 |
explanation = response.choices[0].message.content
|
| 41 |
+
except Exception as e:
|
| 42 |
+
explanation = f"❌ AI explanation not available:\n{e}"
|
| 43 |
|
| 44 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 45 |
|
| 46 |
+
|
| 47 |
# Gradio Blocks UI
|
| 48 |
with gr.Blocks(title="🍽️ NourishNet AI") as demo:
|
| 49 |
gr.Markdown(
|