Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,19 @@ import os
|
|
| 4 |
import json
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
-
#
|
| 8 |
# π§ MEMORY SETUP (HF Spaces Safe)
|
|
|
|
|
|
|
| 9 |
MEMORY_FILE = "/home/user/hoopmind_memory.json"
|
| 10 |
|
| 11 |
def load_memory():
|
| 12 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
return {}
|
| 14 |
|
| 15 |
def save_memory(memory):
|
|
@@ -19,20 +26,36 @@ def save_memory(memory):
|
|
| 19 |
def update_memory(user_input):
|
| 20 |
memory = load_memory()
|
| 21 |
lower = user_input.lower()
|
| 22 |
-
ower:
|
| 23 |
-
name = user_input.split("is")[-1].strip().capitalize()
|
| 24 |
-
memory["name"] = name
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
memory["injury"] = "yes"
|
| 28 |
-
|
|
|
|
| 29 |
memory.pop("injury", None)
|
| 30 |
|
| 31 |
save_memory(memory)
|
| 32 |
return memory
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
"You respond like a friend, especially when someone is feeling down or frustrated."
|
| 37 |
)
|
| 38 |
|
|
@@ -45,14 +68,16 @@ n lower:
|
|
| 45 |
return_full_text=False
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# π₯ FIXED: grab generated_text properly
|
| 49 |
return response.generated_text.strip()
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
return f"π Lil error trying to respond. Debug info: {e}"
|
| 53 |
|
| 54 |
|
| 55 |
-
#
|
|
|
|
|
|
|
|
|
|
| 56 |
def hoopmind_chat(user_input, history=None):
|
| 57 |
if history is None:
|
| 58 |
history = []
|
|
@@ -60,92 +85,95 @@ def hoopmind_chat(user_input, history=None):
|
|
| 60 |
user_input_lower = user_input.lower()
|
| 61 |
memory = update_memory(user_input)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
if any(kw in user_input_lower for kw in ["shooting", "dribbling", "defense", "injury", "ankle", "knee", "motivation"]):
|
| 65 |
if "shooting" in user_input_lower:
|
| 66 |
return random.choice([
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
"Daily reps = automatic jumper
|
| 70 |
-
"Visualize the arc
|
| 71 |
-
"
|
| 72 |
])
|
| 73 |
|
| 74 |
elif "dribbling" in user_input_lower:
|
| 75 |
return random.choice([
|
| 76 |
-
"
|
| 77 |
-
"Two-ball
|
| 78 |
-
"
|
| 79 |
-
"Master basics
|
| 80 |
-
"Cone drills daily
|
| 81 |
])
|
| 82 |
|
| 83 |
elif "defense" in user_input_lower:
|
| 84 |
return random.choice([
|
| 85 |
-
"Stay low
|
| 86 |
"No easy buckets if you hustle fr π€π―",
|
| 87 |
-
"
|
| 88 |
])
|
| 89 |
|
| 90 |
-
elif
|
| 91 |
return random.choice([
|
| 92 |
-
"Take it easy dawg π§ heal up before
|
| 93 |
-
"Ice + stretch + chill β
|
| 94 |
-
"
|
| 95 |
])
|
| 96 |
|
| 97 |
elif "motivation" in user_input_lower:
|
| 98 |
return random.choice([
|
| 99 |
-
"Broβ¦
|
| 100 |
-
"Mamba mentality dawg π
|
| 101 |
-
"Every legend started
|
| 102 |
])
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
return random.choice([
|
| 107 |
-
"Wanna
|
| 108 |
-
"Receivers need
|
| 109 |
"Linebackers read plays like a book fr π",
|
| 110 |
-
"Defense wins games dawg, lock
|
| 111 |
-
"Agility + sprints
|
| 112 |
])
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
|
| 116 |
return random.choice([
|
| 117 |
-
"Whatβs good bro?
|
| 118 |
-
"Wassup homie π¬ what we
|
| 119 |
-
"I
|
| 120 |
])
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
return random.choice([
|
| 125 |
"LMAOO you goofy dawg π",
|
| 126 |
"BROO thatβs facts π",
|
| 127 |
"π€£ you wild fr"
|
| 128 |
])
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
|
| 132 |
return random.choice([
|
| 133 |
-
"Iβm your hoop buddy
|
| 134 |
-
"Ask anything β hoop, life, school,
|
| 135 |
-
"I
|
| 136 |
])
|
| 137 |
|
| 138 |
-
#
|
| 139 |
return alpha_chatbot_response(user_input)
|
| 140 |
|
| 141 |
|
| 142 |
-
#
|
|
|
|
|
|
|
|
|
|
| 143 |
chat = gr.ChatInterface(
|
| 144 |
fn=hoopmind_chat,
|
| 145 |
title="π HoopMind β Your Basketball Helper AI",
|
| 146 |
description="""
|
| 147 |
β¨ Welcome to **HoopMind** β¨
|
| 148 |
-
Your AI hooper brother. Talk hoops, life, school, or just
|
| 149 |
Drop a question below π
|
| 150 |
""",
|
| 151 |
theme="soft",
|
|
@@ -162,3 +190,4 @@ Drop a question below π
|
|
| 162 |
|
| 163 |
chat.launch()
|
| 164 |
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
# ==========================================
|
| 8 |
# π§ MEMORY SETUP (HF Spaces Safe)
|
| 9 |
+
# ==========================================
|
| 10 |
+
|
| 11 |
MEMORY_FILE = "/home/user/hoopmind_memory.json"
|
| 12 |
|
| 13 |
def load_memory():
|
| 14 |
+
if os.path.exists(MEMORY_FILE):
|
| 15 |
+
try:
|
| 16 |
+
with open(MEMORY_FILE, "r") as f:
|
| 17 |
+
return json.load(f)
|
| 18 |
+
except:
|
| 19 |
+
return {}
|
| 20 |
return {}
|
| 21 |
|
| 22 |
def save_memory(memory):
|
|
|
|
| 26 |
def update_memory(user_input):
|
| 27 |
memory = load_memory()
|
| 28 |
lower = user_input.lower()
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# name detection
|
| 31 |
+
if "my name is" in lower or "i am" in lower:
|
| 32 |
+
try:
|
| 33 |
+
name = user_input.split("is")[-1].strip().capitalize()
|
| 34 |
+
if len(name) > 0:
|
| 35 |
+
memory["name"] = name
|
| 36 |
+
except:
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
# injury tracking
|
| 40 |
+
if "i'm injured" in lower or "im injured" in lower:
|
| 41 |
memory["injury"] = "yes"
|
| 42 |
+
|
| 43 |
+
if "i'm healed" in lower or "im healed" in lower:
|
| 44 |
memory.pop("injury", None)
|
| 45 |
|
| 46 |
save_memory(memory)
|
| 47 |
return memory
|
| 48 |
|
| 49 |
+
# ==========================================
|
| 50 |
+
# π§ ZEPHYR / TEXT GENERATION FALLBACK
|
| 51 |
+
# ==========================================
|
| 52 |
+
|
| 53 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 54 |
|
| 55 |
+
def alpha_chatbot_response(user_input):
|
| 56 |
+
try:
|
| 57 |
+
system_prompt = (
|
| 58 |
+
"You are HoopMind, a friendly, motivational, athletic, emotionally supportive AI assistant. "
|
| 59 |
"You respond like a friend, especially when someone is feeling down or frustrated."
|
| 60 |
)
|
| 61 |
|
|
|
|
| 68 |
return_full_text=False
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
return response.generated_text.strip()
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
return f"π Lil error trying to respond. Debug info: {e}"
|
| 75 |
|
| 76 |
|
| 77 |
+
# ==========================================
|
| 78 |
+
# π§ MAIN CHAT LOGIC
|
| 79 |
+
# ==========================================
|
| 80 |
+
|
| 81 |
def hoopmind_chat(user_input, history=None):
|
| 82 |
if history is None:
|
| 83 |
history = []
|
|
|
|
| 85 |
user_input_lower = user_input.lower()
|
| 86 |
memory = update_memory(user_input)
|
| 87 |
|
| 88 |
+
# BASKETBALL LOGIC
|
| 89 |
if any(kw in user_input_lower for kw in ["shooting", "dribbling", "defense", "injury", "ankle", "knee", "motivation"]):
|
| 90 |
if "shooting" in user_input_lower:
|
| 91 |
return random.choice([
|
| 92 |
+
"Shoot like Curry bro ππ₯ Lock that wrist and follow through.",
|
| 93 |
+
"Tuck your elbow, eyes on the rim β smooth release every time π―.",
|
| 94 |
+
"Daily reps = automatic jumper π€π―.",
|
| 95 |
+
"Visualize the arc dawg π§ π.",
|
| 96 |
+
"Slow and controlled forms turn deadly over time π."
|
| 97 |
])
|
| 98 |
|
| 99 |
elif "dribbling" in user_input_lower:
|
| 100 |
return random.choice([
|
| 101 |
+
"Stay low, quick feet β Kyrie mode activated ππ¨.",
|
| 102 |
+
"Two-ball drills build CRAZY control ππ§ .",
|
| 103 |
+
"Mix hesitations + crossovers + spins for max sauce ππ₯.",
|
| 104 |
+
"Master the basics before adding flavor π―.",
|
| 105 |
+
"Cone drills daily make defenders slip π€."
|
| 106 |
])
|
| 107 |
|
| 108 |
elif "defense" in user_input_lower:
|
| 109 |
return random.choice([
|
| 110 |
+
"Stay low & move your feet, clamp season π‘οΈπ₯",
|
| 111 |
"No easy buckets if you hustle fr π€π―",
|
| 112 |
+
"Mirror their hips and keep active hands π§ "
|
| 113 |
])
|
| 114 |
|
| 115 |
+
elif any(kw in user_input_lower for kw in ["injury", "ankle", "knee"]):
|
| 116 |
return random.choice([
|
| 117 |
+
"Take it easy dawg π§ heal up before going beast mode again πͺ",
|
| 118 |
+
"Ice + stretch + chill β your body will thank you π―",
|
| 119 |
+
"Rest > reinjury bro π be smart."
|
| 120 |
])
|
| 121 |
|
| 122 |
elif "motivation" in user_input_lower:
|
| 123 |
return random.choice([
|
| 124 |
+
"Broβ¦ your grind is REAL. Keep pushing π―",
|
| 125 |
+
"Mamba mentality dawg π stay locked in.",
|
| 126 |
+
"Every legend started just like you π₯ keep going."
|
| 127 |
])
|
| 128 |
|
| 129 |
+
# FOOTBALL LOGIC
|
| 130 |
+
if any(kw in user_input_lower for kw in ["football", "quarterback", "receiver", "linebacker", "tackle", "nfl"]):
|
| 131 |
return random.choice([
|
| 132 |
+
"Wanna throw dots like Mahomes? Footwork + timing = elite πͺπ₯",
|
| 133 |
+
"Receivers need crisp routes + sticky hands π§ π―",
|
| 134 |
"Linebackers read plays like a book fr π",
|
| 135 |
+
"Defense wins games dawg, lock IN π―",
|
| 136 |
+
"Agility + sprints turn you into a menace πββοΈπ¨"
|
| 137 |
])
|
| 138 |
|
| 139 |
+
# GREETINGS
|
| 140 |
+
if any(word in user_input_lower for word in ["hi", "hey", "yo", "sup", "what's up"]):
|
| 141 |
return random.choice([
|
| 142 |
+
"Whatβs good bro? Hooping or chilling?",
|
| 143 |
+
"Wassup homie π¬ what we on today?",
|
| 144 |
+
"I'm here dawg π§ π₯ let's cook."
|
| 145 |
])
|
| 146 |
|
| 147 |
+
# REACTIONS
|
| 148 |
+
if any(word in user_input_lower for word in ["lol", "fr", "bro", "lmao", "funny"]):
|
| 149 |
return random.choice([
|
| 150 |
"LMAOO you goofy dawg π",
|
| 151 |
"BROO thatβs facts π",
|
| 152 |
"π€£ you wild fr"
|
| 153 |
])
|
| 154 |
|
| 155 |
+
# HELP OR CONFUSION
|
| 156 |
+
if any(word in user_input_lower for word in ["idk", "help", "what can you do"]):
|
| 157 |
return random.choice([
|
| 158 |
+
"Iβm your hoop buddy AND hype squad dawg ππ₯",
|
| 159 |
+
"Ask anything β hoop, life, school, whateva π―",
|
| 160 |
+
"I got you: trainer + motivator + homie in one π§ πͺ"
|
| 161 |
])
|
| 162 |
|
| 163 |
+
# FALLBACK: ZEPHYR
|
| 164 |
return alpha_chatbot_response(user_input)
|
| 165 |
|
| 166 |
|
| 167 |
+
# ==========================================
|
| 168 |
+
# π GRADIO UI
|
| 169 |
+
# ==========================================
|
| 170 |
+
|
| 171 |
chat = gr.ChatInterface(
|
| 172 |
fn=hoopmind_chat,
|
| 173 |
title="π HoopMind β Your Basketball Helper AI",
|
| 174 |
description="""
|
| 175 |
β¨ Welcome to **HoopMind** β¨
|
| 176 |
+
Your AI hooper brother. Talk hoops, life, school, or just vibe!
|
| 177 |
Drop a question below π
|
| 178 |
""",
|
| 179 |
theme="soft",
|
|
|
|
| 190 |
|
| 191 |
chat.launch()
|
| 192 |
|
| 193 |
+
|