Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- app.js +113 -133
- history.js +37 -5
- index.html +1 -0
- main.py +83 -19
- style.css +17 -0
app.js
CHANGED
|
@@ -1,133 +1,113 @@
|
|
| 1 |
-
//
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
document.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
const
|
| 16 |
-
const
|
| 17 |
-
const
|
| 18 |
-
const
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 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 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
const
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
setTimeout(() => {
|
| 115 |
-
const p = liveOutcomes[Math.floor(Math.random() * liveOutcomes.length)];
|
| 116 |
-
|
| 117 |
-
// update fake score
|
| 118 |
-
balls++;
|
| 119 |
-
if (balls > 6) { balls = 1; }
|
| 120 |
-
overEl.innerText = `15.${balls} Overs`;
|
| 121 |
-
|
| 122 |
-
dOut.innerText = p.delivery;
|
| 123 |
-
oOut.innerText = p.outcome;
|
| 124 |
-
|
| 125 |
-
dProbBar.style.width = `${p.probD}%`;
|
| 126 |
-
oProbBar.style.width = `${p.probO}%`;
|
| 127 |
-
|
| 128 |
-
dProbText.innerText = `${p.probD}% Probability`;
|
| 129 |
-
oProbText.innerText = `${p.probO}% Probability`;
|
| 130 |
-
}, 1000);
|
| 131 |
-
}, 5000); // Cycles continuously every 5 seconds
|
| 132 |
-
}
|
| 133 |
-
});
|
|
|
|
| 1 |
+
// Handle dynamic fetches from the Python Backend
|
| 2 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 3 |
+
// Top Bar Elements
|
| 4 |
+
const statusBadge = document.getElementById('matchStatusBadge');
|
| 5 |
+
const team1El = document.getElementById('team1Text');
|
| 6 |
+
const team2El = document.getElementById('team2Text');
|
| 7 |
+
const scoreEl = document.getElementById('liveScoreText');
|
| 8 |
+
const overEl = document.getElementById('liveOverText');
|
| 9 |
+
const rrEl = document.getElementById('matchRunRateText');
|
| 10 |
+
const apiIndicator = document.getElementById('apiStatus');
|
| 11 |
+
const venueEl = document.getElementById('venueText');
|
| 12 |
+
const subEl = document.getElementById('matchSubtitle');
|
| 13 |
+
|
| 14 |
+
// UI Panes
|
| 15 |
+
const liveBox = document.getElementById('livePredictorBox');
|
| 16 |
+
const upcBox = document.getElementById('upcomingPredictorBox');
|
| 17 |
+
const titleEl = document.getElementById('predictionTitle');
|
| 18 |
+
const refreshBtn = document.getElementById('refreshBtn');
|
| 19 |
+
|
| 20 |
+
async function syncBackendData() {
|
| 21 |
+
try {
|
| 22 |
+
apiIndicator.innerText = "SERVER: SYNCING...";
|
| 23 |
+
apiIndicator.style.color = "yellow";
|
| 24 |
+
|
| 25 |
+
// Fetch exactly from Python Server memory state!
|
| 26 |
+
const res = await fetch('/api/state');
|
| 27 |
+
const state = await res.json();
|
| 28 |
+
|
| 29 |
+
apiIndicator.innerText = "SERVER: CONNECTED";
|
| 30 |
+
apiIndicator.style.color = "var(--accent-green)";
|
| 31 |
+
|
| 32 |
+
team1El.innerText = state.teams[0] || "Team 1";
|
| 33 |
+
team2El.innerText = state.teams[1] || "Team 2";
|
| 34 |
+
venueEl.innerText = state.venue;
|
| 35 |
+
|
| 36 |
+
if (state.live) {
|
| 37 |
+
statusBadge.innerText = "🔥 MATCH IN PROGRESS";
|
| 38 |
+
statusBadge.style.color = "#ef4444";
|
| 39 |
+
statusBadge.style.background = "rgba(239, 68, 68, 0.1)";
|
| 40 |
+
|
| 41 |
+
scoreEl.innerText = state.score;
|
| 42 |
+
overEl.innerText = state.overs + " Overs";
|
| 43 |
+
|
| 44 |
+
scoreEl.classList.remove('hidden');
|
| 45 |
+
overEl.classList.remove('hidden');
|
| 46 |
+
|
| 47 |
+
subEl.innerText = "Live AI Tracking";
|
| 48 |
+
|
| 49 |
+
titleEl.innerText = "LIVE BALL PREDICTIONS ACTIVE";
|
| 50 |
+
liveBox.classList.remove('hidden');
|
| 51 |
+
upcBox.classList.add('hidden');
|
| 52 |
+
|
| 53 |
+
reCalculateAI(); // trigger the visual bars
|
| 54 |
+
} else {
|
| 55 |
+
statusBadge.innerText = "⏳ UPCOMING FIXTURE";
|
| 56 |
+
statusBadge.style.color = "var(--accent-cyan)";
|
| 57 |
+
statusBadge.style.background = "rgba(6, 182, 212, 0.1)";
|
| 58 |
+
|
| 59 |
+
scoreEl.classList.add('hidden');
|
| 60 |
+
overEl.classList.add('hidden');
|
| 61 |
+
subEl.innerText = "Processing Upcoming Schedule";
|
| 62 |
+
|
| 63 |
+
liveBox.classList.add('hidden');
|
| 64 |
+
upcBox.classList.remove('hidden');
|
| 65 |
+
titleEl.innerText = "UPCOMING MATCH WIN PROBABILITY";
|
| 66 |
+
|
| 67 |
+
document.getElementById('favoredTeamText').innerText = state.teams[0];
|
| 68 |
+
document.getElementById('winProbBar').style.width = "72%";
|
| 69 |
+
document.getElementById('winProbText').innerText = "72% Probability";
|
| 70 |
+
}
|
| 71 |
+
} catch(e) {
|
| 72 |
+
console.error("Backend Disconnected:", e);
|
| 73 |
+
apiIndicator.innerText = "SERVER: OFFLINE";
|
| 74 |
+
apiIndicator.style.color = "red";
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
function reCalculateAI() {
|
| 79 |
+
const dOut = document.getElementById('deliveryOut');
|
| 80 |
+
const oOut = document.getElementById('outcomeOut');
|
| 81 |
+
const dProbBar = document.getElementById('delProbBar');
|
| 82 |
+
const oProbBar = document.getElementById('outProbBar');
|
| 83 |
+
|
| 84 |
+
dProbBar.style.width = '0%';
|
| 85 |
+
oProbBar.style.width = '0%';
|
| 86 |
+
dOut.innerHTML = "<span class='text-muted'>Re-calculating Data...</span>";
|
| 87 |
+
|
| 88 |
+
// Random permutations representing dynamic engine
|
| 89 |
+
const outcomes = ["Defends - No Run", "Squeezes to Point - 1 Run", "Hook Shot - SIX!!!", "Bowled", "Cover Drive - FOUR"];
|
| 90 |
+
const dels = ["Inswinger", "Yorker", "Slower Bouncer", "Wide Yorker", "Good Length"];
|
| 91 |
+
|
| 92 |
+
setTimeout(() => {
|
| 93 |
+
dOut.innerText = dels[Math.floor(Math.random() * dels.length)];
|
| 94 |
+
oOut.innerText = outcomes[Math.floor(Math.random() * outcomes.length)];
|
| 95 |
+
|
| 96 |
+
dProbBar.style.width = `${Math.floor(Math.random() * 40 + 50)}%`;
|
| 97 |
+
oProbBar.style.width = `${Math.floor(Math.random() * 40 + 50)}%`;
|
| 98 |
+
}, 800);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
if (refreshBtn) {
|
| 102 |
+
refreshBtn.addEventListener('click', () => {
|
| 103 |
+
refreshBtn.innerHTML = "🔄 Syncing...";
|
| 104 |
+
syncBackendData().then(() => {
|
| 105 |
+
setTimeout(() => { refreshBtn.innerHTML = "🔄 Refresh Live State"; }, 500);
|
| 106 |
+
});
|
| 107 |
+
});
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Auto update every 12 seconds
|
| 111 |
+
setInterval(syncBackendData, 12000);
|
| 112 |
+
syncBackendData(); // Init
|
| 113 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
history.js
CHANGED
|
@@ -1,5 +1,37 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', async () => {
|
| 2 |
+
const tbody = document.getElementById('historyTableBody');
|
| 3 |
+
if(!tbody) return;
|
| 4 |
+
|
| 5 |
+
async function fetchHistory() {
|
| 6 |
+
try {
|
| 7 |
+
const res = await fetch('/api/history');
|
| 8 |
+
const historyData = await res.json();
|
| 9 |
+
|
| 10 |
+
tbody.innerHTML = '';
|
| 11 |
+
|
| 12 |
+
if (historyData.length === 0) {
|
| 13 |
+
tbody.innerHTML = '<tr><td colspan="6" style="text-align:center; padding:3rem; color:var(--text-muted)">The AI Engine has booted up.<br>Waiting for live match data to process before generating history records...</td></tr>';
|
| 14 |
+
return;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
historyData.forEach(item => {
|
| 18 |
+
const tr = document.createElement('tr');
|
| 19 |
+
tr.innerHTML = `
|
| 20 |
+
<td>${item.time}</td>
|
| 21 |
+
<td style="font-weight:bold; color:var(--text-main)">${item.match}</td>
|
| 22 |
+
<td>${item.over} Overs</td>
|
| 23 |
+
<td>${item.score}</td>
|
| 24 |
+
<td class="match-yes">${item.prediction}</td>
|
| 25 |
+
<td style="color:var(--accent-green)">✓ Logged</td>
|
| 26 |
+
`;
|
| 27 |
+
tbody.appendChild(tr);
|
| 28 |
+
});
|
| 29 |
+
} catch(e) {
|
| 30 |
+
console.error("History fetch error", e);
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
fetchHistory();
|
| 35 |
+
// Refresh history grid every 15 seconds automatically
|
| 36 |
+
setInterval(fetchHistory, 15000);
|
| 37 |
+
});
|
index.html
CHANGED
|
@@ -15,6 +15,7 @@
|
|
| 15 |
<span class="ai-glow">Predict</span>IPL
|
| 16 |
</div>
|
| 17 |
<ul class="nav-links">
|
|
|
|
| 18 |
<li><a href="index.html" class="active">Live Prediction</a></li>
|
| 19 |
<li><a href="history.html">Match History</a></li>
|
| 20 |
</ul>
|
|
|
|
| 15 |
<span class="ai-glow">Predict</span>IPL
|
| 16 |
</div>
|
| 17 |
<ul class="nav-links">
|
| 18 |
+
<li><button id="refreshBtn" class="refresh-btn">🔄 Refresh Live State</button></li>
|
| 19 |
<li><a href="index.html" class="active">Live Prediction</a></li>
|
| 20 |
<li><a href="history.html">Match History</a></li>
|
| 21 |
</ul>
|
main.py
CHANGED
|
@@ -1,36 +1,100 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 3 |
import uvicorn
|
| 4 |
import threading
|
| 5 |
import time
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# ==============================================================================
|
| 20 |
-
def autonomous_data_heist():
|
| 21 |
while True:
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
print("
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
# Hugging Face Spaces strictly requires servers to broadcast on port 7860
|
| 35 |
-
print("Initializing Hugging Face Server on Port 7860...")
|
| 36 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
import uvicorn
|
| 5 |
import threading
|
| 6 |
import time
|
| 7 |
+
import requests
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
# Your API Key from earlier
|
| 14 |
+
API_KEY = "c14e1300-c6d6-4165-8424-8a00c80c1b94"
|
| 15 |
+
URL = f"https://api.cricapi.com/v1/currentMatches?apikey={API_KEY}&offset=0"
|
| 16 |
+
|
| 17 |
+
# Central Python Brain State Memory
|
| 18 |
+
system_state = {
|
| 19 |
+
"live": False, "status": "UPCOMING", "teams": ["Initializing", "Data"],
|
| 20 |
+
"score": "0/0", "overs": "0.0", "venue": "Loading Stadium..."
|
| 21 |
+
}
|
| 22 |
+
prediction_history = []
|
| 23 |
+
last_over = ""
|
| 24 |
|
| 25 |
+
def autonomous_backend_scraper():
|
| 26 |
+
global system_state, prediction_history, last_over
|
| 27 |
+
print("AI Backend Brain Engaged. Autonomous Fetching Started...")
|
| 28 |
+
|
|
|
|
|
|
|
| 29 |
while True:
|
| 30 |
try:
|
| 31 |
+
# Backend fetches data. Bypasses all browser CORS limitations!
|
| 32 |
+
res = requests.get(URL, timeout=15)
|
| 33 |
+
data = res.json()
|
| 34 |
+
|
| 35 |
+
if data.get('status') == 'success':
|
| 36 |
+
t20 = [m for m in data.get('data', []) if m.get('matchType') != 'test']
|
| 37 |
+
|
| 38 |
+
live_match = next((m for m in t20 if m.get('matchStarted') and not m.get('matchEnded')), None)
|
| 39 |
+
|
| 40 |
+
if live_match:
|
| 41 |
+
system_state["live"] = True
|
| 42 |
+
system_state["status"] = "LIVE"
|
| 43 |
+
system_state["teams"] = live_match.get('teams', ["Team A", "Team B"])
|
| 44 |
+
system_state["venue"] = live_match.get('venue', "Live Stadium")
|
| 45 |
+
|
| 46 |
+
if live_match.get('score'):
|
| 47 |
+
sc = live_match['score'][-1]
|
| 48 |
+
new_over = str(sc.get('o', '0.0'))
|
| 49 |
+
new_score = f"{sc.get('r', '0')}/{sc.get('w', '0')}"
|
| 50 |
+
|
| 51 |
+
system_state["score"] = new_score
|
| 52 |
+
system_state["overs"] = new_over
|
| 53 |
+
|
| 54 |
+
# History Logic: Save records when balls progress!
|
| 55 |
+
if new_over != last_over and last_over != "":
|
| 56 |
+
prediction_history.append({
|
| 57 |
+
"time": datetime.now().strftime("%H:%M:%S"),
|
| 58 |
+
"match": f"{system_state['teams'][0]} vs {system_state['teams'][1]}",
|
| 59 |
+
"over": new_over,
|
| 60 |
+
"score": new_score,
|
| 61 |
+
"prediction": "Processed successfully"
|
| 62 |
+
})
|
| 63 |
+
# Cap history at 50 to save RAM
|
| 64 |
+
if len(prediction_history) > 50:
|
| 65 |
+
prediction_history.pop(0)
|
| 66 |
+
|
| 67 |
+
last_over = new_over
|
| 68 |
+
|
| 69 |
+
else:
|
| 70 |
+
# No live matches. Find the immediate upcoming one.
|
| 71 |
+
upc = next((m for m in t20 if not m.get('matchStarted')), None)
|
| 72 |
+
if upc:
|
| 73 |
+
system_state["live"] = False
|
| 74 |
+
system_state["status"] = "UPCOMING"
|
| 75 |
+
system_state["teams"] = upc.get('teams', ["Team A", "Team B"])
|
| 76 |
+
system_state["venue"] = upc.get('venue', "Upcoming Venue")
|
| 77 |
+
|
| 78 |
except Exception as e:
|
| 79 |
+
print("Python Server Network Error:", e)
|
| 80 |
|
| 81 |
+
# Cooldown timer to not abuse the API limits
|
| 82 |
+
time.sleep(15)
|
| 83 |
+
|
| 84 |
+
# Start the Ghost Thread
|
| 85 |
+
threading.Thread(target=autonomous_backend_scraper, daemon=True).start()
|
| 86 |
+
|
| 87 |
+
# API Endpoints mapped directly to the UI
|
| 88 |
+
@app.get("/api/state")
|
| 89 |
+
def get_state():
|
| 90 |
+
return system_state
|
| 91 |
+
|
| 92 |
+
@app.get("/api/history")
|
| 93 |
+
def get_history():
|
| 94 |
+
return prediction_history
|
| 95 |
|
| 96 |
+
# Serve the beautiful frontend Website lastly
|
| 97 |
+
app.mount("/", StaticFiles(directory=".", html=True), name="frontend")
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 100 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
style.css
CHANGED
|
@@ -65,6 +65,23 @@ body {
|
|
| 65 |
color: var(--accent-cyan);
|
| 66 |
}
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
/* Dashboard Layout */
|
| 69 |
.dashboard {
|
| 70 |
padding: 2rem 3rem;
|
|
|
|
| 65 |
color: var(--accent-cyan);
|
| 66 |
}
|
| 67 |
|
| 68 |
+
.refresh-btn {
|
| 69 |
+
background: transparent;
|
| 70 |
+
color: var(--accent-green);
|
| 71 |
+
border: 1px solid var(--accent-green);
|
| 72 |
+
border-radius: 20px;
|
| 73 |
+
padding: 0.4rem 1rem;
|
| 74 |
+
cursor: pointer;
|
| 75 |
+
font-weight: 600;
|
| 76 |
+
font-family: 'Outfit', sans-serif;
|
| 77 |
+
transition: all 0.3s;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.refresh-btn:hover {
|
| 81 |
+
background: var(--accent-green);
|
| 82 |
+
color: var(--bg-dark);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
/* Dashboard Layout */
|
| 86 |
.dashboard {
|
| 87 |
padding: 2rem 3rem;
|