Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| from crewai import Agent, Task, Crew, LLM | |
| from pydantic import BaseModel, Field | |
| from typing import List | |
| # 1. DATA STRUCTURE π | |
| # This ensures the AI gives us a clean "story" instead of raw JSON code. | |
| class MatchReport(BaseModel): | |
| match_title: str = Field(description="The formal title of the match.") | |
| player_of_the_match: str = Field(description="The standout performer and why.") | |
| deep_narrative: str = Field(description="A 3-paragraph story of the match events.") | |
| key_highlights: List[str] = Field(description="3-5 bullet points of critical moments.") | |
| # 2. SETUP & SECRETS π | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
| if not GROQ_API_KEY: | |
| st.error("Please set GROQ_API_KEY in your secrets.") | |
| st.stop() | |
| # 3. LLM CONFIGURATION π§ | |
| # Updated LLM Configuration | |
| cricket_llm = LLM( | |
| model="groq/llama-3.1-8b-instant", # Updated supported ID | |
| api_key=GROQ_API_KEY | |
| ) | |
| # 4. AGENT DEFINITIONS π€ | |
| # We define these FIRST so the tasks can find them. | |
| scout = Agent( | |
| role="Cricket Match Scout", | |
| goal="Extract ball-by-ball data and Powerplay scores from {url}.", | |
| backstory="You are an expert at finding specific match facts.", | |
| llm=cricket_llm | |
| ) | |
| analyst = Agent( | |
| role="Technical Match Analyst", | |
| goal="Identify momentum shifts and the Player of the Match.", | |
| backstory="You interpret data to find the 'why' behind the win.", | |
| llm=cricket_llm | |
| ) | |
| writer = Agent( | |
| role="International Sports Journalist", | |
| goal="Write a deep, dramatic narration of the match.", | |
| backstory="You turn technical insights into a compelling story.", | |
| llm=cricket_llm | |
| ) | |
| # 5. TASK DEFINITIONS π | |
| scrape_task = Task( | |
| description="Analyze the commentary at {url}. Find key stats and partnerships.", | |
| agent=scout, | |
| expected_output="A list of match stats." | |
| ) | |
| analyze_task = Task( | |
| description="Determine the turning points and the Player of the Match.", | |
| agent=analyst, | |
| expected_output="A technical summary of the game." | |
| ) | |
| write_report_task = Task( | |
| description="Draft a deep narrative report. Focus on the drama and the Powerplay.", | |
| # We add this line to satisfy the Pydantic requirement: | |
| expected_output="A comprehensive match report following the MatchReport structure.", | |
| agent=writer, | |
| output_pydantic=MatchReport | |
| ) | |
| # 6. STREAMLIT UI π₯οΈ | |
| st.title("π Cricket Intelligence") | |
| url_input = st.text_input("Enter Match Commentary URL:") | |
| if st.button("Generate Report"): | |
| if url_input: | |
| with st.spinner("Analyzing match data..."): | |
| match_crew = Crew( | |
| agents=[scout, analyst, writer], | |
| tasks=[scrape_task, analyze_task, write_report_task], | |
| verbose=True, # This helps us see the agents working in real-time | |
| max_rpm=2 # Limits the crew to 2 requests every 60 seconds | |
| ) | |
| result = match_crew.kickoff(inputs={'url': url_input}) | |
| report = result.pydantic | |
| st.header(report.match_title) | |
| st.subheader(f"π {report.player_of_the_match}") | |
| st.markdown("### β‘ Highlights") | |
| for point in report.key_highlights: | |
| st.write(f"- {point}") | |
| st.divider() | |
| st.markdown("### π Match Story") | |
| st.write(report.deep_narrative) | |
| else: | |
| st.warning("Please provide a URL.") | |