Update app.py
Browse files
app.py
CHANGED
|
@@ -114,9 +114,21 @@ def soccer_predictions():
|
|
| 114 |
home_news = get_team_news(home, "football")
|
| 115 |
away_news = get_team_news(away, "football")
|
| 116 |
|
|
|
|
| 117 |
labels = ["positive", "negative", "injury", "transfer", "motivation"]
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
emb_home = similarity_model.encode(home + " " + home_news, convert_to_tensor=True)
|
| 122 |
emb_away = similarity_model.encode(away + " " + away_news, convert_to_tensor=True)
|
|
@@ -158,13 +170,32 @@ def test_api_keys():
|
|
| 158 |
|
| 159 |
if FOOTBALL_API_KEY != "FOOTBALL_API_KEY":
|
| 160 |
try:
|
|
|
|
|
|
|
| 161 |
headers = {"X-Auth-Token": FOOTBALL_API_KEY}
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
except Exception as e:
|
| 169 |
results["football_api"] = {"status": "error", "error": str(e)}
|
| 170 |
else:
|
|
@@ -172,11 +203,23 @@ def test_api_keys():
|
|
| 172 |
|
| 173 |
# Test NBA API
|
| 174 |
try:
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
except Exception as e:
|
| 181 |
results["nba_api"] = {"status": "error", "error": str(e)}
|
| 182 |
|
|
@@ -244,9 +287,21 @@ def nba_predictions():
|
|
| 244 |
home_news = get_team_news(home, "NBA")
|
| 245 |
away_news = get_team_news(away, "NBA")
|
| 246 |
|
|
|
|
| 247 |
labels = ["positive", "negative", "injury", "motivation"]
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
context = f"""
|
| 252 |
Match: {home} vs {away}.
|
|
|
|
| 114 |
home_news = get_team_news(home, "football")
|
| 115 |
away_news = get_team_news(away, "football")
|
| 116 |
|
| 117 |
+
# Ensure we have valid news text for sentiment analysis
|
| 118 |
labels = ["positive", "negative", "injury", "transfer", "motivation"]
|
| 119 |
+
|
| 120 |
+
# Use fallback text if news is empty
|
| 121 |
+
home_news_text = home_news.strip() if home_news.strip() else f"Recent news about {home} football team"
|
| 122 |
+
away_news_text = away_news.strip() if away_news.strip() else f"Recent news about {away} football team"
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
home_sent = sentiment_model(home_news_text, labels)
|
| 126 |
+
away_sent = sentiment_model(away_news_text, labels)
|
| 127 |
+
except Exception as e:
|
| 128 |
+
# Fallback sentiment analysis with default text
|
| 129 |
+
default_text = f"Football team {home} vs {away} match analysis"
|
| 130 |
+
home_sent = sentiment_model(default_text, labels)
|
| 131 |
+
away_sent = sentiment_model(default_text, labels)
|
| 132 |
|
| 133 |
emb_home = similarity_model.encode(home + " " + home_news, convert_to_tensor=True)
|
| 134 |
emb_away = similarity_model.encode(away + " " + away_news, convert_to_tensor=True)
|
|
|
|
| 170 |
|
| 171 |
if FOOTBALL_API_KEY != "FOOTBALL_API_KEY":
|
| 172 |
try:
|
| 173 |
+
from datetime import timedelta
|
| 174 |
+
|
| 175 |
headers = {"X-Auth-Token": FOOTBALL_API_KEY}
|
| 176 |
+
|
| 177 |
+
# Use the same date range that works (5 days back + 5 days forward)
|
| 178 |
+
today = date.today()
|
| 179 |
+
date_from = (today - timedelta(days=5)).isoformat()
|
| 180 |
+
date_to = (today + timedelta(days=5)).isoformat()
|
| 181 |
+
|
| 182 |
+
resp = requests.get(f"{FOOTBALL_ENDPOINT}?competitions=2021&dateFrom={date_from}&dateTo={date_to}", headers=headers)
|
| 183 |
+
|
| 184 |
+
if resp.status_code == 200:
|
| 185 |
+
data = resp.json()
|
| 186 |
+
matches = data.get("matches", [])
|
| 187 |
+
results["football_api"] = {
|
| 188 |
+
"status": "working",
|
| 189 |
+
"status_code": resp.status_code,
|
| 190 |
+
"matches_found": len(matches),
|
| 191 |
+
"date_range": f"{date_from} to {date_to}"
|
| 192 |
+
}
|
| 193 |
+
else:
|
| 194 |
+
results["football_api"] = {
|
| 195 |
+
"status": "error",
|
| 196 |
+
"status_code": resp.status_code,
|
| 197 |
+
"error": resp.text[:200]
|
| 198 |
+
}
|
| 199 |
except Exception as e:
|
| 200 |
results["football_api"] = {"status": "error", "error": str(e)}
|
| 201 |
else:
|
|
|
|
| 203 |
|
| 204 |
# Test NBA API
|
| 205 |
try:
|
| 206 |
+
today = date.today().isoformat()
|
| 207 |
+
games_response = nba_api.nba.games.list(dates=[today])
|
| 208 |
+
|
| 209 |
+
# Handle the PaginatedListResponse object properly
|
| 210 |
+
if hasattr(games_response, 'data'):
|
| 211 |
+
games = games_response.data
|
| 212 |
+
results["nba_api"] = {
|
| 213 |
+
"status": "working",
|
| 214 |
+
"games_found": len(games),
|
| 215 |
+
"date_tested": today
|
| 216 |
+
}
|
| 217 |
+
else:
|
| 218 |
+
results["nba_api"] = {
|
| 219 |
+
"status": "error",
|
| 220 |
+
"error": "Unexpected response format",
|
| 221 |
+
"response_type": str(type(games_response))
|
| 222 |
+
}
|
| 223 |
except Exception as e:
|
| 224 |
results["nba_api"] = {"status": "error", "error": str(e)}
|
| 225 |
|
|
|
|
| 287 |
home_news = get_team_news(home, "NBA")
|
| 288 |
away_news = get_team_news(away, "NBA")
|
| 289 |
|
| 290 |
+
# Ensure we have valid news text for sentiment analysis
|
| 291 |
labels = ["positive", "negative", "injury", "motivation"]
|
| 292 |
+
|
| 293 |
+
# Use fallback text if news is empty
|
| 294 |
+
home_news_text = home_news.strip() if home_news.strip() else f"Recent news about {home} NBA team"
|
| 295 |
+
away_news_text = away_news.strip() if away_news.strip() else f"Recent news about {away} NBA team"
|
| 296 |
+
|
| 297 |
+
try:
|
| 298 |
+
home_sent = sentiment_model(home_news_text, labels)
|
| 299 |
+
away_sent = sentiment_model(away_news_text, labels)
|
| 300 |
+
except Exception as e:
|
| 301 |
+
# Fallback sentiment analysis with default text
|
| 302 |
+
default_text = f"NBA team {home} vs {away} game analysis"
|
| 303 |
+
home_sent = sentiment_model(default_text, labels)
|
| 304 |
+
away_sent = sentiment_model(default_text, labels)
|
| 305 |
|
| 306 |
context = f"""
|
| 307 |
Match: {home} vs {away}.
|