Spaces:
Runtime error
Runtime error
Alejadro Sanchez-Giraldo
commited on
Commit
·
23152a0
1
Parent(s):
3951dc7
improvements on the qquery and mapp display
Browse files- README.md +1 -2
- app.py +4 -1
- fpl_client.py +15 -2
- nlp_utils.py +12 -2
- player_utils.py +30 -0
README.md
CHANGED
@@ -18,7 +18,6 @@ This is a chatbot that interacts with the Fantasy Premier League (FPL) API to pr
|
|
18 |
```bash
|
19 |
python3 -m venv fplenv
|
20 |
source fplenv/bin/activate
|
21 |
-
|
22 |
```
|
23 |
|
24 |
## Installation
|
@@ -26,7 +25,7 @@ source fplenv/bin/activate
|
|
26 |
1. Clone the repository:
|
27 |
|
28 |
```bash
|
29 |
-
git clone https://github.com/yourusername/
|
30 |
cd fplChatbot
|
31 |
```
|
32 |
|
|
|
18 |
```bash
|
19 |
python3 -m venv fplenv
|
20 |
source fplenv/bin/activate
|
|
|
21 |
```
|
22 |
|
23 |
## Installation
|
|
|
25 |
1. Clone the repository:
|
26 |
|
27 |
```bash
|
28 |
+
git clone https://github.com/yourusername/fplchatbot.git
|
29 |
cd fplChatbot
|
30 |
```
|
31 |
|
app.py
CHANGED
@@ -10,15 +10,18 @@ theme = gr.themes.Soft(
|
|
10 |
neutral_hue="slate",
|
11 |
)
|
12 |
|
13 |
-
|
14 |
# Initialize the FPL client
|
15 |
fpl_client = FPLClient()
|
16 |
|
17 |
# Function to handle user input and generate a response
|
18 |
def chatbot_response(query):
|
19 |
response = process_query(query, fpl_client)
|
|
|
|
|
|
|
20 |
return response
|
21 |
|
|
|
22 |
# Set up the Gradio interface
|
23 |
iface = gr.Interface(
|
24 |
fn=chatbot_response,
|
|
|
10 |
neutral_hue="slate",
|
11 |
)
|
12 |
|
|
|
13 |
# Initialize the FPL client
|
14 |
fpl_client = FPLClient()
|
15 |
|
16 |
# Function to handle user input and generate a response
|
17 |
def chatbot_response(query):
|
18 |
response = process_query(query, fpl_client)
|
19 |
+
# if response if a JSON boject iterate over the elements and conver is a list like "a": "b" "/n" "c": "d"
|
20 |
+
if isinstance(response, dict):
|
21 |
+
response = "\n".join([f"{key}: {value}" for key, value in response.items()])
|
22 |
return response
|
23 |
|
24 |
+
|
25 |
# Set up the Gradio interface
|
26 |
iface = gr.Interface(
|
27 |
fn=chatbot_response,
|
fpl_client.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import requests
|
2 |
import logging
|
3 |
|
|
|
4 |
# Configure logging
|
5 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
6 |
|
@@ -9,6 +10,16 @@ class FPLClient:
|
|
9 |
self.base_url = "https://fantasy.premierleague.com/api"
|
10 |
self.data = self.get_static_data()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def get_static_data(self):
|
13 |
url = f"{self.base_url}/bootstrap-static/"
|
14 |
logging.info(f"Fetching static data from {url}")
|
@@ -49,13 +60,15 @@ class FPLClient:
|
|
49 |
logging.info(f"Fetching stats for player: {player_name}")
|
50 |
player = self.get_player_info(player_name)
|
51 |
if player:
|
|
|
|
|
52 |
team_name = self.get_team_info(player['team'])['name']
|
53 |
stats = {
|
54 |
"Name": player['web_name'],
|
55 |
"Team": team_name,
|
56 |
"Total Points": player['total_points'],
|
57 |
"Price": player['now_cost'] / 10,
|
58 |
-
"Position":
|
59 |
"Selected By": player['selected_by_percent'],
|
60 |
}
|
61 |
logging.debug(f"Stats for player {player_name}: {stats}")
|
@@ -72,4 +85,4 @@ class FPLClient:
|
|
72 |
logging.debug(f"Players found for team {team_name}: {players}")
|
73 |
return players
|
74 |
logging.warning(f"Team not found: {team_name}")
|
75 |
-
return "Team not found"
|
|
|
1 |
import requests
|
2 |
import logging
|
3 |
|
4 |
+
|
5 |
# Configure logging
|
6 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
7 |
|
|
|
10 |
self.base_url = "https://fantasy.premierleague.com/api"
|
11 |
self.data = self.get_static_data()
|
12 |
|
13 |
+
def player_position(self, position_id):
|
14 |
+
logging.info(f"Mapping position ID: {position_id}")
|
15 |
+
positions = {
|
16 |
+
1: 'Goalkeeper',
|
17 |
+
2: 'Defender',
|
18 |
+
3: 'Midfielder',
|
19 |
+
4: 'Forward'
|
20 |
+
}
|
21 |
+
return positions.get(position_id, 'Unknown')
|
22 |
+
|
23 |
def get_static_data(self):
|
24 |
url = f"{self.base_url}/bootstrap-static/"
|
25 |
logging.info(f"Fetching static data from {url}")
|
|
|
60 |
logging.info(f"Fetching stats for player: {player_name}")
|
61 |
player = self.get_player_info(player_name)
|
62 |
if player:
|
63 |
+
logging.info(f"Player position ID: {player['element_type']}")
|
64 |
+
player_pos = self.player_position(player['element_type'])
|
65 |
team_name = self.get_team_info(player['team'])['name']
|
66 |
stats = {
|
67 |
"Name": player['web_name'],
|
68 |
"Team": team_name,
|
69 |
"Total Points": player['total_points'],
|
70 |
"Price": player['now_cost'] / 10,
|
71 |
+
"Position": player_pos,
|
72 |
"Selected By": player['selected_by_percent'],
|
73 |
}
|
74 |
logging.debug(f"Stats for player {player_name}: {stats}")
|
|
|
85 |
logging.debug(f"Players found for team {team_name}: {players}")
|
86 |
return players
|
87 |
logging.warning(f"Team not found: {team_name}")
|
88 |
+
return "Team not found"
|
nlp_utils.py
CHANGED
@@ -9,12 +9,16 @@ fpl_client = FPLClient()
|
|
9 |
teams = fpl_client.get_teams()
|
10 |
|
11 |
def extract_player_name(query):
|
12 |
-
# Simple regex example to extract player names (can be enhanced)
|
13 |
# Updated regex to include accented characters and names with periods
|
14 |
match = re.search(r"(\b[A-Z][a-zA-Z]*[a-zA-Z.]*\b(?:\s[A-Z][a-zA-Z]*[a-zA-Z.]*\b)?)", query, re.UNICODE)
|
15 |
logging.info(f"Extracted player name: {match}")
|
16 |
return match.group(1) if match else None
|
17 |
|
|
|
|
|
|
|
|
|
|
|
18 |
def extract_team_name(query):
|
19 |
logging.info(f"Extracting team name from query: {query}")
|
20 |
logging.debug(f"Teams: {teams}")
|
@@ -29,8 +33,14 @@ def process_query(query, fpl_client):
|
|
29 |
player_name = extract_player_name(query)
|
30 |
if player_name:
|
31 |
stats = fpl_client.get_player_stats(player_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
return stats
|
33 |
-
return
|
34 |
|
35 |
if "team" in query.lower() or "players" in query.lower():
|
36 |
# Extract the team name from the query
|
|
|
9 |
teams = fpl_client.get_teams()
|
10 |
|
11 |
def extract_player_name(query):
|
|
|
12 |
# Updated regex to include accented characters and names with periods
|
13 |
match = re.search(r"(\b[A-Z][a-zA-Z]*[a-zA-Z.]*\b(?:\s[A-Z][a-zA-Z]*[a-zA-Z.]*\b)?)", query, re.UNICODE)
|
14 |
logging.info(f"Extracted player name: {match}")
|
15 |
return match.group(1) if match else None
|
16 |
|
17 |
+
def extract_from_quotes(query):
|
18 |
+
match = re.search(r'"([^"]*)"', query)
|
19 |
+
logging.info(f"Extracted text from quotes: {match}")
|
20 |
+
return match.group(1) if match else None
|
21 |
+
|
22 |
def extract_team_name(query):
|
23 |
logging.info(f"Extracting team name from query: {query}")
|
24 |
logging.debug(f"Teams: {teams}")
|
|
|
33 |
player_name = extract_player_name(query)
|
34 |
if player_name:
|
35 |
stats = fpl_client.get_player_stats(player_name)
|
36 |
+
if stats == "Player not found":
|
37 |
+
logging.info("Player name not found in the query. \nTrying to extract from quotes.")
|
38 |
+
player_name = extract_from_quotes(query)
|
39 |
+
if player_name == None:
|
40 |
+
return 'Player name not found in the query. \nTry "Player-Name" in double-quotes'
|
41 |
+
stats = fpl_client.get_player_stats(player_name)
|
42 |
return stats
|
43 |
+
return 'Player name not found in the query. \nTry "Player-Name" in double-quotes'
|
44 |
|
45 |
if "team" in query.lower() or "players" in query.lower():
|
46 |
# Extract the team name from the query
|
player_utils.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
|
3 |
+
# Example player data
|
4 |
+
# player_data = {
|
5 |
+
# "first_name": "Fábio",
|
6 |
+
# "second_name": "Ferreira Vieira",
|
7 |
+
# "web_name": "Fábio Vieira",
|
8 |
+
# "now_cost": 55,
|
9 |
+
# "status": "d", # Example status: 'd' means doubtful
|
10 |
+
# "total_points": 24,
|
11 |
+
# "team": 1,
|
12 |
+
# "news": "Hip injury - 75% chance of playing",
|
13 |
+
# "goals_scored": 1,
|
14 |
+
# "assists": 3,
|
15 |
+
# "clean_sheets": 1,
|
16 |
+
# "minutes": 290,
|
17 |
+
# "photo": "438098.jpg"
|
18 |
+
# }
|
19 |
+
class FPLPlayer:
|
20 |
+
# Function to map status to a human-readable format and style
|
21 |
+
def get_status_class(status):
|
22 |
+
if status == 'a':
|
23 |
+
return 'Available', 'color: green;'
|
24 |
+
elif status == 'd':
|
25 |
+
return 'Doubtful', 'color: orange;'
|
26 |
+
elif status == 'i':
|
27 |
+
return 'Injured', 'color: red;'
|
28 |
+
else:
|
29 |
+
return 'Unknown', 'color: grey;'
|
30 |
+
|