Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import requests | |
from deep_translator import GoogleTranslator | |
import os | |
# Set up OpenAI API key from environment variables | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
# MLB API endpoint (mocked, replace with actual MLB API endpoint) | |
MLB_API_URL = "https://api.mlb.com/v1/games/" # Example; replace with actual URL | |
def get_highlight_summary(team, player, lang='en'): | |
# Fetch game data from MLB API (this is a mockup) | |
highlight = f"Highlight of {player} from {team} in the latest game..." | |
# Generate a summary with OpenAI API | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=f"Generate a highlight summary for: {highlight}", | |
max_tokens=100 | |
) | |
summary = response.choices[0].text.strip() | |
# Translate summary if necessary | |
if lang != 'en': | |
summary = GoogleTranslator(source='auto', target=lang).translate(summary) | |
return summary | |
# Create Gradio Interface | |
def create_gradio_interface(): | |
with gr.Blocks() as demo: | |
gr.Markdown("### Personalized MLB Highlights System") | |
team_input = gr.Textbox(label="Enter your favorite team", placeholder="e.g., New York Yankees") | |
player_input = gr.Textbox(label="Enter your favorite player", placeholder="e.g., Aaron Judge") | |
lang_input = gr.Dropdown(choices=["en", "es", "ja"], label="Select language", value="en") | |
output = gr.Textbox(label="Your Highlight Summary") | |
submit_button = gr.Button("Generate Highlight") | |
submit_button.click(get_highlight_summary, inputs=[team_input, player_input, lang_input], outputs=output) | |
demo.launch() | |
# Start the Gradio interface | |
create_gradio_interface() | |