Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,20 @@
|
|
| 1 |
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 |
-
# 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 |
-
print("DEBUG: API key loaded?", os.environ.get("GROQ_API_KEY") is not None)
|
| 13 |
if not client:
|
| 14 |
-
return "❌ GROQ_API_KEY not found. Please set your API key in
|
| 15 |
|
| 16 |
# Basic routing logic
|
| 17 |
if quantity < 20:
|
|
@@ -32,20 +35,25 @@ The system selected: {base}
|
|
| 32 |
Explain why this is the best recipient in 2-3 lines.
|
| 33 |
"""
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 46 |
|
| 47 |
-
|
| 48 |
# Gradio Blocks UI
|
|
|
|
| 49 |
with gr.Blocks(title="🍽️ NourishNet AI") as demo:
|
| 50 |
gr.Markdown(
|
| 51 |
"""
|
|
@@ -61,7 +69,7 @@ with gr.Blocks(title="🍽️ NourishNet AI") as demo:
|
|
| 61 |
food_type_input = gr.Dropdown(["halal", "soft", "packaged"], label="Food Type")
|
| 62 |
expiry_input = gr.Number(label="Hours Until Expiry", value=5)
|
| 63 |
|
| 64 |
-
output_text = gr.Textbox(label="Recommendation & AI Reasoning")
|
| 65 |
|
| 66 |
submit_btn = gr.Button("Get Recommendation")
|
| 67 |
submit_btn.click(
|
|
@@ -70,5 +78,8 @@ with gr.Blocks(title="🍽️ NourishNet AI") as demo:
|
|
| 70 |
outputs=output_text
|
| 71 |
)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
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 |
client = Groq(api_key=api_key) if api_key else None
|
| 11 |
|
| 12 |
+
# -------------------------
|
| 13 |
+
# Main recommendation function
|
| 14 |
+
# -------------------------
|
| 15 |
def get_recommendation(quantity, food_type, expiry_hours):
|
|
|
|
| 16 |
if not client:
|
| 17 |
+
return "❌ GROQ_API_KEY not found. Please set your API key in Secrets."
|
| 18 |
|
| 19 |
# Basic routing logic
|
| 20 |
if quantity < 20:
|
|
|
|
| 35 |
Explain why this is the best recipient in 2-3 lines.
|
| 36 |
"""
|
| 37 |
|
| 38 |
+
# Retry logic for temporary connection issues
|
| 39 |
+
explanation = "❌ AI explanation not available."
|
| 40 |
+
for attempt in range(3):
|
| 41 |
+
try:
|
| 42 |
+
response = client.chat.completions.create(
|
| 43 |
+
model="llama-2-7b-chat", # smaller model for Spaces
|
| 44 |
+
messages=[{"role": "user", "content": prompt}]
|
| 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 |
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
|
| 53 |
|
| 54 |
+
# -------------------------
|
| 55 |
# Gradio Blocks UI
|
| 56 |
+
# -------------------------
|
| 57 |
with gr.Blocks(title="🍽️ NourishNet AI") as demo:
|
| 58 |
gr.Markdown(
|
| 59 |
"""
|
|
|
|
| 69 |
food_type_input = gr.Dropdown(["halal", "soft", "packaged"], label="Food Type")
|
| 70 |
expiry_input = gr.Number(label="Hours Until Expiry", value=5)
|
| 71 |
|
| 72 |
+
output_text = gr.Textbox(label="Recommendation & AI Reasoning", lines=7)
|
| 73 |
|
| 74 |
submit_btn = gr.Button("Get Recommendation")
|
| 75 |
submit_btn.click(
|
|
|
|
| 78 |
outputs=output_text
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# -------------------------
|
| 82 |
+
# Run the app
|
| 83 |
+
# -------------------------
|
| 84 |
if __name__ == "__main__":
|
| 85 |
demo.launch()
|