File size: 3,465 Bytes
f475530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# πŸ’‘ 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.")