ans123 commited on
Commit
eda8825
·
verified ·
1 Parent(s): b352729

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -23
app.py CHANGED
@@ -8,42 +8,92 @@ client = Groq(
8
  api_key=os.getenv("GROQ_API_KEY"), # Ensure you add this key to your environment variables
9
  )
10
 
11
- def get_highlight_summary(team, player, lang='en'):
12
- # Generate the highlight content
13
- highlight = f"Highlight of {player} from {team} in the latest game..."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Call Groq API for chat completions
 
 
 
16
  chat_completion = client.chat.completions.create(
17
  messages=[
18
- {"role": "user", "content": f"Generate a highlight summary for: {highlight}"}
19
  ],
20
- model="llama-3.3-70b-versatile", # Specify the model to use
21
  )
22
 
23
- # Extract the generated summary
24
- summary = chat_completion.choices[0].message.content.strip()
25
 
26
- # Translate summary if necessary
27
- if lang != 'en':
28
- summary = GoogleTranslator(source='auto', target=lang).translate(summary)
 
 
 
 
 
29
 
30
- return summary
31
 
32
- # Create Gradio Interface
33
  def create_gradio_interface():
34
  with gr.Blocks() as demo:
35
- gr.Markdown("### Personalized MLB Highlights System")
36
 
37
- team_input = gr.Textbox(label="Enter your favorite team", placeholder="e.g., New York Yankees")
38
- player_input = gr.Textbox(label="Enter your favorite player", placeholder="e.g., Aaron Judge")
39
- lang_input = gr.Dropdown(choices=["en", "es", "ja"], label="Select language", value="en")
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- output = gr.Textbox(label="Your Highlight Summary")
 
 
 
 
 
42
 
43
- submit_button = gr.Button("Generate Highlight")
44
- submit_button.click(get_highlight_summary, inputs=[team_input, player_input, lang_input], outputs=output)
45
-
46
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- # Start the Gradio interface
49
  create_gradio_interface()
 
8
  api_key=os.getenv("GROQ_API_KEY"), # Ensure you add this key to your environment variables
9
  )
10
 
11
+ # Function to fetch team and player list from LLM (using Groq API)
12
+ def get_team_and_players():
13
+ chat_completion = client.chat.completions.create(
14
+ messages=[
15
+ {"role": "user", "content": "List all MLB teams and their top players."}
16
+ ],
17
+ model="llama-3.3-70b-versatile", # You can adjust the model here
18
+ )
19
+
20
+ return chat_completion.choices[0].message.content.strip()
21
+
22
+ # Function to generate team overview
23
+ def get_team_overview(team):
24
+ chat_completion = client.chat.completions.create(
25
+ messages=[
26
+ {"role": "user", "content": f"Provide an overview of the {team} MLB team, including recent performance and standings."}
27
+ ],
28
+ model="llama-3.3-70b-versatile",
29
+ )
30
 
31
+ return chat_completion.choices[0].message.content.strip()
32
+
33
+ # Function for season predictions
34
+ def predict_season_outcomes(team):
35
  chat_completion = client.chat.completions.create(
36
  messages=[
37
+ {"role": "user", "content": f"Predict the potential season outcomes for the {team} based on their current performance."}
38
  ],
39
+ model="llama-3.3-70b-versatile",
40
  )
41
 
42
+ return chat_completion.choices[0].message.content.strip()
 
43
 
44
+ # Function for real-time strategy insights
45
+ def real_time_tooltips(game_event):
46
+ chat_completion = client.chat.completions.create(
47
+ messages=[
48
+ {"role": "user", "content": f"Explain the strategy behind the following baseball play: {game_event}"}
49
+ ],
50
+ model="llama-3.3-70b-versatile",
51
+ )
52
 
53
+ return chat_completion.choices[0].message.content.strip()
54
 
55
+ # Gradio app interface
56
  def create_gradio_interface():
57
  with gr.Blocks() as demo:
58
+ gr.Markdown("# MLB Fan Engagement App")
59
 
60
+ with gr.Tab("Team and Player Selection"):
61
+ team_dropdown = gr.Dropdown(choices=[], label="Select Team")
62
+ player_dropdown = gr.Dropdown(choices=[], label="Select Player")
63
+
64
+ # Update teams and players dynamically using LLM
65
+ def update_teams_and_players():
66
+ team_and_players = get_team_and_players()
67
+ teams = team_and_players.split("\n") # Assuming each team is on a new line
68
+ team_dropdown.update(choices=teams)
69
+ return team_dropdown, player_dropdown
70
+
71
+ # Load teams and players when app starts
72
+ update_teams_and_players_button = gr.Button("Load Teams and Players")
73
+ update_teams_and_players_button.click(update_teams_and_players, inputs=[], outputs=[team_dropdown, player_dropdown])
74
 
75
+ with gr.Tab("Team Overview"):
76
+ team_dropdown_overview = gr.Dropdown(choices=[], label="Select Team")
77
+ overview_output = gr.Textbox(label="Team Overview")
78
+ team_dropdown_overview.change(
79
+ get_team_overview, inputs=team_dropdown_overview, outputs=overview_output
80
+ )
81
 
82
+ with gr.Tab("Season Predictions"):
83
+ team_dropdown_predictions = gr.Dropdown(choices=[], label="Select Team")
84
+ predictions_output = gr.Textbox(label="Season Predictions")
85
+ team_dropdown_predictions.change(
86
+ predict_season_outcomes, inputs=team_dropdown_predictions, outputs=predictions_output
87
+ )
88
+
89
+ with gr.Tab("Real-Time Strategy Insights"):
90
+ game_event_input = gr.Textbox(
91
+ label="Describe the game event (e.g., 'Why did the batter bunt in the 8th inning?')"
92
+ )
93
+ strategy_output = gr.Textbox(label="Strategy Explanation")
94
+ game_event_input.submit(real_time_tooltips, inputs=game_event_input, outputs=strategy_output)
95
+
96
+ demo.launch()
97
 
98
+ # Run the app
99
  create_gradio_interface()