reddmann007 commited on
Commit
ed403d8
Β·
verified Β·
1 Parent(s): 14f6e2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -53
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. SETUP & SECRETS πŸ”‘
 
 
 
 
 
 
 
 
9
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
10
 
11
  if not GROQ_API_KEY:
12
- st.error("API Key missing! Please set GROQ_API_KEY in your Hugging Face Secrets.")
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, Powerplay scores, and partnership details from {url}.",
43
- backstory="You are a meticulous data collector who never misses a boundary or a dot ball.",
44
  llm=cricket_llm
45
  )
46
 
47
  analyst = Agent(
48
  role="Technical Match Analyst",
49
- goal="Identify momentum shifts and explain how the Powerplay performance impacted the final result.",
50
- backstory="You are a strategist who understands the 'why' behind the numbers.",
51
  llm=cricket_llm
52
  )
53
 
54
  writer = Agent(
55
  role="International Sports Journalist",
56
- goal="Write a deep, dramatic narration of the match based on technical insights.",
57
- backstory="You write for a global audience, making the game's drama accessible and exciting.",
58
  llm=cricket_llm
59
  )
60
 
61
  # 5. TASK DEFINITIONS πŸ“‹
62
  scrape_task = Task(
63
- description="Analyze the commentary at {url}. Find the Powerplay score and the most impactful partnership.",
64
  agent=scout,
65
- expected_output="A detailed list of match events and statistics."
66
  )
67
 
68
  analyze_task = Task(
69
- description="Review the scout's data. Determine who the 'Player of the Match' was based on high-impact moments.",
70
  agent=analyst,
71
- expected_output="Technical analysis and selection of the standout performer."
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.set_page_config(page_title="Cricket Intel", page_icon="🏏")
86
- st.title("🏏 Deep Match Intelligence")
87
 
88
  url_input = st.text_input("Enter Match Commentary URL:")
89
 
90
- if st.button("Generate Deep Narrative"):
91
  if url_input:
92
- with st.spinner("Writing the match story..."):
93
  match_crew = Crew(
94
  agents=[scout, analyst, writer],
95
  tasks=[scrape_task, analyze_task, write_report_task]
96
  )
97
 
98
- # Crew returns the Pydantic object directly
99
- report = match_crew.kickoff(inputs={'url': url_input}).pydantic
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("### ⚑ Quick Highlights")
106
- cols = st.columns(len(report.key_stats))
107
- for i, stat in enumerate(report.key_stats):
108
- cols[i % len(cols)].info(stat)
109
  st.divider()
110
- st.markdown("### πŸ“– The Deep Narrative")
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 to begin.")
 
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.")