Update app.py
Browse files
app.py
CHANGED
|
@@ -13,28 +13,6 @@ load_dotenv()
|
|
| 13 |
|
| 14 |
app = FastAPI(title="Sports Predictor", description="AI-powered sports predictions for soccer and NBA")
|
| 15 |
|
| 16 |
-
sentiment_model = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-1")
|
| 17 |
-
reasoning_model = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 18 |
-
similarity_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 19 |
-
|
| 20 |
-
FOOTBALL_API_KEY = os.getenv("FOOTBALL_API_KEY", "YOUR_FOOTBALL_DATA_API_KEY")
|
| 21 |
-
FOOTBALL_ENDPOINT = "https://api.football-data.org/v4/matches"
|
| 22 |
-
NBA_API_KEY = os.getenv("NBA_API_KEY", "your-api-key")
|
| 23 |
-
nba_api = BalldontlieAPI(api_key=NBA_API_KEY)
|
| 24 |
-
|
| 25 |
-
SOCCER_LEAGUES = {
|
| 26 |
-
"EPL": 2021,
|
| 27 |
-
"LaLiga": 2014,
|
| 28 |
-
"Bundesliga": 2002
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
def get_team_news(team: str, sport: str = "football"):
|
| 32 |
-
url = f"https://news.google.com/search?q={team}+{sport}&hl=en"
|
| 33 |
-
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
| 34 |
-
soup = BeautifulSoup(r.text, "html.parser")
|
| 35 |
-
headlines = [h.text for h in soup.select("h3")[:5]]
|
| 36 |
-
return " ".join(headlines)
|
| 37 |
-
|
| 38 |
@app.get("/")
|
| 39 |
def root():
|
| 40 |
return {
|
|
@@ -68,18 +46,67 @@ def root():
|
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
@app.get("/soccer-predictions")
|
| 72 |
def soccer_predictions():
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
predictions = []
|
|
|
|
| 75 |
|
| 76 |
for league, code in SOCCER_LEAGUES.items():
|
| 77 |
headers = {"X-Auth-Token": FOOTBALL_API_KEY}
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
for match in matches:
|
| 85 |
home, away = match["homeTeam"]["name"], match["awayTeam"]["name"]
|
|
@@ -116,13 +143,100 @@ def soccer_predictions():
|
|
| 116 |
}
|
| 117 |
})
|
| 118 |
|
| 119 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
@app.get("/nba-predictions")
|
| 122 |
def nba_predictions():
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
predictions = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
for g in games:
|
| 128 |
home, away = g["home_team"]["full_name"], g["visitor_team"]["full_name"]
|
|
@@ -153,7 +267,12 @@ def nba_predictions():
|
|
| 153 |
}
|
| 154 |
})
|
| 155 |
|
| 156 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 13 |
|
| 14 |
app = FastAPI(title="Sports Predictor", description="AI-powered sports predictions for soccer and NBA")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@app.get("/")
|
| 17 |
def root():
|
| 18 |
return {
|
|
|
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
+
sentiment_model = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-1")
|
| 50 |
+
reasoning_model = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 51 |
+
similarity_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 52 |
+
|
| 53 |
+
FOOTBALL_API_KEY = os.getenv("FOOTBALL_API_KEY", "FOOTBALL_API_KEY")
|
| 54 |
+
FOOTBALL_ENDPOINT = "https://api.football-data.org/v4/matches"
|
| 55 |
+
NBA_API_KEY = os.getenv("NBA_API_KEY", "NBA_API_KEY")
|
| 56 |
+
nba_api = BalldontlieAPI(api_key=NBA_API_KEY)
|
| 57 |
+
|
| 58 |
+
SOCCER_LEAGUES = {
|
| 59 |
+
"EPL": 2021,
|
| 60 |
+
"LaLiga": 2014,
|
| 61 |
+
"Bundesliga": 2002
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
def get_team_news(team: str, sport: str = "football"):
|
| 65 |
+
url = f"https://news.google.com/search?q={team}+{sport}&hl=en"
|
| 66 |
+
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
| 67 |
+
soup = BeautifulSoup(r.text, "html.parser")
|
| 68 |
+
headlines = [h.text for h in soup.select("h3")[:5]]
|
| 69 |
+
return " ".join(headlines)
|
| 70 |
+
|
| 71 |
@app.get("/soccer-predictions")
|
| 72 |
def soccer_predictions():
|
| 73 |
+
from datetime import timedelta
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
today = date.today()
|
| 77 |
+
date_from = (today - timedelta(days=5)).isoformat()
|
| 78 |
+
date_to = (today + timedelta(days=5)).isoformat()
|
| 79 |
+
|
| 80 |
predictions = []
|
| 81 |
+
debug_info = []
|
| 82 |
|
| 83 |
for league, code in SOCCER_LEAGUES.items():
|
| 84 |
headers = {"X-Auth-Token": FOOTBALL_API_KEY}
|
| 85 |
+
try:
|
| 86 |
+
resp = requests.get(
|
| 87 |
+
f"{FOOTBALL_ENDPOINT}?competitions={code}&dateFrom={date_from}&dateTo={date_to}",
|
| 88 |
+
headers=headers
|
| 89 |
+
)
|
| 90 |
+
resp.raise_for_status()
|
| 91 |
+
data = resp.json()
|
| 92 |
+
matches = data.get("matches", [])
|
| 93 |
+
|
| 94 |
+
debug_info.append({
|
| 95 |
+
"league": league,
|
| 96 |
+
"code": code,
|
| 97 |
+
"matches_found": len(matches),
|
| 98 |
+
"api_response_status": resp.status_code,
|
| 99 |
+
"date_range": f"{date_from} to {date_to}"
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
except requests.exceptions.RequestException as e:
|
| 103 |
+
debug_info.append({
|
| 104 |
+
"league": league,
|
| 105 |
+
"code": code,
|
| 106 |
+
"error": str(e),
|
| 107 |
+
"api_response_status": getattr(resp, 'status_code', 'No response')
|
| 108 |
+
})
|
| 109 |
+
continue
|
| 110 |
|
| 111 |
for match in matches:
|
| 112 |
home, away = match["homeTeam"]["name"], match["awayTeam"]["name"]
|
|
|
|
| 143 |
}
|
| 144 |
})
|
| 145 |
|
| 146 |
+
return {
|
| 147 |
+
"date": today,
|
| 148 |
+
"predictions": predictions,
|
| 149 |
+
"debug_info": debug_info,
|
| 150 |
+
"total_predictions": len(predictions)
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
@app.get("/test-api-keys")
|
| 154 |
+
def test_api_keys():
|
| 155 |
+
"""Test endpoint to check if API keys are working"""
|
| 156 |
+
results = {}
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if FOOTBALL_API_KEY != "FOOTBALL_API_KEY":
|
| 160 |
+
try:
|
| 161 |
+
headers = {"X-Auth-Token": FOOTBALL_API_KEY}
|
| 162 |
+
resp = requests.get(f"{FOOTBALL_ENDPOINT}?competitions=2021&dateFrom=2025-01-01&dateTo=2025-01-31", headers=headers)
|
| 163 |
+
results["football_api"] = {
|
| 164 |
+
"status": "working" if resp.status_code == 200 else "error",
|
| 165 |
+
"status_code": resp.status_code,
|
| 166 |
+
"response_keys": list(resp.json().keys()) if resp.status_code == 200 else None
|
| 167 |
+
}
|
| 168 |
+
except Exception as e:
|
| 169 |
+
results["football_api"] = {"status": "error", "error": str(e)}
|
| 170 |
+
else:
|
| 171 |
+
results["football_api"] = {"status": "not_configured", "message": "API key not set"}
|
| 172 |
+
|
| 173 |
+
# Test NBA API
|
| 174 |
+
try:
|
| 175 |
+
games = nba_api.nba.games.list(dates=["2025-01-15"])
|
| 176 |
+
results["nba_api"] = {
|
| 177 |
+
"status": "working",
|
| 178 |
+
"games_found": len(games) if games else 0
|
| 179 |
+
}
|
| 180 |
+
except Exception as e:
|
| 181 |
+
results["nba_api"] = {"status": "error", "error": str(e)}
|
| 182 |
+
|
| 183 |
+
return results
|
| 184 |
|
| 185 |
@app.get("/nba-predictions")
|
| 186 |
def nba_predictions():
|
| 187 |
+
from datetime import timedelta
|
| 188 |
+
|
| 189 |
+
# Use a 2-day forward range for NBA
|
| 190 |
+
today = date.today()
|
| 191 |
+
date_from = today.isoformat()
|
| 192 |
+
date_to = (today + timedelta(days=2)).isoformat()
|
| 193 |
+
|
| 194 |
+
# Generate list of dates for the next 2 days (including today)
|
| 195 |
+
dates = [(today + timedelta(days=i)).isoformat() for i in range(3)]
|
| 196 |
+
|
| 197 |
predictions = []
|
| 198 |
+
debug_info = []
|
| 199 |
+
|
| 200 |
+
try:
|
| 201 |
+
# Test NBA API connectivity first
|
| 202 |
+
games_response = nba_api.nba.games.list(dates=dates)
|
| 203 |
+
|
| 204 |
+
# Handle the PaginatedListResponse object properly
|
| 205 |
+
if hasattr(games_response, 'data'):
|
| 206 |
+
games = games_response.data
|
| 207 |
+
debug_info.append({
|
| 208 |
+
"api_status": "working",
|
| 209 |
+
"games_found": len(games),
|
| 210 |
+
"date_range": f"{date_from} to {date_to}",
|
| 211 |
+
"dates_queried": dates
|
| 212 |
+
})
|
| 213 |
+
else:
|
| 214 |
+
debug_info.append({
|
| 215 |
+
"api_status": "error",
|
| 216 |
+
"error": "Unexpected response format",
|
| 217 |
+
"response_type": str(type(games_response))
|
| 218 |
+
})
|
| 219 |
+
return {
|
| 220 |
+
"date_range": f"{date_from} to {date_to}",
|
| 221 |
+
"predictions": [],
|
| 222 |
+
"debug_info": debug_info,
|
| 223 |
+
"total_predictions": 0,
|
| 224 |
+
"error": "NBA API returned unexpected response format"
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
except Exception as e:
|
| 228 |
+
debug_info.append({
|
| 229 |
+
"api_status": "error",
|
| 230 |
+
"error": str(e),
|
| 231 |
+
"date_range": f"{date_from} to {date_to}"
|
| 232 |
+
})
|
| 233 |
+
return {
|
| 234 |
+
"date_range": f"{date_from} to {date_to}",
|
| 235 |
+
"predictions": [],
|
| 236 |
+
"debug_info": debug_info,
|
| 237 |
+
"total_predictions": 0,
|
| 238 |
+
"error": f"NBA API error: {str(e)}"
|
| 239 |
+
}
|
| 240 |
|
| 241 |
for g in games:
|
| 242 |
home, away = g["home_team"]["full_name"], g["visitor_team"]["full_name"]
|
|
|
|
| 267 |
}
|
| 268 |
})
|
| 269 |
|
| 270 |
+
return {
|
| 271 |
+
"date_range": f"{date_from} to {date_to}",
|
| 272 |
+
"predictions": predictions,
|
| 273 |
+
"debug_info": debug_info,
|
| 274 |
+
"total_predictions": len(predictions)
|
| 275 |
+
}
|
| 276 |
|
| 277 |
if __name__ == "__main__":
|
| 278 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|