Spaces:
Runtime error
Runtime error
| # -*- coding: utf-8 -*- | |
| """Fitness.ipynb | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1rF0K4Qn52EJlq0ULtyo9jFhQ8aZrgV8P | |
| """ | |
| #pip install replicate duckduckgo-search | |
| import replicate | |
| from duckduckgo_search import DDGS | |
| # π IBM Granite on Replicate | |
| REPLICATE_API_TOKEN = "r8_fN4twLwm1FOIgMwxX0IpFmpVtUbPxEV3f1hNd" | |
| client = replicate.Client(api_token=REPLICATE_API_TOKEN) | |
| # π Optional: search tool | |
| 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, model="ibm-granite/granite-3.3-8b-instruct"): | |
| output = client.run(model, input={"prompt": prompt, "max_new_tokens": 300}) | |
| return "".join(output) | |
| # π€ Agent 1: Workout Recommender | |
| 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}") | |
| # π Agent 2: Nutrition Coach | |
| 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}") | |
| # π‘ Agent 3: Motivational Buddy | |
| 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}") | |
| def fitness_buddy_router(user_prompt): | |
| workout_keywords = ["workout", "exercise", "routine", "stretch", "home gym"] | |
| nutrition_keywords = ["diet", "meal", "protein", "food", "nutrition", "eat"] | |
| 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: | |
| # default fallback agent | |
| return "π€ Please clarify: are you asking about workouts, meals, or motivation?" | |
| import gradio as gr | |
| gr.Interface( | |
| fn=fitness_buddy_router, | |
| inputs=gr.Textbox(lines=2, placeholder="Ask about fitness, meals, or motivation..."), | |
| outputs=gr.Textbox(label="Fitness Buddy Says"), | |
| title="πͺ Fitness Buddy AI", | |
| description="Your personal AI coach for workouts, nutrition, and motivation!" | |
| ).launch() | |