File size: 11,651 Bytes
06cb2a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import gradio as gr
import pandas as pd
import os
import html

def create_game_recap_component(game_data=None):
    """
    Creates a Gradio component to display game information with a simple table layout.
    Args:
        game_data (dict, optional): Game data to display. If None, returns an empty component.
    Returns:
        gr.HTML: A Gradio component displaying the game recap.
    """
    try:
        # If no game data provided, return an empty component
        if game_data is None or not isinstance(game_data, dict):
            return gr.HTML("")
        
        # Extract game details
        match_number = game_data.get('match_number', game_data.get('Match Number', 'N/A'))
        date = game_data.get('date', 'N/A')
        location = game_data.get('location', 'N/A')
        
        # Handle different column naming conventions between sources
        home_team = game_data.get('home_team', game_data.get('Home Team', game_data.get('HomeTeam', 'N/A')))
        away_team = game_data.get('away_team', game_data.get('Away Team', game_data.get('AwayTeam', 'N/A')))
        
        # Get team logo URLs
        home_logo = game_data.get('home_team_logo_url', '')
        away_logo = game_data.get('away_team_logo_url', '')
        
        # Get result and determine scores
        result = game_data.get('result', 'N/A')
        home_score = game_data.get('home_score', 'N/A')
        away_score = game_data.get('away_score', 'N/A')
        
        # If we don't have separate scores but have result, try to parse it
        if (home_score == 'N/A' or away_score == 'N/A') and result != 'N/A':
            scores = result.split('-')
            if len(scores) == 2:
                home_score = scores[0].strip()
                away_score = scores[1].strip()
        
        # Determine winner for highlighting
        winner = game_data.get('winner')
        if not winner and result != 'N/A':
            try:
                home_score_int = int(home_score)
                away_score_int = int(away_score)
                winner = 'home' if home_score_int > away_score_int else 'away'
            except ValueError:
                winner = None
        
        # Get highlight video URL
        highlight_video_url = game_data.get('highlight_video_url', '')
        
        # Create a simple HTML table layout
        html_content = f"""
        <style>
            .game-recap-table {{
                width: 100%;
                border-collapse: collapse;
                border: 1px solid #ddd;
                border-radius: 8px;
                overflow: hidden;
                margin: 20px 0;
                box-shadow: 0 2px 5px rgba(0,0,0,0.1); /* Reverted: Uncommented for Bug 2 re-evaluation */
            }}
            
            .team-cell {{
                padding: 15px;
                text-align: left;
                vertical-align: middle;
                border-bottom: 1px solid #eee;
            }}
            
            .team-logo {{
                width: 40px;
                height: 40px;
                object-fit: contain;
                margin-right: 10px;
                border-radius: 50%;
                vertical-align: middle;
            }}
            
            .team-name {{
                display: inline-block;
                vertical-align: middle;
            }}
            
            .team-name-main {{
                font-weight: bold;
                font-size: 18px;
                display: block;
            }}
            
            .team-name-sub {{
                font-size: 14px;
                color: #666;
                display: block;
            }}
            
            .score-cell {{
                padding: 15px;
                text-align: right;
                font-size: 32px;
                font-weight: bold;
                vertical-align: middle;
                width: 80px;
            }}
            
            .winner-indicator {{
                color: #ff6b00;
                margin-left: 5px;
            }}
            
            .video-cell {{
                background: #ffffff; /* Reverted back to solid white */
                color: #333;         /* changed from white to a darker color for visibility */
                padding: 20px;
                text-align: center;
                vertical-align: middle;
            }}
            
            .vs-container {{
                margin-bottom: 15px;
            }}
            
            .vs-logo {{
                width: 30px;
                height: 30px;
                object-fit: contain;
                margin: 0 5px;
                border-radius: 50%;
                background-color: rgba(255,255,255,0.2);
                vertical-align: middle;
            }}
            
            .vs-text {{
                font-weight: bold;
                margin: 0 5px;
            }}
            
            .recap-text {{
                font-size: 24px;
                font-weight: bold;
                margin: 10px 0;
                color: #AA0000 !important; /* 49ers primary red from your design system */
            }}
            
            .video-link {{
                display: inline-block;
                padding: 8px 15px;
                background-color: #AA0000;
                color: white;
                text-decoration: none;
                border-radius: 4px;
                margin-top: 10px;
            }}
            
            .video-link:hover {{
                background-color: #B3995D;
            }}
        </style>
        
        <table class="game-recap-table">
            <tr>
                <td class="team-cell">
                    <img src="{html.escape(away_logo)}" alt="{html.escape(away_team)} logo" class="team-logo">
                    <div class="team-name">
                        <span class="team-name-main">{html.escape(away_team.split(' ')[0] if ' ' in away_team else away_team)}</span>
                        <span class="team-name-sub">{html.escape(away_team.split(' ', 1)[1] if ' ' in away_team else '')}</span>
                    </div>
                </td>
                <td class="score-cell">
                    {away_score}{' <span class="winner-indicator">▶</span>' if winner == 'away' else ''}
                </td>
                <td class="video-cell" rowspan="2">
                    <div class="vs-container">
                        <img src="{html.escape(away_logo)}" alt="{html.escape(away_team)} logo" class="vs-logo">
                        <span class="vs-text">VS</span>
                        <img src="{html.escape(home_logo)}" alt="{html.escape(home_team)} logo" class="vs-logo">
                    </div>
                    <div class="recap-text">Recap</div>
                    {f'<a href="{html.escape(highlight_video_url)}" target="_blank" class="video-link">Watch Highlights</a>' if highlight_video_url else ''}
                </td>
            </tr>
            <tr>
                <td class="team-cell">
                    <img src="{html.escape(home_logo)}" alt="{html.escape(home_team)} logo" class="team-logo">
                    <div class="team-name">
                        <span class="team-name-main">{html.escape(home_team.split(' ')[0] if ' ' in home_team else home_team)}</span>
                        <span class="team-name-sub">{html.escape(home_team.split(' ', 1)[1] if ' ' in home_team else '')}</span>
                    </div>
                </td>
                <td class="score-cell">
                    {home_score}{' <span class="winner-indicator">▶</span>' if winner == 'home' else ''}
                </td>
            </tr>
        </table>
        """
        
        return gr.HTML(html_content)

    except Exception as e:
        print(f"Error creating game recap component: {str(e)}")
        # Return a simple error message component
        return gr.HTML("<div style='padding: 1rem; color: red;'>⚠️ Error loading game recap. Please try again later.</div>")

