Spaces:
Sleeping
Sleeping
File size: 1,752 Bytes
395f1d2 |
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 |
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()
|