Spaces:
Runtime error
Runtime error
| # π‘ Run this file with: streamlit run app.py | |
| #Install the required dependencies using the following command | |
| #pip install streamlit replicate duckduckgo-search | |
| import streamlit as st | |
| import replicate | |
| from duckduckgo_search import DDGS | |
| # ------------------- π Setup Replicate ------------------- | |
| REPLICATE_API_TOKEN = "r8_fN4twLwm1FOIgMwxX0IpFmpVtUbPxEV3f1hNd" # <-- Replace this with your token | |
| client = replicate.Client(api_token=REPLICATE_API_TOKEN) | |
| GRANITE_MODEL_ID = "ibm-granite/granite-3.3-8b-instruct" | |
| # ------------------- π Optional Web Search ------------------- | |
| def search_duckduckgo(query): | |
| with DDGS() as ddgs: | |
| results = ddgs.text(query, max_results=3) | |
| return "\n".join([f"- {r['title']}: {r['href']}" for r in results]) | |
| # ------------------- π§ LLM Query Function ------------------- | |
| def query_replicate(prompt): | |
| try: | |
| output = client.run(GRANITE_MODEL_ID, input={"prompt": prompt, "max_new_tokens": 300}) | |
| return "".join(output) | |
| except Exception as e: | |
| return f"β Error from Granite: {str(e)}" | |
| # ------------------- π€ Agent Functions ------------------- | |
| def workout_agent(prompt): | |
| system_prompt = "You are a personal workout coach. Recommend home-based fitness routines based on user goals and preferences." | |
| return query_replicate(f"{system_prompt}\n\nUser: {prompt}") | |
| def nutrition_agent(prompt): | |
| system_prompt = "You are a friendly nutritionist. Suggest healthy, affordable meal ideas based on user input. Be simple and practical." | |
| return query_replicate(f"{system_prompt}\n\nUser: {prompt}") | |
| def motivation_agent(prompt): | |
| system_prompt = "You are a motivational fitness buddy. Share tips, inspiration, and strategies to stay consistent with healthy habits." | |
| return query_replicate(f"{system_prompt}\n\nUser: {prompt}") | |
| # ------------------- π§ Smart Router ------------------- | |
| def fitness_buddy_router(user_prompt): | |
| workout_keywords = ["workout", "exercise", "routine", "stretch", "home gym", "training"] | |
| nutrition_keywords = ["diet", "meal", "protein", "food", "nutrition", "eat", "snack"] | |
| motivation_keywords = ["motivation", "lazy", "can't", "habit", "inspiration", "routine", "mental"] | |
| prompt_lower = user_prompt.lower() | |
| if any(word in prompt_lower for word in workout_keywords): | |
| return workout_agent(user_prompt) | |
| elif any(word in prompt_lower for word in nutrition_keywords): | |
| return nutrition_agent(user_prompt) | |
| elif any(word in prompt_lower for word in motivation_keywords): | |
| return motivation_agent(user_prompt) | |
| else: | |
| return "π€ Please clarify: are you asking about workouts, meals, or motivation?" | |
| # ------------------- π¨ Streamlit UI ------------------- | |
| st.set_page_config(page_title="Fitness Buddy", page_icon="πͺ") | |
| st.title("ποΈ Fitness Buddy AI") | |
| st.caption("Powered by IBM Granite via Replicate") | |
| st.markdown("**Ask me about:**\n- ποΈ Home workouts\n- π₯ Nutritious meal ideas\n- π‘ Fitness motivation and habit building") | |
| user_input = st.text_area("Ask Fitness Buddy:", height=150, placeholder="e.g., Suggest a workout for weight loss at home") | |
| if st.button("Ask AI"): | |
| if user_input.strip(): | |
| with st.spinner("Thinking..."): | |
| reply = fitness_buddy_router(user_input) | |
| st.markdown("### π€ Fitness Buddy Says:") | |
| st.write(reply) | |
| else: | |
| st.warning("Please enter a question or message first.") | |