Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
chat_completion = client.chat.completions.create(
|
| 17 |
messages=[
|
| 18 |
-
{"role": "user", "content": f"
|
| 19 |
],
|
| 20 |
-
model="llama-3.3-70b-versatile",
|
| 21 |
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
summary = chat_completion.choices[0].message.content.strip()
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
return
|
| 31 |
|
| 32 |
-
#
|
| 33 |
def create_gradio_interface():
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
-
gr.Markdown("
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 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()
|