Alejadro Sanchez-Giraldo commited on
Commit
86e230c
·
1 Parent(s): db194db

push to origin

Browse files
Files changed (6) hide show
  1. .gitignore +5 -0
  2. README.md +36 -1
  3. app.py +11 -59
  4. fpl_client.py +75 -0
  5. nlp_utils.py +40 -0
  6. requirements.txt +4 -1
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fplenv/
2
+ .DS_Store
3
+ .env
4
+
5
+ __pycache__/
README.md CHANGED
@@ -10,4 +10,39 @@ pinned: false
10
  license: mit
11
  ---
12
 
13
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  license: mit
11
  ---
12
 
13
+
14
+ # FPL Chatbot
15
+
16
+ This is a chatbot that interacts with the Fantasy Premier League (FPL) API to provide information about players, teams, and stats.
17
+
18
+ ```bash
19
+ python3 -m venv fplenv
20
+ source fplenv/bin/activate
21
+
22
+ ```
23
+
24
+ ## Installation
25
+
26
+ 1. Clone the repository:
27
+
28
+ ```bash
29
+ git clone https://github.com/yourusername/fpl_chatbot.git
30
+ cd fplChatbot
31
+ ```
32
+
33
+ 2. Install the dependencies:
34
+
35
+ ```bash
36
+ pip install -r requirements.txt
37
+ ```
38
+
39
+ 3. Run the chatbot:
40
+
41
+ ```bash
42
+ python app.py
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ - Ask about a player's stats: "Tell me about Harry Kane's stats."
48
+ - Ask about a team: "Show me all players from Manchester United."
app.py CHANGED
@@ -1,63 +1,15 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
  import gradio as gr
2
+ from fpl_client import FPLClient
3
+ from nlp_utils import process_query
4
 
5
+ # Initialize the FPL client
6
+ fpl_client = FPLClient()
 
 
7
 
8
+ # Function to handle user input and generate a response
9
+ def chatbot_response(query):
10
+ response = process_query(query, fpl_client)
11
+ return response
12
 
13
+ # Set up the Gradio interface
14
+ iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text", title="FPL Chatbot")
15
+ iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fpl_client.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import logging
3
+
4
+ # Configure logging
5
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
6
+
7
+ class FPLClient:
8
+ def __init__(self):
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}")
15
+ response = requests.get(url)
16
+ if response.status_code == 200:
17
+ logging.debug("Static data fetched successfully.")
18
+ return response.json()
19
+ else:
20
+ logging.error(f"Failed to fetch static data. Status code: {response.status_code}")
21
+ return None
22
+
23
+ def get_teams(self):
24
+ teams = self.data.get('teams', [])
25
+ logging.info(f"Fetching team names. Total teams: {len(teams)}")
26
+ return [team['name'] for team in teams]
27
+
28
+ def get_player_info(self, player_name):
29
+ logging.info(f"Searching for player: {player_name}")
30
+ players = self.data['elements']
31
+ player = next((p for p in players if p['web_name'].lower() == player_name.lower()), None)
32
+ if player:
33
+ logging.debug(f"Player found: {player['web_name']}")
34
+ else:
35
+ logging.warning(f"Player not found: {player_name}")
36
+ return player
37
+
38
+ def get_team_info(self, team_id):
39
+ logging.info(f"Fetching team info for team ID: {team_id}")
40
+ teams = self.data['teams']
41
+ team = next((t for t in teams if t['id'] == team_id), None)
42
+ if team:
43
+ logging.debug(f"Team found: {team['name']}")
44
+ else:
45
+ logging.warning(f"Team not found for ID: {team_id}")
46
+ return team
47
+
48
+ def get_player_stats(self, player_name):
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": player['element_type'],
59
+ "Selected By": player['selected_by_percent'],
60
+ }
61
+ logging.debug(f"Stats for player {player_name}: {stats}")
62
+ return stats
63
+ logging.warning(f"Stats not found for player: {player_name}")
64
+ return "Player not found"
65
+
66
+ def get_team_players(self, team_name):
67
+ logging.info(f"Fetching players for team: {team_name}")
68
+ teams = self.data['teams']
69
+ team = next((t for t in teams if t['name'].lower() == team_name.lower()), None)
70
+ if team:
71
+ players = [p['web_name'] for p in self.data['elements'] if p['team'] == team['id']]
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"
nlp_utils.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from fpl_client import FPLClient
3
+ import logging
4
+
5
+ # Initialize the FPL client
6
+ fpl_client = FPLClient()
7
+
8
+ # Get the list of teams
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
+ match = re.search(r"(\b[A-Z][a-z]+(?:\s[A-Z][a-z]+)?)", query)
14
+ return match.group(1) if match else None
15
+
16
+ def extract_team_name(query):
17
+ logging.info(f"Extracting team name from query: {query}")
18
+ logging.info(f"Teams: {teams}")
19
+ # Add your logic to extract team names, could use a list of teams
20
+ for team in teams:
21
+ if team.lower() in query.lower():
22
+ return team
23
+ return None
24
+
25
+ def process_query(query, fpl_client):
26
+ if "stats" in query.lower():
27
+ player_name = extract_player_name(query)
28
+ if player_name:
29
+ stats = fpl_client.get_player_stats(player_name)
30
+ return stats
31
+ return "Player name not found in the query."
32
+
33
+ if "team" in query.lower():
34
+ team_name = extract_team_name(query)
35
+ if team_name:
36
+ players = fpl_client.get_team_players(team_name)
37
+ return f"Players from {team_name}: {', '.join(players)}"
38
+ return "Team name not found in the query, please use from the list: " + ", ".join(teams)
39
+
40
+ return "I'm not sure how to help with that."
requirements.txt CHANGED
@@ -1 +1,4 @@
1
- huggingface_hub==0.22.2
 
 
 
 
1
+ huggingface_hub==0.22.2
2
+ gradio
3
+ requests
4
+ transformers