Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,166 @@
|
|
1 |
-
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
"""
|
5 |
-
|
6 |
"""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
|
|
|
|
2 |
"""
|
3 |
+
Hockey Mind AI Chatbot - Fixed Gradio Interface for Hugging Face Spaces
|
4 |
"""
|
5 |
+
import gradio as gr
|
6 |
+
import asyncio
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
from OpenAPI_DB import agentic_hockey_chat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Load environment variables
|
12 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Global variable to track if resources are loaded
|
15 |
+
resources_loaded = False
|
16 |
|
17 |
+
async def chat_interface(user_role, user_team, user_prompt):
|
18 |
+
"""Interface function for Gradio"""
|
19 |
+
global resources_loaded
|
20 |
+
|
21 |
+
try:
|
22 |
+
# Load resources on first use to save memory
|
23 |
+
if not resources_loaded:
|
24 |
+
try:
|
25 |
+
from OpenAPI_DB import load_resources
|
26 |
+
load_resources()
|
27 |
+
resources_loaded = True
|
28 |
+
except ImportError as import_err:
|
29 |
+
return f"Import Error: {str(import_err)}. Please check if all required packages are installed.", "Unable to load ML models."
|
30 |
+
|
31 |
+
# Call the main chat function
|
32 |
+
result = await agentic_hockey_chat(user_role, user_team, user_prompt)
|
33 |
+
|
34 |
+
# Format response for Gradio
|
35 |
+
ai_response = result.get('ai_response', 'Sorry, no response generated.')
|
36 |
+
recommendations = result.get('recommended_content_details', [])
|
37 |
+
|
38 |
+
# Format recommendations as HTML
|
39 |
+
rec_html = ""
|
40 |
+
if recommendations:
|
41 |
+
rec_html = "<h3>π Recommended Videos:</h3><ul>"
|
42 |
+
for rec in recommendations[:5]:
|
43 |
+
title = rec.get('title', 'No title')
|
44 |
+
url = rec.get('url', '#')
|
45 |
+
similarity = rec.get('similarity', 0)
|
46 |
+
rec_html += f"<li><a href='{url}' target='_blank'>{title}</a> (Similarity: {similarity:.3f})</li>"
|
47 |
+
rec_html += "</ul>"
|
48 |
+
|
49 |
+
return ai_response, rec_html
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
import traceback
|
53 |
+
error_details = traceback.format_exc()
|
54 |
+
return f"Error: {str(e)}\n\nDetails:\n{error_details}", "No recommendations available due to error."
|
55 |
|
56 |
+
def sync_chat_interface(user_role, user_team, user_prompt):
|
57 |
+
"""Synchronous wrapper for Gradio"""
|
58 |
+
return asyncio.run(chat_interface(user_role, user_team, user_prompt))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Gradio Interface
|
61 |
+
with gr.Blocks(
|
62 |
+
title="π Hockey Mind AI Chatbot",
|
63 |
+
theme=gr.themes.Soft(),
|
64 |
+
css="""
|
65 |
+
.gradio-container {max-width: 800px !important; margin: auto !important;}
|
66 |
+
.main-header {text-align: center; margin-bottom: 2rem;}
|
67 |
+
"""
|
68 |
+
) as demo:
|
69 |
+
|
70 |
+
gr.HTML("""
|
71 |
+
<div class="main-header">
|
72 |
+
<h1>π Hockey Mind AI Chatbot</h1>
|
73 |
+
<p>Get personalized hockey advice and video recommendations!</p>
|
74 |
+
<p><i>Optimized for field hockey coaching, training, and player development</i></p>
|
75 |
+
</div>
|
76 |
+
""")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
user_role = gr.Dropdown(
|
81 |
+
choices=["le Trainer", "le Coach", "Speler"],
|
82 |
+
label="Your Role π€",
|
83 |
+
value="Coach"
|
84 |
+
)
|
85 |
+
|
86 |
+
user_team = gr.Textbox(
|
87 |
+
label="Team/Level π",
|
88 |
+
placeholder="e.g., U8C, Toronto Maple Leafs, Beginner",
|
89 |
+
value="U10"
|
90 |
+
)
|
91 |
+
|
92 |
+
user_prompt = gr.Textbox(
|
93 |
+
label="Your Question β",
|
94 |
+
placeholder="Ask about drills, techniques, strategies, rules...",
|
95 |
+
lines=3
|
96 |
+
)
|
97 |
+
|
98 |
+
submit_btn = gr.Button("Get Hockey Advice π", variant="primary", size="lg")
|
99 |
+
|
100 |
+
with gr.Row():
|
101 |
+
ai_response = gr.Textbox(
|
102 |
+
label="π€ AI Response",
|
103 |
+
lines=8,
|
104 |
+
interactive=False
|
105 |
+
)
|
106 |
+
|
107 |
+
with gr.Row():
|
108 |
+
recommendations = gr.HTML()
|
109 |
+
|
110 |
+
# Examples section
|
111 |
+
gr.HTML("<br><h3>π‘ Example Questions:</h3>")
|
112 |
+
|
113 |
+
examples = gr.Examples(
|
114 |
+
examples=[
|
115 |
+
["Coach", "U8C", "What are the best backhand shooting drills for young players?"],
|
116 |
+
["Player", "Intermediate", "How can I improve my penalty corner technique?"],
|
117 |
+
["le Coach", "U10", "Geef me oefeningen voor backhandschoten"],
|
118 |
+
["Parent", "Beginner", "What equipment does my child need to start playing hockey?"],
|
119 |
+
["Coach", "Advanced", "What are effective small-sided games for skill development?"],
|
120 |
+
],
|
121 |
+
inputs=[user_role, user_team, user_prompt],
|
122 |
+
outputs=[ai_response, recommendations],
|
123 |
+
fn=sync_chat_interface,
|
124 |
+
)
|
125 |
+
|
126 |
+
# Event handler
|
127 |
+
submit_btn.click(
|
128 |
+
fn=sync_chat_interface,
|
129 |
+
inputs=[user_role, user_team, user_prompt],
|
130 |
+
outputs=[ai_response, recommendations],
|
131 |
+
api_name="chat"
|
132 |
+
)
|
133 |
+
|
134 |
+
user_prompt.submit(
|
135 |
+
fn=sync_chat_interface,
|
136 |
+
inputs=[user_role, user_team, user_prompt],
|
137 |
+
outputs=[ai_response, recommendations]
|
138 |
+
)
|
139 |
+
|
140 |
+
# Footer
|
141 |
+
gr.HTML("""
|
142 |
+
<br>
|
143 |
+
<div style="text-align: center; color: #666; font-size: 0.9em;">
|
144 |
+
<p>π Hockey Mind AI - Powered by OpenRouter & Sentence Transformers</p>
|
145 |
+
<p>Supports English & Dutch | Built for field hockey community</p>
|
146 |
+
</div>
|
147 |
+
""")
|
148 |
|
149 |
+
# Launch configuration for Hugging Face Spaces
|
150 |
if __name__ == "__main__":
|
151 |
+
# Check if running on Hugging Face Spaces
|
152 |
+
if os.getenv("SPACE_ID"):
|
153 |
+
# Production mode on HF Spaces
|
154 |
+
demo.launch(
|
155 |
+
server_name="0.0.0.0",
|
156 |
+
server_port=7860,
|
157 |
+
share=False,
|
158 |
+
show_error=True,
|
159 |
+
quiet=False
|
160 |
+
)
|
161 |
+
else:
|
162 |
+
# Local development mode
|
163 |
+
demo.launch(
|
164 |
+
share=True,
|
165 |
+
show_error=True
|
166 |
+
)
|