Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
|
| 5 |
+
IPL_TEAMS = [
|
| 6 |
+
"Chennai Super Kings", "Delhi Capitals", "Gujarat Titans", "Kolkata Knight Riders",
|
| 7 |
+
"Lucknow Super Giants", "Mumbai Indians", "Punjab Kings", "Rajasthan Royals",
|
| 8 |
+
"Royal Challengers Bangalore", "Sunrisers Hyderabad"
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
# Helper to detect team name
|
| 12 |
+
def detect_team(user_input):
|
| 13 |
+
for team in IPL_TEAMS:
|
| 14 |
+
if team.lower() in user_input.lower():
|
| 15 |
+
return team
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
# Get live scores
|
| 19 |
+
def get_live_scores(team_name=None):
|
| 20 |
+
try:
|
| 21 |
+
url = "https://www.cricbuzz.com/cricket-match/live-scores"
|
| 22 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 23 |
+
response = requests.get(url, headers=headers)
|
| 24 |
+
|
| 25 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 26 |
+
matches = soup.find_all("div", class_="cb-mtch-lst")
|
| 27 |
+
|
| 28 |
+
results = []
|
| 29 |
+
for match in matches:
|
| 30 |
+
title = match.find("h3").text.strip() if match.find("h3") else None
|
| 31 |
+
status = match.find("div", class_="cb-lv-scrs-col").text.strip() if match.find("div", class_="cb-lv-scrs-col") else None
|
| 32 |
+
if title and status:
|
| 33 |
+
if not team_name or team_name.lower() in title.lower():
|
| 34 |
+
results.append(f"๐ด {title} - {status}")
|
| 35 |
+
return results
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return []
|
| 38 |
+
|
| 39 |
+
# Get recent (non-live) match results
|
| 40 |
+
def get_recent_results(team_name=None):
|
| 41 |
+
try:
|
| 42 |
+
url = "https://www.cricbuzz.com/cricket-scorecard-archives"
|
| 43 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 44 |
+
response = requests.get(url, headers=headers)
|
| 45 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 46 |
+
|
| 47 |
+
matches = soup.find_all("a", class_="text-hvr-underline", href=True)
|
| 48 |
+
results = []
|
| 49 |
+
for match in matches[:10]:
|
| 50 |
+
text = match.text.strip()
|
| 51 |
+
if not team_name or team_name.lower() in text.lower():
|
| 52 |
+
results.append(f"๐ {text}")
|
| 53 |
+
return results if results else ["No past matches found."]
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return [f"Error getting recent results: {e}"]
|
| 56 |
+
|
| 57 |
+
# Get news
|
| 58 |
+
def get_team_news(team_name=None):
|
| 59 |
+
try:
|
| 60 |
+
url = "https://www.cricbuzz.com/cricket-news"
|
| 61 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 62 |
+
response = requests.get(url, headers=headers)
|
| 63 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 64 |
+
headlines = soup.find_all("h2", class_="big-crd-hd")[:5]
|
| 65 |
+
|
| 66 |
+
results = []
|
| 67 |
+
for headline in headlines:
|
| 68 |
+
text = headline.text.strip()
|
| 69 |
+
if not team_name or team_name.lower() in text.lower():
|
| 70 |
+
results.append(f"๐ฐ {text}")
|
| 71 |
+
return results if results else [f"No news found for {team_name or 'IPL'}."]
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return [f"Error fetching news: {e}"]
|
| 74 |
+
|
| 75 |
+
# Fantasy stats (placeholder)
|
| 76 |
+
def get_fantasy_stats(team_name=None):
|
| 77 |
+
return [f"Fantasy stats for {team_name or 'your team'} are currently limited. Try Dream11 or official IPL fantasy apps."]
|
| 78 |
+
|
| 79 |
+
# Streamlit UI
|
| 80 |
+
st.set_page_config(page_title="IPL Chatbot", layout="centered")
|
| 81 |
+
st.title("๐ IPL Chatbot")
|
| 82 |
+
|
| 83 |
+
user_input = st.text_input("Ask me anything about IPL teams, scores, or news:", placeholder="e.g. Whatโs the score for CSK?")
|
| 84 |
+
|
| 85 |
+
if user_input:
|
| 86 |
+
team = detect_team(user_input)
|
| 87 |
+
|
| 88 |
+
if "score" in user_input.lower():
|
| 89 |
+
scores = get_live_scores(team)
|
| 90 |
+
if scores:
|
| 91 |
+
st.subheader("๐บ Live Scores")
|
| 92 |
+
for s in scores:
|
| 93 |
+
st.write(s)
|
| 94 |
+
else:
|
| 95 |
+
st.subheader("๐ No live match. Showing recent results instead:")
|
| 96 |
+
for r in get_recent_results(team):
|
| 97 |
+
st.write(r)
|
| 98 |
+
|
| 99 |
+
elif "news" in user_input.lower():
|
| 100 |
+
st.subheader("๐ฐ Latest News")
|
| 101 |
+
for news in get_team_news(team):
|
| 102 |
+
st.write(news)
|
| 103 |
+
|
| 104 |
+
elif "fantasy" in user_input.lower():
|
| 105 |
+
st.subheader("๐ Fantasy Stats")
|
| 106 |
+
for stat in get_fantasy_stats(team):
|
| 107 |
+
st.write(stat)
|
| 108 |
+
|
| 109 |
+
else:
|
| 110 |
+
st.info("Try asking about 'scores', 'news', or 'fantasy' for a team.")
|
| 111 |
+
|
| 112 |
+
# Examples
|
| 113 |
+
with st.expander("๐ก Try these examples"):
|
| 114 |
+
st.markdown("""
|
| 115 |
+
- Whatโs the score for Mumbai Indians?
|
| 116 |
+
- Show me CSK news.
|
| 117 |
+
- Fantasy info for RCB.
|
| 118 |
+
- Any update on Rajasthan Royals match?
|
| 119 |
+
- Get Gujarat Titans score.
|
| 120 |
+
""")
|