Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from datetime import date
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from sentence_transformers import SentenceTransformer, util
|
| 7 |
+
from balldontlie import BalldontlieAPI
|
| 8 |
+
import os
|
| 9 |
+
import uvicorn
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
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("/soccer-predictions")
|
| 39 |
+
def soccer_predictions():
|
| 40 |
+
today = date.today().isoformat()
|
| 41 |
+
predictions = []
|
| 42 |
+
|
| 43 |
+
for league, code in SOCCER_LEAGUES.items():
|
| 44 |
+
headers = {"X-Auth-Token": FOOTBALL_API_KEY}
|
| 45 |
+
resp = requests.get(
|
| 46 |
+
f"{FOOTBALL_ENDPOINT}?competitions={code}&dateFrom={today}&dateTo={today}",
|
| 47 |
+
headers=headers
|
| 48 |
+
)
|
| 49 |
+
matches = resp.json().get("matches", [])
|
| 50 |
+
|
| 51 |
+
for match in matches:
|
| 52 |
+
home, away = match["homeTeam"]["name"], match["awayTeam"]["name"]
|
| 53 |
+
|
| 54 |
+
home_news = get_team_news(home, "football")
|
| 55 |
+
away_news = get_team_news(away, "football")
|
| 56 |
+
|
| 57 |
+
labels = ["positive", "negative", "injury", "transfer", "motivation"]
|
| 58 |
+
home_sent = sentiment_model(home_news, labels)
|
| 59 |
+
away_sent = sentiment_model(away_news, labels)
|
| 60 |
+
|
| 61 |
+
emb_home = similarity_model.encode(home + " " + home_news, convert_to_tensor=True)
|
| 62 |
+
emb_away = similarity_model.encode(away + " " + away_news, convert_to_tensor=True)
|
| 63 |
+
similarity = util.pytorch_cos_sim(emb_home, emb_away).item()
|
| 64 |
+
|
| 65 |
+
context = f"""
|
| 66 |
+
Match: {home} vs {away}.
|
| 67 |
+
Home news: {home_news}.
|
| 68 |
+
Away news: {away_news}.
|
| 69 |
+
Semantic similarity: {similarity}.
|
| 70 |
+
"""
|
| 71 |
+
reasoning = reasoning_model(
|
| 72 |
+
f"Predict 1X2 and Over/Under 2.5 goals for this soccer match. Context: {context}",
|
| 73 |
+
max_length=256
|
| 74 |
+
)[0]['generated_text']
|
| 75 |
+
|
| 76 |
+
predictions.append({
|
| 77 |
+
"league": league,
|
| 78 |
+
"match": f"{home} vs {away}",
|
| 79 |
+
"reason": reasoning,
|
| 80 |
+
"news_summary": {
|
| 81 |
+
home: home_sent,
|
| 82 |
+
away: away_sent
|
| 83 |
+
}
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
return {"date": today, "predictions": predictions}
|
| 87 |
+
|
| 88 |
+
@app.get("/nba-predictions")
|
| 89 |
+
def nba_predictions():
|
| 90 |
+
today = date.today().isoformat()
|
| 91 |
+
games = nba_api.nba.games.list(dates=[today])
|
| 92 |
+
predictions = []
|
| 93 |
+
|
| 94 |
+
for g in games:
|
| 95 |
+
home, away = g["home_team"]["full_name"], g["visitor_team"]["full_name"]
|
| 96 |
+
|
| 97 |
+
home_news = get_team_news(home, "NBA")
|
| 98 |
+
away_news = get_team_news(away, "NBA")
|
| 99 |
+
|
| 100 |
+
labels = ["positive", "negative", "injury", "motivation"]
|
| 101 |
+
home_sent = sentiment_model(home_news, labels)
|
| 102 |
+
away_sent = sentiment_model(away_news, labels)
|
| 103 |
+
|
| 104 |
+
context = f"""
|
| 105 |
+
Match: {home} vs {away}.
|
| 106 |
+
Home news: {home_news}.
|
| 107 |
+
Away news: {away_news}.
|
| 108 |
+
"""
|
| 109 |
+
reasoning = reasoning_model(
|
| 110 |
+
f"Predict winner and Over/Under points for this NBA game. Context: {context}",
|
| 111 |
+
max_length=256
|
| 112 |
+
)[0]['generated_text']
|
| 113 |
+
|
| 114 |
+
predictions.append({
|
| 115 |
+
"match": f"{home} vs {away}",
|
| 116 |
+
"reason": reasoning,
|
| 117 |
+
"news_summary": {
|
| 118 |
+
home: home_sent,
|
| 119 |
+
away: away_sent
|
| 120 |
+
}
|
| 121 |
+
})
|
| 122 |
+
|
| 123 |
+
return {"date": today, "predictions": predictions}
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|