Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import requests
|
4 |
+
from deep_translator import GoogleTranslator
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Set up OpenAI API key from environment variables
|
8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
+
|
10 |
+
# MLB API endpoint (mocked, replace with actual MLB API endpoint)
|
11 |
+
MLB_API_URL = "https://api.mlb.com/v1/games/" # Example; replace with actual URL
|
12 |
+
|
13 |
+
def get_highlight_summary(team, player, lang='en'):
|
14 |
+
# Fetch game data from MLB API (this is a mockup)
|
15 |
+
highlight = f"Highlight of {player} from {team} in the latest game..."
|
16 |
+
|
17 |
+
# Generate a summary with OpenAI API
|
18 |
+
response = openai.Completion.create(
|
19 |
+
engine="text-davinci-003",
|
20 |
+
prompt=f"Generate a highlight summary for: {highlight}",
|
21 |
+
max_tokens=100
|
22 |
+
)
|
23 |
+
summary = response.choices[0].text.strip()
|
24 |
+
|
25 |
+
# Translate summary if necessary
|
26 |
+
if lang != 'en':
|
27 |
+
summary = GoogleTranslator(source='auto', target=lang).translate(summary)
|
28 |
+
|
29 |
+
return summary
|
30 |
+
|
31 |
+
# Create Gradio Interface
|
32 |
+
def create_gradio_interface():
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown("### Personalized MLB Highlights System")
|
35 |
+
|
36 |
+
team_input = gr.Textbox(label="Enter your favorite team", placeholder="e.g., New York Yankees")
|
37 |
+
player_input = gr.Textbox(label="Enter your favorite player", placeholder="e.g., Aaron Judge")
|
38 |
+
lang_input = gr.Dropdown(choices=["en", "es", "ja"], label="Select language", value="en")
|
39 |
+
|
40 |
+
output = gr.Textbox(label="Your Highlight Summary")
|
41 |
+
|
42 |
+
submit_button = gr.Button("Generate Highlight")
|
43 |
+
submit_button.click(get_highlight_summary, inputs=[team_input, player_input, lang_input], outputs=output)
|
44 |
+
|
45 |
+
demo.launch()
|
46 |
+
|
47 |
+
# Start the Gradio interface
|
48 |
+
create_gradio_interface()
|