# Function to process a game recap response from the agent
def process_game_recap_response(response):
    """
    Process a response from the agent that may contain game recap data.
    
    Args:
        response (dict): The response from the agent
        
    Returns:
        tuple: (text_output, game_data)
            - text_output (str): The text output to display
            - game_data (dict or None): Game data for the visual component or None
    """
    try:
        # Check if the response has game_data directly
        if isinstance(response, dict) and "game_data" in response:
            return response.get("output", ""), response.get("game_data")
        
        # Check if game data is in intermediate steps (where LangChain often puts tool outputs)
        if isinstance(response, dict) and "intermediate_steps" in response:
            steps = response.get("intermediate_steps", [])
            for step in steps:
                # Check the observation part of the step, which contains the tool output
                if isinstance(step, list) and len(step) >= 2:
                    observation = step[1]  # Second element is typically the observation
                    if isinstance(observation, dict) and "game_data" in observation:
                        return observation.get("output", response.get("output", "")), observation.get("game_data")
                
                # Alternative format where step might be a dict with observation key
                if isinstance(step, dict) and "observation" in step:
                    observation = step["observation"]
                    if isinstance(observation, dict) and "game_data" in observation:
                        return observation.get("output", response.get("output", "")), observation.get("game_data")
        
        # If it's just a text response
        if isinstance(response, str):
            return response, None
        
        # Default case for other response types
        if isinstance(response, dict):
            return response.get("output", ""), None
        
        return str(response), None
        
    except Exception as e:
        print(f"Error processing game recap response: {str(e)}")
        import traceback
        traceback.print_exc()  # Add stack trace for debugging
        return "I encountered an error processing the game data. Please try again.", None

# Test function for running the component directly
if __name__ == "__main__":
    # Create sample game data for testing
    test_game_data = {
        'game_id': 'test-game-123',
        'date': '10/09/2024',
        'location': "Levi's Stadium",
        'home_team': 'San Francisco 49ers',
        'away_team': 'New York Jets',
        'home_score': '32',
        'away_score': '19',
        'result': '32-19',
        'winner': 'home',
        'home_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/sf.png',
        'away_team_logo_url': 'https://a.espncdn.com/i/teamlogos/nfl/500/nyj.png',
        'highlight_video_url': 'https://www.youtube.com/watch?v=igOb4mfV7To'
    }
    
    # Create a test Gradio interface
    with gr.Blocks() as demo:
        gr.Markdown("# Game Recap Component Test")
        
        with gr.Row():
            game_recap = create_game_recap_component(test_game_data)
            
        with gr.Row():
            clear_btn = gr.Button("Clear Component")
            show_btn = gr.Button("Show Component")
        
        clear_btn.click(lambda: None, None, game_recap)
        show_btn.click(lambda: test_game_data, None, game_recap)
    
    demo.launch(share=True)