File size: 4,953 Bytes
395f1d2
 
0e6321c
127b405
395f1d2
127b405
 
 
 
395f1d2
1e4c863
 
eda8825
 
1e4c863
eda8825
1e4c863
eda8825
 
 
1e4c863
 
eda8825
 
1e4c863
eda8825
 
 
 
 
1e4c863
 
127b405
 
1e4c863
127b405
eda8825
395f1d2
eda8825
395f1d2
eda8825
 
 
 
 
 
 
 
 
395f1d2
0e6321c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda8825
395f1d2
 
0e6321c
395f1d2
eda8825
1e4c863
 
0e6321c
 
1e4c863
0e6321c
 
 
 
 
395f1d2
eda8825
1e4c863
eda8825
0e6321c
 
1e4c863
0e6321c
 
 
 
 
1e4c863
 
 
 
0e6321c
 
1e4c863
0e6321c
 
 
 
 
eda8825
 
 
 
 
 
 
 
 
395f1d2
eda8825
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import gradio as gr
import os
import requests
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
)

# Function to fetch team overview
def get_team_overview(team):
    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",
    )
    return chat_completion.choices[0].message.content.strip()

# Function to predict season outcomes
def predict_season_outcomes(team):
    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",
    )
    return chat_completion.choices[0].message.content.strip()

# Function for player wildcards
def get_player_wildcards(player):
    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",
    )
    return chat_completion.choices[0].message.content.strip()

# Function for real-time strategy insights
def real_time_tooltips(game_event):
    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",
    )
    return chat_completion.choices[0].message.content.strip()

# Function to fetch images and news for a team or player
def fetch_news(query):
    url = "https://www.searchapi.io/api/v1/search"
    params = {
        "engine": "google",
        "q": query,
        "api_key": "PMjgK27a8Lyb6uMXP2jnSNnB",  # Replace with your API key
    }
    try:
        response = requests.get(url, params=params)
        data = response.json()
        
        # Extract news articles
        news = "\n".join([f"{item['title']}: {item['link']}" for item in data.get("items", [])[:5]])
        
        # Extract image URLs
        images = [item["image"] for item in data.get("items", []) if "image" in item][:5]
        
        return news, images
    except Exception as e:
        return f"Error fetching data: {e}", []

# Gradio app interface
def create_gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# Enhanced MLB Fan Engagement App with News and Images")
        
        with gr.Tab("Team Overview"):
            team_input = gr.Textbox(label="Enter Team Name")
            team_output = gr.Textbox(label="Team Overview")
            team_news_output = gr.Textbox(label="Team News")
            team_image_output = gr.Gallery(label="Team Images")
            team_input.submit(get_team_overview, inputs=team_input, outputs=team_output)
            team_input.submit(
                fetch_news_images,
                inputs=team_input,
                outputs=[team_news_output, team_image_output],
            )
        
        with gr.Tab("Season Predictions"):
            team_input_pred = gr.Textbox(label="Enter Team Name")
            predictions_output = gr.Textbox(label="Season Predictions")
            team_news_output_pred = gr.Textbox(label="Team News")
            team_image_output_pred = gr.Gallery(label="Team gallery")
            team_input_pred.submit(predict_season_outcomes, inputs=team_input_pred, outputs=predictions_output)
            team_input_pred.submit(
                fetch_news_images,
                inputs=team_input_pred,
                outputs=[team_news_output_pred, team_image_output_pred],
            )
        
        with gr.Tab("Player Wildcards"):
            player_input = gr.Textbox(label="Enter Player Name")
            player_output = gr.Textbox(label="Player Highlights")
            player_news_output = gr.Textbox(label="Player News")
            player_image_output = gr.Gallery(label="Player Gallery")
            player_input.submit(get_player_wildcards, inputs=player_input, outputs=player_output)
            player_input.submit(
                fetch_news_images,
                inputs=player_input,
                outputs=[player_news_output, player_image_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?')"
            )
            strategy_output = gr.Textbox(label="Strategy Explanation")
            game_event_input.submit(real_time_tooltips, inputs=game_event_input, outputs=strategy_output)
        
        demo.launch()

# Run the app
create_gradio_interface()