Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import requests
|
4 |
from groq import Groq
|
5 |
|
6 |
# Set up Groq API client
|
@@ -8,97 +7,120 @@ client = Groq(
|
|
8 |
api_key=os.getenv("GROQ_API_KEY"), # Ensure you add this key to your environment variables
|
9 |
)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Function to fetch team overview
|
12 |
-
def get_team_overview(team):
|
13 |
chat_completion = client.chat.completions.create(
|
14 |
messages=[
|
15 |
{"role": "user", "content": f"Provide an overview of the {team} MLB team, including recent performance and standings."}
|
16 |
],
|
17 |
model="llama-3.3-70b-versatile",
|
18 |
)
|
19 |
-
|
|
|
20 |
|
21 |
# Function to predict season outcomes
|
22 |
-
def predict_season_outcomes(team):
|
23 |
chat_completion = client.chat.completions.create(
|
24 |
messages=[
|
25 |
{"role": "user", "content": f"Predict the potential season outcomes for the {team} based on their current performance."}
|
26 |
],
|
27 |
model="llama-3.3-70b-versatile",
|
28 |
)
|
29 |
-
|
|
|
30 |
|
31 |
# Function for player wildcards
|
32 |
-
def get_player_wildcards(player):
|
33 |
chat_completion = client.chat.completions.create(
|
34 |
messages=[
|
35 |
{"role": "user", "content": f"Describe any standout performances or recent achievements for the player {player} in MLB."}
|
36 |
],
|
37 |
model="llama-3.3-70b-versatile",
|
38 |
)
|
39 |
-
|
|
|
40 |
|
41 |
# Function for real-time strategy insights
|
42 |
-
def real_time_tooltips(game_event):
|
43 |
chat_completion = client.chat.completions.create(
|
44 |
messages=[
|
45 |
{"role": "user", "content": f"Explain the strategy behind the following baseball play: {game_event}"}
|
46 |
],
|
47 |
model="llama-3.3-70b-versatile",
|
48 |
)
|
49 |
-
|
50 |
-
|
51 |
-
# Function to fetch images or news for a team/player
|
52 |
-
def fetch_news_images(query):
|
53 |
-
url = "https://www.searchapi.io/api/v1/search"
|
54 |
-
params = {
|
55 |
-
"engine": "google",
|
56 |
-
"q": query,
|
57 |
-
"api_key": "PMjgK27a8Lyb6uMXP2jnSNnB", # Replace with your API key
|
58 |
-
}
|
59 |
-
try:
|
60 |
-
response = requests.get(url, params=params)
|
61 |
-
data = response.json()
|
62 |
-
if "items" in data:
|
63 |
-
news = "\n".join([f"{item['title']}: {item['link']}" for item in data["items"][:5]])
|
64 |
-
return news
|
65 |
-
else:
|
66 |
-
return "No news or images found for this query."
|
67 |
-
except Exception as e:
|
68 |
-
return f"Error fetching data: {e}"
|
69 |
|
70 |
# Gradio app interface
|
71 |
def create_gradio_interface():
|
72 |
with gr.Blocks() as demo:
|
73 |
-
gr.Markdown("#
|
74 |
|
75 |
with gr.Tab("Team Overview"):
|
76 |
team_input = gr.Textbox(label="Enter Team Name")
|
|
|
|
|
|
|
77 |
team_output = gr.Textbox(label="Team Overview")
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
with gr.Tab("Season Predictions"):
|
83 |
team_input_pred = gr.Textbox(label="Enter Team Name")
|
|
|
|
|
|
|
84 |
predictions_output = gr.Textbox(label="Season Predictions")
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
88 |
|
89 |
with gr.Tab("Player Wildcards"):
|
90 |
player_input = gr.Textbox(label="Enter Player Name")
|
|
|
|
|
|
|
91 |
player_output = gr.Textbox(label="Player Highlights")
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
95 |
|
96 |
with gr.Tab("Real-Time Strategy Insights"):
|
97 |
game_event_input = gr.Textbox(
|
98 |
label="Describe the game event (e.g., 'Why did the batter bunt in the 8th inning?')"
|
99 |
)
|
|
|
|
|
|
|
100 |
strategy_output = gr.Textbox(label="Strategy Explanation")
|
101 |
-
game_event_input.submit(
|
|
|
|
|
|
|
|
|
102 |
|
103 |
demo.launch()
|
104 |
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
3 |
from groq import Groq
|
4 |
|
5 |
# Set up Groq API client
|
|
|
7 |
api_key=os.getenv("GROQ_API_KEY"), # Ensure you add this key to your environment variables
|
8 |
)
|
9 |
|
10 |
+
# Supported languages
|
11 |
+
LANGUAGES = {
|
12 |
+
"English": "en",
|
13 |
+
"Spanish": "es",
|
14 |
+
"Japanese": "ja",
|
15 |
+
"Urdu": "ur",
|
16 |
+
}
|
17 |
+
|
18 |
+
# Function to translate text
|
19 |
+
def translate_text(text, language_code):
|
20 |
+
chat_completion = client.chat.completions.create(
|
21 |
+
messages=[
|
22 |
+
{"role": "user", "content": f"Translate this text to {language_code}: {text}"}
|
23 |
+
],
|
24 |
+
model="llama-3.3-70b-versatile",
|
25 |
+
)
|
26 |
+
return chat_completion.choices[0].message.content.strip()
|
27 |
+
|
28 |
# Function to fetch team overview
|
29 |
+
def get_team_overview(team, language):
|
30 |
chat_completion = client.chat.completions.create(
|
31 |
messages=[
|
32 |
{"role": "user", "content": f"Provide an overview of the {team} MLB team, including recent performance and standings."}
|
33 |
],
|
34 |
model="llama-3.3-70b-versatile",
|
35 |
)
|
36 |
+
result = chat_completion.choices[0].message.content.strip()
|
37 |
+
return translate_text(result, LANGUAGES[language])
|
38 |
|
39 |
# Function to predict season outcomes
|
40 |
+
def predict_season_outcomes(team, language):
|
41 |
chat_completion = client.chat.completions.create(
|
42 |
messages=[
|
43 |
{"role": "user", "content": f"Predict the potential season outcomes for the {team} based on their current performance."}
|
44 |
],
|
45 |
model="llama-3.3-70b-versatile",
|
46 |
)
|
47 |
+
result = chat_completion.choices[0].message.content.strip()
|
48 |
+
return translate_text(result, LANGUAGES[language])
|
49 |
|
50 |
# Function for player wildcards
|
51 |
+
def get_player_wildcards(player, language):
|
52 |
chat_completion = client.chat.completions.create(
|
53 |
messages=[
|
54 |
{"role": "user", "content": f"Describe any standout performances or recent achievements for the player {player} in MLB."}
|
55 |
],
|
56 |
model="llama-3.3-70b-versatile",
|
57 |
)
|
58 |
+
result = chat_completion.choices[0].message.content.strip()
|
59 |
+
return translate_text(result, LANGUAGES[language])
|
60 |
|
61 |
# Function for real-time strategy insights
|
62 |
+
def real_time_tooltips(game_event, language):
|
63 |
chat_completion = client.chat.completions.create(
|
64 |
messages=[
|
65 |
{"role": "user", "content": f"Explain the strategy behind the following baseball play: {game_event}"}
|
66 |
],
|
67 |
model="llama-3.3-70b-versatile",
|
68 |
)
|
69 |
+
result = chat_completion.choices[0].message.content.strip()
|
70 |
+
return translate_text(result, LANGUAGES[language])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Gradio app interface
|
73 |
def create_gradio_interface():
|
74 |
with gr.Blocks() as demo:
|
75 |
+
gr.Markdown("#MLB Fantasy World")
|
76 |
|
77 |
with gr.Tab("Team Overview"):
|
78 |
team_input = gr.Textbox(label="Enter Team Name")
|
79 |
+
language_selector = gr.Dropdown(
|
80 |
+
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
|
81 |
+
)
|
82 |
team_output = gr.Textbox(label="Team Overview")
|
83 |
+
team_input.submit(
|
84 |
+
get_team_overview, inputs=[team_input, language_selector], outputs=team_output
|
85 |
+
)
|
86 |
|
87 |
with gr.Tab("Season Predictions"):
|
88 |
team_input_pred = gr.Textbox(label="Enter Team Name")
|
89 |
+
language_selector_pred = gr.Dropdown(
|
90 |
+
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
|
91 |
+
)
|
92 |
predictions_output = gr.Textbox(label="Season Predictions")
|
93 |
+
team_input_pred.submit(
|
94 |
+
predict_season_outcomes,
|
95 |
+
inputs=[team_input_pred, language_selector_pred],
|
96 |
+
outputs=predictions_output,
|
97 |
+
)
|
98 |
|
99 |
with gr.Tab("Player Wildcards"):
|
100 |
player_input = gr.Textbox(label="Enter Player Name")
|
101 |
+
language_selector_player = gr.Dropdown(
|
102 |
+
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
|
103 |
+
)
|
104 |
player_output = gr.Textbox(label="Player Highlights")
|
105 |
+
player_input.submit(
|
106 |
+
get_player_wildcards,
|
107 |
+
inputs=[player_input, language_selector_player],
|
108 |
+
outputs=player_output,
|
109 |
+
)
|
110 |
|
111 |
with gr.Tab("Real-Time Strategy Insights"):
|
112 |
game_event_input = gr.Textbox(
|
113 |
label="Describe the game event (e.g., 'Why did the batter bunt in the 8th inning?')"
|
114 |
)
|
115 |
+
language_selector_event = gr.Dropdown(
|
116 |
+
label="Select Language", choices=list(LANGUAGES.keys()), value="English"
|
117 |
+
)
|
118 |
strategy_output = gr.Textbox(label="Strategy Explanation")
|
119 |
+
game_event_input.submit(
|
120 |
+
real_time_tooltips,
|
121 |
+
inputs=[game_event_input, language_selector_event],
|
122 |
+
outputs=strategy_output,
|
123 |
+
)
|
124 |
|
125 |
demo.launch()
|
126 |
|