File size: 2,799 Bytes
a9f30f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e4742f
a9f30f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- 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()