MLB-Fanbase / app.py
ans123's picture
Update app.py
d88b8cd verified
import gradio as gr
import os
from groq import Groq
# Set up Groq API client
client = Groq(
api_key=os.getenv("GROQ_API_KEY"), # Ensure you add this key to your environment variables
)
# Supported languages
LANGUAGES = {
"English": "en",
"Spanish": "es",
"Japanese": "ja",
"Urdu": "ur",
}
# Function to translate text
def translate_text(text, language_code):
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": f"Translate this text to {language_code}: {text}"}
],
model="llama-3.3-70b-versatile",
)
return chat_completion.choices[0].message.content.strip()
# Function to fetch team overview
def get_team_overview(team, language):
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": f"Provide an overview of the {team} MLB team, including recent performance and standings."}
],
model="llama-3.3-70b-versatile",
)
result = chat_completion.choices[0].message.content.strip()
return translate_text(result, LANGUAGES[language])
# Function to predict season outcomes
def predict_season_outcomes(team, language):
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": f"Predict the potential season outcomes for the {team} based on their current performance."}
],
model="llama-3.3-70b-versatile",
)
result = chat_completion.choices[0].message.content.strip()
return translate_text(result, LANGUAGES[language])
# Function for player wildcards
def get_player_wildcards(player, language):
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": f"Describe any standout performances or recent achievements for the player {player} in MLB."}
],
model="llama-3.3-70b-versatile",
)
result = chat_completion.choices[0].message.content.strip()
return translate_text(result, LANGUAGES[language])
# Function for real-time strategy insights
def real_time_tooltips(game_event, language):
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": f"Explain the strategy behind the following baseball play: {game_event}"}
],
model="llama-3.3-70b-versatile",
)
result = chat_completion.choices[0].message.content.strip()
return translate_text(result, LANGUAGES[language])
# Gradio app interface
def create_gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("#MLB Fantasy World")
with gr.Tab("Team Overview"):
team_input = gr.Textbox(label="Enter Team Name")
language_selector = gr.Dropdown(
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
)
team_output = gr.Textbox(label="Team Overview")
team_input.submit(
get_team_overview, inputs=[team_input, language_selector], outputs=team_output
)
with gr.Tab("Season Predictions"):
team_input_pred = gr.Textbox(label="Enter Team Name")
language_selector_pred = gr.Dropdown(
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
)
predictions_output = gr.Textbox(label="Season Predictions")
team_input_pred.submit(
predict_season_outcomes,
inputs=[team_input_pred, language_selector_pred],
outputs=predictions_output,
)
with gr.Tab("Player Wildcards"):
player_input = gr.Textbox(label="Enter Player Name")
language_selector_player = gr.Dropdown(
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
)
player_output = gr.Textbox(label="Player Highlights")
player_input.submit(
get_player_wildcards,
inputs=[player_input, language_selector_player],
outputs=player_output,
)
with gr.Tab("Real-Time Strategy Insights"):
game_event_input = gr.Textbox(
label="Describe the game event (e.g., 'Why did the batter bunt in the 8th inning?')"
)
language_selector_event = gr.Dropdown(
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
)
strategy_output = gr.Textbox(label="Strategy Explanation")
game_event_input.submit(
real_time_tooltips,
inputs=[game_event_input, language_selector_event],
outputs=strategy_output,
)
demo.launch()
# Run the app
create_gradio_interface()