Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345567891011121013141516171819201 unmodified line222324182526202122232728293031253233272834353637383940413031323342434445464748495035515253545556395758596044616246636465661 unmodified linefrom groq import Groqimport os
# Get Groq API key from environmentapi_key = os.environ.get("GROQ_API_KEY")
# Initialize Groq clientclient = Groq(api_key=api_key)
def get_recommendation(quantity, food_type, expiry_hours): # Basic logic (fast + reliable) """ Returns a recommended recipient for surplus food along with AI explanation. """
# Basic routing logic if quantity < 20: base = "Elderly Individuals" elif quantity < 100:1 unmodified line else: base = "NGOs / Refugee Centers"
# AI explanation # Prompt for AI explanation prompt = f""" A restaurant has: - {quantity} meals - Food type: {food_type} - Expiry in {expiry_hours} hoursA restaurant has:- {quantity} meals- Food type: {food_type}- Expiry in {expiry_hours} hours
The system selected: {base}The system selected: {base}
Explain why this is the best recipient in 2-3 lines. """Explain why this is the best recipient in 2-3 lines.Output format:Recommended recipient: <recipient>Reason: <AI explanation>"""
# Call Groq AI with retry logic try: response = client.chat.completions.create( model="llama-3.3-70b-versatile", messages=[{"role": "user", "content": prompt}] ) response = client.chat.completions.create( model="llama-3.3-70b-versatile", messages=[{"role": "user", "content": prompt}] ) except Exception as e: return f"❌ AI request failed:\n{str(e)}"
# Safely extract AI explanation try: explanation = response.choices[0].message.content explanation = response.choices[0].message.content except (AttributeError, IndexError): explanation = "❌ No AI explanation returned."
return f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"
# Gradio UIdemo = gr.Interface( fn=get_recommendation, inputs=[ gr.Number(label="Number of Meals"), gr.Number(label="Number of Meals", value=10), gr.Dropdown(["halal", "soft", "packaged"], label="Food Type"), gr.Number(label="Hours Until Expiry") gr.Number(label="Hours Until Expiry", value=5) ], outputs="text", title="🍽️ NourishNet AI",app.py+35-18