Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line23456789101112131 unmodified line15161718192021222324252627282930313233343536373839404142434445464748491 unmodified linefrom groq import Groqimport osapi_key = os.environ.get("GROQ_API_KEY")client = Groq(api_key=api_key)def get_recommendation(quantity, food_type, expiry_hours):# Basic logic (fast + reliable)if quantity < 20:base = "Elderly Individuals"elif quantity < 100:1 unmodified lineelse:base = "NGOs / Refugee Centers"# AI explanationprompt = f"""A restaurant has:- {quantity} meals- Food type: {food_type}- Expiry in {expiry_hours} hoursThe system selected: {base}Explain why this is the best recipient in 2-3 lines."""response = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=[{"role": "user", "content": prompt}])explanation = response.choices[0].message.contentreturn f"✅ Recommended: {base}\n\n🤖 AI Reasoning:\n{explanation}"# Gradio UIdemo = gr.Interface(fn=get_recommendation,inputs=[gr.Number(label="Number of Meals"),gr.Dropdown(["halal", "soft", "packaged"], label="Food Type"),gr.Number(label="Hours Until Expiry")],outputs="text",title="🍽️ NourishNet AI",1 unmodified line2345678910111213141516171819201 unmodified line2223242526272829303132333435363738394041424344454647484950515253545556575859606162636465661 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):"""Returns a recommended recipient for surplus food along with AI explanation."""# Basic routing logicif quantity < 20:base = "Elderly Individuals"elif quantity < 100:1 unmodified lineelse:base = "NGOs / Refugee Centers"# Prompt for AI explanationprompt = f"""A restaurant has:- {quantity} meals- Food type: {food_type}- Expiry in {expiry_hours} hoursThe system selected: {base}Explain why this is the best recipient in 2-3 lines.Output format:Recommended recipient: <recipient>Reason: <AI explanation>"""# Call Groq AI with retry logictry: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 explanationtry:explanation = response.choices[0].message.contentexcept (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", value=10),gr.Dropdown(["halal", "soft", "packaged"], label="Food Type"),gr.Number(label="Hours Until Expiry", value=5)],outputs="text",title="🍽️ NourishNet AI",
app.py+35-18