Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,97 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import re
|
| 4 |
from crewai import Agent, Task, Crew, LLM
|
| 5 |
from pydantic import BaseModel, Field
|
| 6 |
from typing import List
|
| 7 |
|
| 8 |
-
# 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 10 |
|
| 11 |
if not GROQ_API_KEY:
|
| 12 |
-
st.error("
|
| 13 |
st.stop()
|
| 14 |
|
| 15 |
-
# 2. DATA STRUCTURE π
|
| 16 |
-
class MatchReport(BaseModel):
|
| 17 |
-
match_title: str = Field(description="The formal title, e.g., 'DC vs MI, Match 43, IPL 2024'. Do not include any other text.")
|
| 18 |
-
player_of_the_match: str = Field(description="Name and a 1-sentence reason.")
|
| 19 |
-
deep_narrative: str = Field(description="A 3-paragraph story of the match events.")
|
| 20 |
-
key_stats: List[str] = Field(description="A list of 3-5 specific bullet points (e.g., 'DC: 257/4')")
|
| 21 |
-
|
| 22 |
-
write_report_task = Task(
|
| 23 |
-
description=(
|
| 24 |
-
"Based on the analysis, create a deep narrative match report. "
|
| 25 |
-
"IMPORTANT: Provide ONLY the structured information required. "
|
| 26 |
-
"Do not include conversational filler like 'Here is the report' or 'Okay, I will do that'."
|
| 27 |
-
),
|
| 28 |
-
agent=writer,
|
| 29 |
-
output_pydantic=MatchReport
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
# 3. LLM CONFIGURATION π§
|
| 33 |
-
# Using a current, supported model
|
| 34 |
cricket_llm = LLM(
|
| 35 |
model="groq/llama-3.3-70b-8192",
|
| 36 |
api_key=GROQ_API_KEY
|
| 37 |
)
|
| 38 |
|
| 39 |
# 4. AGENT DEFINITIONS π€
|
|
|
|
| 40 |
scout = Agent(
|
| 41 |
role="Cricket Match Scout",
|
| 42 |
-
goal="Extract ball-by-ball data
|
| 43 |
-
backstory="You are
|
| 44 |
llm=cricket_llm
|
| 45 |
)
|
| 46 |
|
| 47 |
analyst = Agent(
|
| 48 |
role="Technical Match Analyst",
|
| 49 |
-
goal="Identify momentum shifts and
|
| 50 |
-
backstory="You
|
| 51 |
llm=cricket_llm
|
| 52 |
)
|
| 53 |
|
| 54 |
writer = Agent(
|
| 55 |
role="International Sports Journalist",
|
| 56 |
-
goal="Write a deep, dramatic narration of the match
|
| 57 |
-
backstory="You
|
| 58 |
llm=cricket_llm
|
| 59 |
)
|
| 60 |
|
| 61 |
# 5. TASK DEFINITIONS π
|
| 62 |
scrape_task = Task(
|
| 63 |
-
description="Analyze the commentary at {url}. Find
|
| 64 |
agent=scout,
|
| 65 |
-
expected_output="A
|
| 66 |
)
|
| 67 |
|
| 68 |
analyze_task = Task(
|
| 69 |
-
description="
|
| 70 |
agent=analyst,
|
| 71 |
-
expected_output="
|
| 72 |
)
|
| 73 |
|
| 74 |
write_report_task = Task(
|
| 75 |
-
description=
|
| 76 |
-
"Draft a 'Deep Narrative' report. Start with the atmosphere and the Powerplay battle, "
|
| 77 |
-
"then describe the middle-over shifts, ending with the climax. Use the provided data "
|
| 78 |
-
"to ensure accuracy, but prioritize storytelling."
|
| 79 |
-
),
|
| 80 |
agent=writer,
|
| 81 |
-
output_pydantic=MatchReport
|
| 82 |
)
|
| 83 |
|
| 84 |
# 6. STREAMLIT UI π₯οΈ
|
| 85 |
-
st.
|
| 86 |
-
st.title("π Deep Match Intelligence")
|
| 87 |
|
| 88 |
url_input = st.text_input("Enter Match Commentary URL:")
|
| 89 |
|
| 90 |
-
if st.button("Generate
|
| 91 |
if url_input:
|
| 92 |
-
with st.spinner("
|
| 93 |
match_crew = Crew(
|
| 94 |
agents=[scout, analyst, writer],
|
| 95 |
tasks=[scrape_task, analyze_task, write_report_task]
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
report =
|
| 100 |
|
| 101 |
-
# Displaying the structured narrative
|
| 102 |
st.header(report.match_title)
|
| 103 |
st.subheader(f"π {report.player_of_the_match}")
|
| 104 |
|
| 105 |
-
st.markdown("### β‘
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
st.divider()
|
| 110 |
-
st.markdown("### π
|
| 111 |
st.write(report.deep_narrative)
|
| 112 |
-
st.markdown("### π Key Statistics")
|
| 113 |
-
for stat in report.key_stats:
|
| 114 |
-
st.write(f"- {stat}")
|
| 115 |
else:
|
| 116 |
-
st.warning("Please provide a URL
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
from crewai import Agent, Task, Crew, LLM
|
| 4 |
from pydantic import BaseModel, Field
|
| 5 |
from typing import List
|
| 6 |
|
| 7 |
+
# 1. DATA STRUCTURE π
|
| 8 |
+
# This ensures the AI gives us a clean "story" instead of raw JSON code.
|
| 9 |
+
class MatchReport(BaseModel):
|
| 10 |
+
match_title: str = Field(description="The formal title of the match.")
|
| 11 |
+
player_of_the_match: str = Field(description="The standout performer and why.")
|
| 12 |
+
deep_narrative: str = Field(description="A 3-paragraph story of the match events.")
|
| 13 |
+
key_highlights: List[str] = Field(description="3-5 bullet points of critical moments.")
|
| 14 |
+
|
| 15 |
+
# 2. SETUP & SECRETS π
|
| 16 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 17 |
|
| 18 |
if not GROQ_API_KEY:
|
| 19 |
+
st.error("Please set GROQ_API_KEY in your secrets.")
|
| 20 |
st.stop()
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# 3. LLM CONFIGURATION π§
|
|
|
|
| 23 |
cricket_llm = LLM(
|
| 24 |
model="groq/llama-3.3-70b-8192",
|
| 25 |
api_key=GROQ_API_KEY
|
| 26 |
)
|
| 27 |
|
| 28 |
# 4. AGENT DEFINITIONS π€
|
| 29 |
+
# We define these FIRST so the tasks can find them.
|
| 30 |
scout = Agent(
|
| 31 |
role="Cricket Match Scout",
|
| 32 |
+
goal="Extract ball-by-ball data and Powerplay scores from {url}.",
|
| 33 |
+
backstory="You are an expert at finding specific match facts.",
|
| 34 |
llm=cricket_llm
|
| 35 |
)
|
| 36 |
|
| 37 |
analyst = Agent(
|
| 38 |
role="Technical Match Analyst",
|
| 39 |
+
goal="Identify momentum shifts and the Player of the Match.",
|
| 40 |
+
backstory="You interpret data to find the 'why' behind the win.",
|
| 41 |
llm=cricket_llm
|
| 42 |
)
|
| 43 |
|
| 44 |
writer = Agent(
|
| 45 |
role="International Sports Journalist",
|
| 46 |
+
goal="Write a deep, dramatic narration of the match.",
|
| 47 |
+
backstory="You turn technical insights into a compelling story.",
|
| 48 |
llm=cricket_llm
|
| 49 |
)
|
| 50 |
|
| 51 |
# 5. TASK DEFINITIONS π
|
| 52 |
scrape_task = Task(
|
| 53 |
+
description="Analyze the commentary at {url}. Find key stats and partnerships.",
|
| 54 |
agent=scout,
|
| 55 |
+
expected_output="A list of match stats."
|
| 56 |
)
|
| 57 |
|
| 58 |
analyze_task = Task(
|
| 59 |
+
description="Determine the turning points and the Player of the Match.",
|
| 60 |
agent=analyst,
|
| 61 |
+
expected_output="A technical summary of the game."
|
| 62 |
)
|
| 63 |
|
| 64 |
write_report_task = Task(
|
| 65 |
+
description="Draft a deep narrative report. Focus on the drama and the Powerplay.",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
agent=writer,
|
| 67 |
+
output_pydantic=MatchReport # This links to our data structure above
|
| 68 |
)
|
| 69 |
|
| 70 |
# 6. STREAMLIT UI π₯οΈ
|
| 71 |
+
st.title("π Cricket Intelligence")
|
|
|
|
| 72 |
|
| 73 |
url_input = st.text_input("Enter Match Commentary URL:")
|
| 74 |
|
| 75 |
+
if st.button("Generate Report"):
|
| 76 |
if url_input:
|
| 77 |
+
with st.spinner("Analyzing match data..."):
|
| 78 |
match_crew = Crew(
|
| 79 |
agents=[scout, analyst, writer],
|
| 80 |
tasks=[scrape_task, analyze_task, write_report_task]
|
| 81 |
)
|
| 82 |
|
| 83 |
+
result = match_crew.kickoff(inputs={'url': url_input})
|
| 84 |
+
report = result.pydantic
|
| 85 |
|
|
|
|
| 86 |
st.header(report.match_title)
|
| 87 |
st.subheader(f"π {report.player_of_the_match}")
|
| 88 |
|
| 89 |
+
st.markdown("### β‘ Highlights")
|
| 90 |
+
for point in report.key_highlights:
|
| 91 |
+
st.write(f"- {point}")
|
| 92 |
+
|
| 93 |
st.divider()
|
| 94 |
+
st.markdown("### π Match Story")
|
| 95 |
st.write(report.deep_narrative)
|
|
|
|
|
|
|
|
|
|
| 96 |
else:
|
| 97 |
+
st.warning("Please provide a URL.")
|