Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,11 @@ import os
|
|
| 4 |
import json
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
-
# π§ Load Zephyr model
|
| 8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 9 |
|
| 10 |
-
# π§ MEMORY SETUP
|
| 11 |
-
MEMORY_FILE = "hoopmind_memory.json"
|
| 12 |
|
| 13 |
def load_memory():
|
| 14 |
if os.path.exists(MEMORY_FILE):
|
|
@@ -22,118 +22,141 @@ def save_memory(memory):
|
|
| 22 |
|
| 23 |
def update_memory(user_input):
|
| 24 |
memory = load_memory()
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
name = user_input.split("is")[-1].strip().capitalize()
|
| 27 |
memory["name"] = name
|
| 28 |
-
|
|
|
|
| 29 |
memory["injury"] = "yes"
|
| 30 |
-
|
|
|
|
| 31 |
memory.pop("injury", None)
|
|
|
|
| 32 |
save_memory(memory)
|
| 33 |
return memory
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
def alpha_chatbot_response(user_input):
|
| 37 |
try:
|
| 38 |
system_prompt = (
|
| 39 |
"You are a helpful, smart, and emotionally supportive AI assistant. "
|
| 40 |
"You respond like a friend, especially when someone is feeling down or frustrated."
|
| 41 |
)
|
|
|
|
| 42 |
prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{user_input}\n<|assistant|>"
|
|
|
|
| 43 |
response = client.text_generation(
|
| 44 |
prompt,
|
| 45 |
max_new_tokens=200,
|
| 46 |
temperature=0.7,
|
| 47 |
return_full_text=False
|
| 48 |
)
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"π Lil error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# π§ HOOPMIND CHAT FUNCTION
|
| 54 |
-
def hoopmind_chat(user_input, history=[]):
|
| 55 |
user_input_lower = user_input.lower()
|
| 56 |
memory = update_memory(user_input)
|
| 57 |
|
|
|
|
| 58 |
if any(kw in user_input_lower for kw in ["shooting", "dribbling", "defense", "injury", "ankle", "knee", "motivation"]):
|
| 59 |
if "shooting" in user_input_lower:
|
| 60 |
return random.choice([
|
| 61 |
-
"Aiming to shoot like Steph Curry? ππ₯ Focus on
|
| 62 |
-
"
|
| 63 |
-
"Daily
|
| 64 |
-
"Visualize the arc
|
| 65 |
-
"
|
| 66 |
-
|
| 67 |
])
|
|
|
|
| 68 |
elif "dribbling" in user_input_lower:
|
| 69 |
return random.choice([
|
| 70 |
-
"
|
| 71 |
-
"Two-ball dribbling
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
|
| 76 |
])
|
|
|
|
| 77 |
elif "defense" in user_input_lower:
|
| 78 |
return random.choice([
|
| 79 |
-
"Stay low, move your feet,
|
| 80 |
-
"No easy buckets if you
|
| 81 |
-
"
|
| 82 |
])
|
|
|
|
| 83 |
elif "injury" in user_input_lower or "ankle" in user_input_lower or "knee" in user_input_lower:
|
| 84 |
return random.choice([
|
| 85 |
-
"
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
])
|
|
|
|
| 89 |
elif "motivation" in user_input_lower:
|
| 90 |
return random.choice([
|
| 91 |
-
"Bro
|
| 92 |
-
"Mamba mentality dawg
|
| 93 |
-
"Every legend started where you
|
| 94 |
])
|
| 95 |
-
|
|
|
|
| 96 |
elif any(kw in user_input_lower for kw in ["football", "quarterback", "receiver", "linebacker", "tackle", "nfl"]):
|
| 97 |
return random.choice([
|
| 98 |
-
"Wanna
|
| 99 |
-
"
|
| 100 |
-
"
|
| 101 |
-
"
|
| 102 |
-
"
|
| 103 |
])
|
| 104 |
|
|
|
|
| 105 |
elif any(word in user_input_lower for word in ["hi", "hey", "yo", "sup", "what's up"]):
|
| 106 |
return random.choice([
|
| 107 |
-
"What
|
| 108 |
-
"
|
| 109 |
-
"I
|
| 110 |
])
|
| 111 |
-
|
|
|
|
| 112 |
elif any(word in user_input_lower for word in ["lol", "fr", "bro", "lmao", "funny"]):
|
| 113 |
return random.choice([
|
| 114 |
-
"
|
| 115 |
"BROO thatβs facts π",
|
| 116 |
-
"π€£ you
|
| 117 |
])
|
| 118 |
-
|
|
|
|
| 119 |
elif any(word in user_input_lower for word in ["idk", "help", "what can you do"]):
|
| 120 |
return random.choice([
|
| 121 |
-
"Iβm your hoop buddy
|
| 122 |
-
"Ask
|
| 123 |
-
"Iβm your
|
| 124 |
])
|
| 125 |
-
|
| 126 |
-
# Fallback
|
| 127 |
return alpha_chatbot_response(user_input)
|
| 128 |
|
| 129 |
-
|
|
|
|
| 130 |
chat = gr.ChatInterface(
|
| 131 |
fn=hoopmind_chat,
|
| 132 |
title="π HoopMind β Your Basketball Helper AI",
|
| 133 |
description="""
|
| 134 |
β¨ Welcome to **HoopMind** β¨
|
| 135 |
Your AI hooper brother. Talk hoops, life, school, or just chill!
|
| 136 |
-
Drop a question
|
| 137 |
""",
|
| 138 |
theme="soft",
|
| 139 |
examples=[
|
|
@@ -148,3 +171,4 @@ Drop a question or say what's up π
|
|
| 148 |
)
|
| 149 |
|
| 150 |
chat.launch()
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
# π§ Load Zephyr model
|
| 8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 9 |
|
| 10 |
+
# π§ MEMORY SETUP (HF Spaces Safe)
|
| 11 |
+
MEMORY_FILE = "/home/user/hoopmind_memory.json"
|
| 12 |
|
| 13 |
def load_memory():
|
| 14 |
if os.path.exists(MEMORY_FILE):
|
|
|
|
| 22 |
|
| 23 |
def update_memory(user_input):
|
| 24 |
memory = load_memory()
|
| 25 |
+
lower = user_input.lower()
|
| 26 |
+
|
| 27 |
+
if "my name is" in lower:
|
| 28 |
name = user_input.split("is")[-1].strip().capitalize()
|
| 29 |
memory["name"] = name
|
| 30 |
+
|
| 31 |
+
if "i'm injured" in lower:
|
| 32 |
memory["injury"] = "yes"
|
| 33 |
+
|
| 34 |
+
if "forget my injury" in lower:
|
| 35 |
memory.pop("injury", None)
|
| 36 |
+
|
| 37 |
save_memory(memory)
|
| 38 |
return memory
|
| 39 |
|
| 40 |
+
|
| 41 |
+
# π₯ FIXED: Zephyr fallback
|
| 42 |
def alpha_chatbot_response(user_input):
|
| 43 |
try:
|
| 44 |
system_prompt = (
|
| 45 |
"You are a helpful, smart, and emotionally supportive AI assistant. "
|
| 46 |
"You respond like a friend, especially when someone is feeling down or frustrated."
|
| 47 |
)
|
| 48 |
+
|
| 49 |
prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{user_input}\n<|assistant|>"
|
| 50 |
+
|
| 51 |
response = client.text_generation(
|
| 52 |
prompt,
|
| 53 |
max_new_tokens=200,
|
| 54 |
temperature=0.7,
|
| 55 |
return_full_text=False
|
| 56 |
)
|
| 57 |
+
|
| 58 |
+
# π₯ FIXED: grab generated_text properly
|
| 59 |
+
return response.generated_text.strip()
|
| 60 |
+
|
| 61 |
except Exception as e:
|
| 62 |
+
return f"π Lil error trying to respond. Debug info: {e}"
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# π₯ FIXED: history=None (no white-screen crash)
|
| 66 |
+
def hoopmind_chat(user_input, history=None):
|
| 67 |
+
if history is None:
|
| 68 |
+
history = []
|
| 69 |
|
|
|
|
|
|
|
| 70 |
user_input_lower = user_input.lower()
|
| 71 |
memory = update_memory(user_input)
|
| 72 |
|
| 73 |
+
# π Basketball logic
|
| 74 |
if any(kw in user_input_lower for kw in ["shooting", "dribbling", "defense", "injury", "ankle", "knee", "motivation"]):
|
| 75 |
if "shooting" in user_input_lower:
|
| 76 |
return random.choice([
|
| 77 |
+
"Aiming to shoot like Steph Curry? ππ₯ Focus on that wrist flick and smooth follow-through.",
|
| 78 |
+
"Make sure your elbow stays tucked and eyes locked on the rim, trust your form dawg π―.",
|
| 79 |
+
"Daily reps = automatic jumper π€π― stay locked in.",
|
| 80 |
+
"Visualize the arc and let it fly smooth π§ π.",
|
| 81 |
+
"Patience and consistency make your shot deadly π."
|
|
|
|
| 82 |
])
|
| 83 |
+
|
| 84 |
elif "dribbling" in user_input_lower:
|
| 85 |
return random.choice([
|
| 86 |
+
"Low stance, quick feet β thatβs how you get Kyrie-level handles ππ¨.",
|
| 87 |
+
"Two-ball dribbling builds control like crazy ππ§ .",
|
| 88 |
+
"Hesitations + crossovers + spins = unguardable ππ₯.",
|
| 89 |
+
"Master basics first then add the sauce π―.",
|
| 90 |
+
"Cone drills daily turn you into a problem fr π€."
|
|
|
|
| 91 |
])
|
| 92 |
+
|
| 93 |
elif "defense" in user_input_lower:
|
| 94 |
return random.choice([
|
| 95 |
+
"Stay low, move your feet, clamp season π‘οΈπ₯",
|
| 96 |
+
"No easy buckets if you hustle fr π€π―",
|
| 97 |
+
"Slide, mirror, active hands β lock in π§ "
|
| 98 |
])
|
| 99 |
+
|
| 100 |
elif "injury" in user_input_lower or "ankle" in user_input_lower or "knee" in user_input_lower:
|
| 101 |
return random.choice([
|
| 102 |
+
"Take it easy dawg π§ heal up before you go beast mode again πͺ",
|
| 103 |
+
"Ice + stretch + chill β trust the process π―",
|
| 104 |
+
"Resting ain't weak bro, it's smart π"
|
| 105 |
])
|
| 106 |
+
|
| 107 |
elif "motivation" in user_input_lower:
|
| 108 |
return random.choice([
|
| 109 |
+
"Broβ¦ you got this. Your grind is valid π―",
|
| 110 |
+
"Mamba mentality dawg π keep pushing",
|
| 111 |
+
"Every legend started where you are π₯ keep going"
|
| 112 |
])
|
| 113 |
+
|
| 114 |
+
# π Football logic
|
| 115 |
elif any(kw in user_input_lower for kw in ["football", "quarterback", "receiver", "linebacker", "tackle", "nfl"]):
|
| 116 |
return random.choice([
|
| 117 |
+
"Wanna drop dimes like Mahomes? Footwork + timing = elite πͺπ₯",
|
| 118 |
+
"Receivers need clean routes + sticky hands π§ π―",
|
| 119 |
+
"Linebackers read plays like a book fr π",
|
| 120 |
+
"Defense wins games dawg, lock in π―",
|
| 121 |
+
"Agility + sprints make you a menace πββοΈπ¨"
|
| 122 |
])
|
| 123 |
|
| 124 |
+
# π Greetings
|
| 125 |
elif any(word in user_input_lower for word in ["hi", "hey", "yo", "sup", "what's up"]):
|
| 126 |
return random.choice([
|
| 127 |
+
"Whatβs good bro? Tryna hoop or chill?",
|
| 128 |
+
"Wassup homie π¬ what we cookin today?",
|
| 129 |
+
"Iβm here dawg π§ π₯ whatβs the move?"
|
| 130 |
])
|
| 131 |
+
|
| 132 |
+
# π Reactions
|
| 133 |
elif any(word in user_input_lower for word in ["lol", "fr", "bro", "lmao", "funny"]):
|
| 134 |
return random.choice([
|
| 135 |
+
"LMAOO you goofy dawg π",
|
| 136 |
"BROO thatβs facts π",
|
| 137 |
+
"π€£ you wild fr"
|
| 138 |
])
|
| 139 |
+
|
| 140 |
+
# π Help or confusion
|
| 141 |
elif any(word in user_input_lower for word in ["idk", "help", "what can you do"]):
|
| 142 |
return random.choice([
|
| 143 |
+
"Iβm your hoop buddy + hype man dawg ππ₯",
|
| 144 |
+
"Ask anything β hoop, life, school, whatever π―",
|
| 145 |
+
"Iβm your trainer + motivator + homie in one π§ πͺ"
|
| 146 |
])
|
| 147 |
+
|
| 148 |
+
# π§ Fallback to Zephyr
|
| 149 |
return alpha_chatbot_response(user_input)
|
| 150 |
|
| 151 |
+
|
| 152 |
+
# π Fixed chat UI
|
| 153 |
chat = gr.ChatInterface(
|
| 154 |
fn=hoopmind_chat,
|
| 155 |
title="π HoopMind β Your Basketball Helper AI",
|
| 156 |
description="""
|
| 157 |
β¨ Welcome to **HoopMind** β¨
|
| 158 |
Your AI hooper brother. Talk hoops, life, school, or just chill!
|
| 159 |
+
Drop a question below π
|
| 160 |
""",
|
| 161 |
theme="soft",
|
| 162 |
examples=[
|
|
|
|
| 171 |
)
|
| 172 |
|
| 173 |
chat.launch()
|
| 174 |
+
|