yhraje commited on
Commit
f475530
Β·
verified Β·
1 Parent(s): a9f30f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -81
app.py CHANGED
@@ -1,81 +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.")
 
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_fN4twLwm1FOIgMwxX0IpFmpVtUbPxEV3f1hNd" # <-- 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.")