Spaces:
No application file
No application file
Liss, Alex (NYC-HUG)
commited on
Commit
·
58e0496
1
Parent(s):
5ea2e35
rebuilding WIP component, take 2
Browse files- gradio_app.py +91 -52
- tools/game_recap.py +19 -0
gradio_app.py
CHANGED
@@ -222,61 +222,91 @@ async def process_message(message):
|
|
222 |
print('Calling generate_response function...')
|
223 |
agent_response = generate_response(message, state.session_id)
|
224 |
print(f"Agent response received: {agent_response}")
|
225 |
-
|
226 |
-
#
|
227 |
output = agent_response.get("output", "")
|
228 |
metadata = agent_response.get("metadata", {})
|
229 |
print(f"Extracted output: {output}")
|
230 |
print(f"Extracted metadata: {metadata}")
|
231 |
|
232 |
-
#
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
|
|
255 |
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
'date': '10/11/24',
|
261 |
-
'location': 'Raymond James Stadium',
|
262 |
-
'home_team': 'Tampa Bay Buccaneers',
|
263 |
-
'away_team': 'San Francisco 49ers',
|
264 |
-
'home_score': '20',
|
265 |
-
'away_score': '23',
|
266 |
-
'result': '20-23',
|
267 |
-
'winner': 'away',
|
268 |
-
'home_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/tb.png',
|
269 |
-
'away_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/sf.png',
|
270 |
-
'highlight_video_url': 'https://www.youtube.com/watch?v=607mv01G8UU'
|
271 |
-
}
|
272 |
-
state.set_current_game(game_data)
|
273 |
-
print(f"Set current game to Bucs game")
|
274 |
else:
|
275 |
-
#
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
else:
|
278 |
# Not a game recap query
|
279 |
state.set_current_game(None)
|
|
|
280 |
|
281 |
# Add assistant response to state
|
282 |
state.add_message("assistant", output)
|
@@ -367,24 +397,33 @@ with gr.Blocks(title="49ers FanAI Hub", theme=gr.themes.Soft(), css=css) as demo
|
|
367 |
history.append({"role": "user", "content": message})
|
368 |
response = await process_message(message)
|
369 |
history.append({"role": "assistant", "content": response})
|
370 |
-
|
371 |
# Update game recap component visibility based on current_game
|
372 |
has_game_data = state.current_game is not None
|
373 |
|
374 |
# Create the game recap HTML content if we have game data
|
375 |
if has_game_data:
|
376 |
-
#
|
|
|
|
|
|
|
377 |
game_recap_html = create_game_recap_component(state.current_game)
|
378 |
-
|
|
|
|
|
|
|
|
|
379 |
container_update = gr.update(visible=True)
|
|
|
|
|
380 |
else:
|
381 |
# Create an empty HTML component
|
|
|
382 |
game_recap_html = gr.HTML("")
|
383 |
-
#
|
384 |
container_update = gr.update(visible=False)
|
385 |
-
|
386 |
-
|
387 |
-
return "", history, game_recap_html, container_update
|
388 |
|
389 |
# Set up event handlers with the combined function - explicitly disable queue
|
390 |
msg.submit(process_and_respond, [msg, chatbot], [msg, chatbot, game_recap, game_recap_container], queue=False)
|
|
|
222 |
print('Calling generate_response function...')
|
223 |
agent_response = generate_response(message, state.session_id)
|
224 |
print(f"Agent response received: {agent_response}")
|
225 |
+
|
226 |
+
# Always extract the output first, before any other processing
|
227 |
output = agent_response.get("output", "")
|
228 |
metadata = agent_response.get("metadata", {})
|
229 |
print(f"Extracted output: {output}")
|
230 |
print(f"Extracted metadata: {metadata}")
|
231 |
|
232 |
+
# Import the game_recap module to access the cached game data
|
233 |
+
from tools.game_recap import get_last_game_data
|
234 |
+
|
235 |
+
# FIRST CHECK: Get the cached game data (this is the most reliable indicator)
|
236 |
+
cached_game_data = get_last_game_data()
|
237 |
+
|
238 |
+
# SECOND CHECK: Look for game-related keywords in the output
|
239 |
+
is_game_related = any(keyword in output.lower() for keyword in [
|
240 |
+
"score", "stadium", "defeated", "won", "lost", "final score",
|
241 |
+
"game at", "home team", "away team", "dolphins", "49ers", "seahawks",
|
242 |
+
"jets", "vikings", "cardinals", "buccaneers", "final"
|
243 |
+
])
|
244 |
+
|
245 |
+
# THIRD CHECK: Check metadata for Game Recap tool usage (rarely works but try)
|
246 |
+
tools_used = metadata.get("tools_used", [])
|
247 |
+
tool_used_game_recap = "Game Recap" in str(tools_used)
|
248 |
+
|
249 |
+
# Determine if this is a game recap response
|
250 |
+
is_game_recap = cached_game_data is not None or (is_game_related and "game" in message.lower())
|
251 |
+
|
252 |
+
print(f"Is game recap detection: cached_data={cached_game_data is not None}, keywords={is_game_related}, tool={tool_used_game_recap}")
|
253 |
+
|
254 |
+
if is_game_recap:
|
255 |
+
print("Game Recap detected in response")
|
256 |
|
257 |
+
if cached_game_data:
|
258 |
+
print(f"Found cached game data: {cached_game_data}")
|
259 |
+
state.set_current_game(cached_game_data)
|
260 |
+
print("Set current game from cache")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
else:
|
262 |
+
# Fallback for cases where the cache doesn't work
|
263 |
+
print("No cached game data found - using text-based game detection")
|
264 |
+
|
265 |
+
# Text-based game detection as a fallback
|
266 |
+
if "Vikings" in output and "49ers" in output:
|
267 |
+
# Create Vikings game data
|
268 |
+
game_data = {
|
269 |
+
'game_id': 'vikings-game',
|
270 |
+
'date': '15/09/2024',
|
271 |
+
'location': 'U.S. Bank Stadium',
|
272 |
+
'home_team': 'Minnesota Vikings',
|
273 |
+
'away_team': 'San Francisco 49ers',
|
274 |
+
'home_score': '23',
|
275 |
+
'away_score': '17',
|
276 |
+
'result': '23-17',
|
277 |
+
'winner': 'home',
|
278 |
+
'home_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/min.png',
|
279 |
+
'away_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/sf.png',
|
280 |
+
'highlight_video_url': 'https://www.youtube.com/watch?v=jTJw2uf-Pdg'
|
281 |
+
}
|
282 |
+
state.set_current_game(game_data)
|
283 |
+
print("Set current game to Vikings game from text")
|
284 |
+
elif "Dolphins" in output and "49ers" in output:
|
285 |
+
# Create Dolphins game data
|
286 |
+
game_data = {
|
287 |
+
'game_id': 'dolphins-game',
|
288 |
+
'date': '22/12/2024',
|
289 |
+
'location': 'Hard Rock Stadium',
|
290 |
+
'home_team': 'Miami Dolphins',
|
291 |
+
'away_team': 'San Francisco 49ers',
|
292 |
+
'home_score': '29',
|
293 |
+
'away_score': '17',
|
294 |
+
'result': '29-17',
|
295 |
+
'winner': 'home',
|
296 |
+
'home_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/mia.png',
|
297 |
+
'away_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/sf.png',
|
298 |
+
'highlight_video_url': 'https://www.youtube.com/watch?v=example'
|
299 |
+
}
|
300 |
+
state.set_current_game(game_data)
|
301 |
+
print("Set current game to Dolphins game from text")
|
302 |
+
else:
|
303 |
+
# No game detected
|
304 |
+
state.set_current_game(None)
|
305 |
+
print("No game detected in text")
|
306 |
else:
|
307 |
# Not a game recap query
|
308 |
state.set_current_game(None)
|
309 |
+
print("Not a game recap query")
|
310 |
|
311 |
# Add assistant response to state
|
312 |
state.add_message("assistant", output)
|
|
|
397 |
history.append({"role": "user", "content": message})
|
398 |
response = await process_message(message)
|
399 |
history.append({"role": "assistant", "content": response})
|
400 |
+
|
401 |
# Update game recap component visibility based on current_game
|
402 |
has_game_data = state.current_game is not None
|
403 |
|
404 |
# Create the game recap HTML content if we have game data
|
405 |
if has_game_data:
|
406 |
+
# Print game data for debugging
|
407 |
+
print(f"Creating game recap component with data: {state.current_game}")
|
408 |
+
|
409 |
+
# Create game recap component and add debugging
|
410 |
game_recap_html = create_game_recap_component(state.current_game)
|
411 |
+
|
412 |
+
# Debug the HTML content
|
413 |
+
print(f"Game recap component created: {type(game_recap_html)}")
|
414 |
+
|
415 |
+
# Make sure the container is visible
|
416 |
container_update = gr.update(visible=True)
|
417 |
+
|
418 |
+
return "", history, game_recap_html, container_update
|
419 |
else:
|
420 |
# Create an empty HTML component
|
421 |
+
print("No game data available, hiding game recap component")
|
422 |
game_recap_html = gr.HTML("")
|
423 |
+
# Hide the container
|
424 |
container_update = gr.update(visible=False)
|
425 |
+
|
426 |
+
return "", history, game_recap_html, container_update
|
|
|
427 |
|
428 |
# Set up event handlers with the combined function - explicitly disable queue
|
429 |
msg.submit(process_and_respond, [msg, chatbot], [msg, chatbot, game_recap, game_recap_container], queue=False)
|
tools/game_recap.py
CHANGED
@@ -17,6 +17,21 @@ from gradio_graph import graph
|
|
17 |
from langchain_neo4j import GraphCypherQAChain
|
18 |
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Create the Cypher generation prompt for game search
|
21 |
GAME_SEARCH_TEMPLATE = """
|
22 |
You are an expert Neo4j Developer translating user questions about NFL games into Cypher queries.
|
@@ -216,6 +231,10 @@ def game_recap_qa(input_text):
|
|
216 |
# Generate the recap
|
217 |
recap_text = generate_game_recap(game_data)
|
218 |
|
|
|
|
|
|
|
|
|
219 |
# Return both the text and structured data
|
220 |
return {
|
221 |
"output": recap_text,
|
|
|
17 |
from langchain_neo4j import GraphCypherQAChain
|
18 |
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
|
19 |
|
20 |
+
# Create a global variable to store the last retrieved game data
|
21 |
+
# This is a workaround for LangChain dropping structured data
|
22 |
+
LAST_GAME_DATA = None
|
23 |
+
|
24 |
+
# Function to get the cached game data
|
25 |
+
def get_last_game_data():
|
26 |
+
global LAST_GAME_DATA
|
27 |
+
return LAST_GAME_DATA
|
28 |
+
|
29 |
+
# Function to set the cached game data
|
30 |
+
def set_last_game_data(game_data):
|
31 |
+
global LAST_GAME_DATA
|
32 |
+
LAST_GAME_DATA = game_data
|
33 |
+
print(f"STORED GAME DATA IN CACHE: {game_data}")
|
34 |
+
|
35 |
# Create the Cypher generation prompt for game search
|
36 |
GAME_SEARCH_TEMPLATE = """
|
37 |
You are an expert Neo4j Developer translating user questions about NFL games into Cypher queries.
|
|
|
231 |
# Generate the recap
|
232 |
recap_text = generate_game_recap(game_data)
|
233 |
|
234 |
+
# CRITICAL: Store the game data in our cache so it can be retrieved later
|
235 |
+
# This is a workaround for LangChain dropping structured data
|
236 |
+
set_last_game_data(game_data)
|
237 |
+
|
238 |
# Return both the text and structured data
|
239 |
return {
|
240 |
"output": recap_text,
|