Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +81 -0
- fitness.py +71 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π‘ Run this file with: streamlit run app.py
|
| 2 |
+
#Install the required dependencies using the following command
|
| 3 |
+
#pip install streamlit replicate duckduckgo-search
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import replicate
|
| 8 |
+
from duckduckgo_search import DDGS
|
| 9 |
+
|
| 10 |
+
# ------------------- π Setup Replicate -------------------
|
| 11 |
+
|
| 12 |
+
REPLICATE_API_TOKEN = "r8_0yAsouiplq8fxaxqVrqJ69tEF3CdgRD37L2Eq" # <-- Replace this with your token
|
| 13 |
+
client = replicate.Client(api_token=REPLICATE_API_TOKEN)
|
| 14 |
+
GRANITE_MODEL_ID = "ibm-granite/granite-3.3-8b-instruct"
|
| 15 |
+
|
| 16 |
+
# ------------------- π Optional Web Search -------------------
|
| 17 |
+
|
| 18 |
+
def search_duckduckgo(query):
|
| 19 |
+
with DDGS() as ddgs:
|
| 20 |
+
results = ddgs.text(query, max_results=3)
|
| 21 |
+
return "\n".join([f"- {r['title']}: {r['href']}" for r in results])
|
| 22 |
+
|
| 23 |
+
# ------------------- π§ LLM Query Function -------------------
|
| 24 |
+
|
| 25 |
+
def query_replicate(prompt):
|
| 26 |
+
try:
|
| 27 |
+
output = client.run(GRANITE_MODEL_ID, input={"prompt": prompt, "max_new_tokens": 300})
|
| 28 |
+
return "".join(output)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"β Error from Granite: {str(e)}"
|
| 31 |
+
|
| 32 |
+
# ------------------- π€ Agent Functions -------------------
|
| 33 |
+
|
| 34 |
+
def workout_agent(prompt):
|
| 35 |
+
system_prompt = "You are a personal workout coach. Recommend home-based fitness routines based on user goals and preferences."
|
| 36 |
+
return query_replicate(f"{system_prompt}\n\nUser: {prompt}")
|
| 37 |
+
|
| 38 |
+
def nutrition_agent(prompt):
|
| 39 |
+
system_prompt = "You are a friendly nutritionist. Suggest healthy, affordable meal ideas based on user input. Be simple and practical."
|
| 40 |
+
return query_replicate(f"{system_prompt}\n\nUser: {prompt}")
|
| 41 |
+
|
| 42 |
+
def motivation_agent(prompt):
|
| 43 |
+
system_prompt = "You are a motivational fitness buddy. Share tips, inspiration, and strategies to stay consistent with healthy habits."
|
| 44 |
+
return query_replicate(f"{system_prompt}\n\nUser: {prompt}")
|
| 45 |
+
|
| 46 |
+
# ------------------- π§ Smart Router -------------------
|
| 47 |
+
|
| 48 |
+
def fitness_buddy_router(user_prompt):
|
| 49 |
+
workout_keywords = ["workout", "exercise", "routine", "stretch", "home gym", "training"]
|
| 50 |
+
nutrition_keywords = ["diet", "meal", "protein", "food", "nutrition", "eat", "snack"]
|
| 51 |
+
motivation_keywords = ["motivation", "lazy", "can't", "habit", "inspiration", "routine", "mental"]
|
| 52 |
+
|
| 53 |
+
prompt_lower = user_prompt.lower()
|
| 54 |
+
|
| 55 |
+
if any(word in prompt_lower for word in workout_keywords):
|
| 56 |
+
return workout_agent(user_prompt)
|
| 57 |
+
elif any(word in prompt_lower for word in nutrition_keywords):
|
| 58 |
+
return nutrition_agent(user_prompt)
|
| 59 |
+
elif any(word in prompt_lower for word in motivation_keywords):
|
| 60 |
+
return motivation_agent(user_prompt)
|
| 61 |
+
else:
|
| 62 |
+
return "π€ Please clarify: are you asking about workouts, meals, or motivation?"
|
| 63 |
+
|
| 64 |
+
# ------------------- π¨ Streamlit UI -------------------
|
| 65 |
+
|
| 66 |
+
st.set_page_config(page_title="Fitness Buddy", page_icon="πͺ")
|
| 67 |
+
st.title("ποΈ Fitness Buddy AI")
|
| 68 |
+
st.caption("Powered by IBM Granite via Replicate")
|
| 69 |
+
|
| 70 |
+
st.markdown("**Ask me about:**\n- ποΈ Home workouts\n- π₯ Nutritious meal ideas\n- π‘ Fitness motivation and habit building")
|
| 71 |
+
|
| 72 |
+
user_input = st.text_area("Ask Fitness Buddy:", height=150, placeholder="e.g., Suggest a workout for weight loss at home")
|
| 73 |
+
|
| 74 |
+
if st.button("Ask AI"):
|
| 75 |
+
if user_input.strip():
|
| 76 |
+
with st.spinner("Thinking..."):
|
| 77 |
+
reply = fitness_buddy_router(user_input)
|
| 78 |
+
st.markdown("### π€ Fitness Buddy Says:")
|
| 79 |
+
st.write(reply)
|
| 80 |
+
else:
|
| 81 |
+
st.warning("Please enter a question or message first.")
|
fitness.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Fitness.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1rF0K4Qn52EJlq0ULtyo9jFhQ8aZrgV8P
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
#pip install replicate duckduckgo-search
|
| 11 |
+
|
| 12 |
+
import replicate
|
| 13 |
+
from duckduckgo_search import DDGS
|
| 14 |
+
|
| 15 |
+
# π IBM Granite on Replicate
|
| 16 |
+
REPLICATE_API_TOKEN = "r8_0yAsouiplq8fxaxqVrqJ69tEF3CdgRD37L2Eq"
|
| 17 |
+
client = replicate.Client(api_token=REPLICATE_API_TOKEN)
|
| 18 |
+
|
| 19 |
+
# π Optional: search tool
|
| 20 |
+
def search_duckduckgo(query):
|
| 21 |
+
with DDGS() as ddgs:
|
| 22 |
+
results = ddgs.text(query, max_results=3)
|
| 23 |
+
return "\n".join([f"- {r['title']}: {r['href']}" for r in results])
|
| 24 |
+
|
| 25 |
+
# π§ LLM query function
|
| 26 |
+
def query_replicate(prompt, model="ibm-granite/granite-3.3-8b-instruct"):
|
| 27 |
+
output = client.run(model, input={"prompt": prompt, "max_new_tokens": 300})
|
| 28 |
+
return "".join(output)
|
| 29 |
+
|
| 30 |
+
# π€ Agent 1: Workout Recommender
|
| 31 |
+
def workout_agent(prompt):
|
| 32 |
+
system_prompt = "You are a personal workout coach. Recommend home-based fitness routines based on user goals and preferences."
|
| 33 |
+
return query_replicate(f"{system_prompt}\n\nUser: {prompt}")
|
| 34 |
+
|
| 35 |
+
# π Agent 2: Nutrition Coach
|
| 36 |
+
def nutrition_agent(prompt):
|
| 37 |
+
system_prompt = "You are a friendly nutritionist. Suggest healthy, affordable meal ideas based on user input. Be simple and practical."
|
| 38 |
+
return query_replicate(f"{system_prompt}\n\nUser: {prompt}")
|
| 39 |
+
|
| 40 |
+
# π‘ Agent 3: Motivational Buddy
|
| 41 |
+
def motivation_agent(prompt):
|
| 42 |
+
system_prompt = "You are a motivational fitness buddy. Share tips, inspiration, and strategies to stay consistent with healthy habits."
|
| 43 |
+
return query_replicate(f"{system_prompt}\n\nUser: {prompt}")
|
| 44 |
+
|
| 45 |
+
def fitness_buddy_router(user_prompt):
|
| 46 |
+
workout_keywords = ["workout", "exercise", "routine", "stretch", "home gym"]
|
| 47 |
+
nutrition_keywords = ["diet", "meal", "protein", "food", "nutrition", "eat"]
|
| 48 |
+
motivation_keywords = ["motivation", "lazy", "can't", "habit", "inspiration", "routine", "mental"]
|
| 49 |
+
|
| 50 |
+
prompt_lower = user_prompt.lower()
|
| 51 |
+
|
| 52 |
+
if any(word in prompt_lower for word in workout_keywords):
|
| 53 |
+
return workout_agent(user_prompt)
|
| 54 |
+
elif any(word in prompt_lower for word in nutrition_keywords):
|
| 55 |
+
return nutrition_agent(user_prompt)
|
| 56 |
+
elif any(word in prompt_lower for word in motivation_keywords):
|
| 57 |
+
return motivation_agent(user_prompt)
|
| 58 |
+
else:
|
| 59 |
+
# default fallback agent
|
| 60 |
+
return "π€ Please clarify: are you asking about workouts, meals, or motivation?"
|
| 61 |
+
|
| 62 |
+
import gradio as gr
|
| 63 |
+
|
| 64 |
+
gr.Interface(
|
| 65 |
+
fn=fitness_buddy_router,
|
| 66 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask about fitness, meals, or motivation..."),
|
| 67 |
+
outputs=gr.Textbox(label="Fitness Buddy Says"),
|
| 68 |
+
title="πͺ Fitness Buddy AI",
|
| 69 |
+
description="Your personal AI coach for workouts, nutrition, and motivation!"
|
| 70 |
+
).launch()
|
| 71 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
replicate
|
| 2 |
+
duckduckgo-search
|
| 3 |
+
gradio
|
| 4 |
+
streamlit
|
| 5 |
+
requests
|