meesamraza's picture
Update app.py
9808ce5 verified
import streamlit as st
import os
from groq import Groq
from datetime import datetime
st.set_page_config(page_title="Health & Diet Chat", page_icon="πŸ₯—")
# Get API key from environment variable ONLY - no st.secrets!
@st.cache_resource
def get_client():
api_key = os.getenv("GROQ_API_KEY")
if api_key:
return Groq(api_key=api_key)
return None
client = get_client()
# Check if API key exists
if not client:
st.error("⚠️ GROQ_API_KEY not found in environment variables!")
st.info("πŸ’‘ Set it with: export GROQ_API_KEY='your-key-here'")
st.stop()
# Simple chat function
def get_response(prompt):
try:
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "system", "content": "You are a helpful health and diet advisor. Give short, practical advice."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=500
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# UI
st.title("πŸ₯— Health & Diet Advisor")
st.write("Ask anything about health, diet, or nutrition")
# Input
user_input = st.text_area("Your question:", height=100,
placeholder="e.g., What should I eat for breakfast? OR How to lose weight?")
col1, col2 = st.columns(2)
with col1:
if st.button("πŸ’¬ Ask", type="primary", use_container_width=True):
if user_input:
with st.spinner("Thinking..."):
response = get_response(user_input)
# Show response
st.subheader("πŸ“‹ Answer:")
st.write(response)
# Save to session for download
st.session_state['last_response'] = response
st.session_state['last_question'] = user_input
else:
st.warning("Please enter a question")
with col2:
if 'last_response' in st.session_state:
# Create text file
text_content = f"""HEALTH & DIET ADVICE
===========================
Date: {datetime.now().strftime('%Y-%m-%d %H:%M')}
Q: {st.session_state['last_question']}
A: {st.session_state['last_response']}
===========================
"""
st.download_button(
"πŸ“₯ Download Advice",
data=text_content,
file_name=f"health_advice_{datetime.now().strftime('%Y%m%d_%H%M')}.txt",
mime="text/plain",
use_container_width=True
)
# Quick examples
st.divider()
st.caption("Try asking about:")
st.caption("🍎 Healthy breakfast ideas β€’ πŸ’ͺ Weight loss tips β€’ πŸ’§ Water intake β€’ πŸ₯— Meal planning")