import gradio as gr import random import os import json from huggingface_hub import InferenceClient # ========================================== # 🧠 MEMORY SETUP (HF Spaces Safe) # ========================================== MEMORY_FILE = "/home/user/hoopmind_memory.json" def load_memory(): if os.path.exists(MEMORY_FILE): try: with open(MEMORY_FILE, "r") as f: return json.load(f) except: return {} return {} def save_memory(memory): with open(MEMORY_FILE, "w") as f: json.dump(memory, f) def update_memory(user_input): memory = load_memory() lower = user_input.lower() # name detection if "my name is" in lower or "i am" in lower: try: name = user_input.split("is")[-1].strip().capitalize() if len(name) > 0: memory["name"] = name except: pass # injury tracking if "i'm injured" in lower or "im injured" in lower: memory["injury"] = "yes" if "i'm healed" in lower or "im healed" in lower: memory.pop("injury", None) save_memory(memory) return memory # ========================================== # 🧠 ZEPHYR / TEXT GENERATION FALLBACK # ========================================== client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") def alpha_chatbot_response(user_input): try: system_prompt = ( "You are HoopMind, a friendly, motivational, athletic, emotionally supportive AI assistant. " "You respond like a friend, especially when someone is feeling down or frustrated." ) prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{user_input}\n<|assistant|>" response = client.text_generation( prompt, max_new_tokens=200, temperature=0.7, return_full_text=False ) return response.generated_text.strip() except Exception as e: return f"πŸ˜“ Lil error trying to respond. Debug info: {e}" # ========================================== # 🧠 MAIN CHAT LOGIC # ========================================== def hoopmind_chat(user_input, history=None): if history is None: history = [] user_input_lower = user_input.lower() memory = update_memory(user_input) # BASKETBALL LOGIC if any(kw in user_input_lower for kw in ["shooting", "dribbling", "defense", "injury", "ankle", "knee", "motivation"]): if "shooting" in user_input_lower: return random.choice([ "Shoot like Curry bro πŸ€πŸ”₯ Lock that wrist and follow through.", "Tuck your elbow, eyes on the rim β€” smooth release every time 🎯.", "Daily reps = automatic jumper πŸ˜€πŸ’―.", "Visualize the arc dawg πŸ§ πŸ€.", "Slow and controlled forms turn deadly over time πŸ“ˆ." ]) elif "dribbling" in user_input_lower: return random.choice([ "Stay low, quick feet β€” Kyrie mode activated πŸ˜ˆπŸ’¨.", "Two-ball drills build CRAZY control πŸ€πŸ§ .", "Mix hesitations + crossovers + spins for max sauce πŸŒ€πŸ”₯.", "Master the basics before adding flavor πŸ’―.", "Cone drills daily make defenders slip 😀." ]) elif "defense" in user_input_lower: return random.choice([ "Stay low & move your feet, clamp season πŸ›‘οΈπŸ”₯", "No easy buckets if you hustle fr πŸ˜€πŸ’―", "Mirror their hips and keep active hands 🧠" ]) elif any(kw in user_input_lower for kw in ["injury", "ankle", "knee"]): return random.choice([ "Take it easy dawg 🧊 heal up before going beast mode again πŸ’ͺ", "Ice + stretch + chill β€” your body will thank you πŸ’―", "Rest > reinjury bro πŸ™ be smart." ]) elif "motivation" in user_input_lower: return random.choice([ "Bro… your grind is REAL. Keep pushing πŸ’―", "Mamba mentality dawg 🐍 stay locked in.", "Every legend started just like you πŸ”₯ keep going." ]) # FOOTBALL LOGIC if any(kw in user_input_lower for kw in ["football", "quarterback", "receiver", "linebacker", "tackle", "nfl"]): return random.choice([ "Wanna throw dots like Mahomes? Footwork + timing = elite πŸ’ͺπŸ”₯", "Receivers need crisp routes + sticky hands πŸ§ πŸ’―", "Linebackers read plays like a book fr πŸ“–", "Defense wins games dawg, lock IN πŸ’―", "Agility + sprints turn you into a menace πŸƒβ€β™‚οΈπŸ’¨" ]) # GREETINGS if any(word in user_input_lower for word in ["hi", "hey", "yo", "sup", "what's up"]): return random.choice([ "What’s good bro? Hooping or chilling?", "Wassup homie πŸ’¬ what we on today?", "I'm here dawg 🧠πŸ”₯ let's cook." ]) # REACTIONS if any(word in user_input_lower for word in ["lol", "fr", "bro", "lmao", "funny"]): return random.choice([ "LMAOO you goofy dawg πŸ˜‚", "BROO that’s facts πŸ’€", "🀣 you wild fr" ]) # HELP OR CONFUSION if any(word in user_input_lower for word in ["idk", "help", "what can you do"]): return random.choice([ "I’m your hoop buddy AND hype squad dawg πŸ€πŸ”₯", "Ask anything β€” hoop, life, school, whateva πŸ’―", "I got you: trainer + motivator + homie in one 🧠πŸ’ͺ" ]) # FALLBACK: ZEPHYR return alpha_chatbot_response(user_input) # ========================================== # πŸš€ GRADIO UI # ========================================== chat = gr.ChatInterface( fn=hoopmind_chat, title="πŸ€ HoopMind – Your Basketball Helper AI", description=""" ✨ Welcome to **HoopMind** ✨ Your AI hooper brother. Talk hoops, life, school, or just vibe! Drop a question below πŸ‘‡ """, theme="soft", examples=[ "How do I get better at shooting?", "Hey I'm injured right now", "What's up bro?", "Give me motivation", "Dribbling tips?", "Math help please?", "What's the capital of France?" ] ) chat.launch()