Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,43 +25,66 @@ rapid_api_key=os.environ["RAPID_KEY"]
|
|
| 25 |
|
| 26 |
|
| 27 |
@tool
|
| 28 |
-
def get_football_results(
|
| 29 |
-
|
| 30 |
-
"""A tool that connect to an API that provides live football data and compares the stats of two teams in the English Premier League, then it returns a text response
|
| 31 |
-
where you should look for those football teams given, returns a comparison of stats including a funny reflection on the comparison.
|
| 32 |
Args:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
"""
|
| 36 |
-
url = "https://free-api-live-football-data.p.rapidapi.com/football-get-standing-home"
|
| 37 |
|
|
|
|
|
|
|
| 38 |
querystring = {"leagueid": "47"} # British Premier League
|
| 39 |
-
|
| 40 |
headers = {
|
| 41 |
"x-rapidapi-key": rapid_api_key,
|
| 42 |
"x-rapidapi-host": "free-api-live-football-data.p.rapidapi.com"
|
| 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 |
@tool
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
@tool
|
| 28 |
+
def get_football_results(team1: str, team2: str) -> str:
|
| 29 |
+
"""A tool that connects to an API that provides live football data and compares the stats of two teams in the English Premier League, then uses an LLM to generate a reflection on how both teams stack up.
|
|
|
|
|
|
|
| 30 |
Args:
|
| 31 |
+
team1: The first football team to check
|
| 32 |
+
team2: The second football team to check
|
| 33 |
"""
|
|
|
|
| 34 |
|
| 35 |
+
# First attempt to get actual data from the API
|
| 36 |
+
url = "https://free-api-live-football-data.p.rapidapi.com/football-get-standing-home"
|
| 37 |
querystring = {"leagueid": "47"} # British Premier League
|
|
|
|
| 38 |
headers = {
|
| 39 |
"x-rapidapi-key": rapid_api_key,
|
| 40 |
"x-rapidapi-host": "free-api-live-football-data.p.rapidapi.com"
|
| 41 |
}
|
| 42 |
|
| 43 |
+
# Try to get the data from the API
|
| 44 |
+
try:
|
| 45 |
+
response = requests.get(url, headers=headers, params=querystring)
|
| 46 |
+
response_text = response.text # Get the raw response as text
|
| 47 |
+
|
| 48 |
+
# Load the TinyLlama model for analysis
|
| 49 |
+
tiny_llama = pipeline("text-generation",
|
| 50 |
+
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 51 |
+
torch_dtype=torch.bfloat16,
|
| 52 |
+
device_map="auto")
|
| 53 |
+
|
| 54 |
+
# Create the prompt for the LLM
|
| 55 |
+
messages = [
|
| 56 |
+
{
|
| 57 |
+
"role": "system",
|
| 58 |
+
"content": f"You are a friendly AI system embedded in a wider AI agent. You are going to facilitate the work of other LLMs. You have received from user an API response in JSON format. In the response item 28 there is a long text starting with a text like 'status:success,response...You are going to extract the team names from the response and compare the stats of the two teams. First team check is {team1} and second team is {team2}. Provide a funny reflection on your analysis of how these two teams stack up against each other. You should provide a text response"
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"role": "user",
|
| 62 |
+
"content": response_text
|
| 63 |
+
}
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# Apply chat template and generate response
|
| 67 |
+
prompt = tiny_llama.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 68 |
+
output = tiny_llama(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
|
| 69 |
+
|
| 70 |
+
# Extract the generated text
|
| 71 |
+
comparison = output[0]['generated_text']
|
| 72 |
+
|
| 73 |
+
# Return the comparison as is (it's already a string)
|
| 74 |
+
return comparison
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
# Handle errors gracefully
|
| 78 |
+
return f"""
|
| 79 |
+
## {team1} vs {team2} Premier League Comparison
|
| 80 |
+
|
| 81 |
+
I couldn't complete the analysis due to an error: {str(e)}
|
| 82 |
+
|
| 83 |
+
Let me provide some general information instead:
|
| 84 |
+
|
| 85 |
+
Both {team1} and {team2} are prominent teams in the English Premier League. To get a detailed comparison with current statistics, please try again later when the API and LLM processing are working properly.
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
|
| 89 |
|
| 90 |
@tool
|