Spaces:
Sleeping
Sleeping
| 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! | |
| 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") |