cryogenic22 commited on
Commit
afa9ee7
·
verified ·
1 Parent(s): cd64635

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +87 -22
utils.py CHANGED
@@ -10,11 +10,18 @@ def update_progress(container, percentage, message=""):
10
 
11
  def extract_data_points(text):
12
  llm = ChatOpenAI(temperature=0, model="gpt-4")
13
- prompt = """Extract market metrics from this text as JSON:
14
  {text}
15
 
16
- Return format:
17
- {{"market_size": "value", "cagr": "value", "market_share_leader": "value", "total_players": "value"}}"""
 
 
 
 
 
 
 
18
 
19
  response = llm.invoke(prompt.format(text=text))
20
  try:
@@ -23,48 +30,106 @@ def extract_data_points(text):
23
  except:
24
  return {}
25
 
26
- def expand_content(base_content, topic):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  llm = ChatOpenAI(temperature=0.7, model="gpt-4")
28
- prompt = f"""Expand this market research content for {topic}:
29
  {base_content}
30
 
31
- Provide detailed analysis covering:
32
- 1. Current market state
33
- 2. Growth drivers and barriers
34
- 3. Competitive dynamics
35
- 4. Future outlook
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- Use markdown formatting."""
 
 
 
 
 
 
38
 
39
  return llm.invoke(prompt).content
40
 
41
  def process_crew_output(crew_result, topic):
42
  try:
43
- # Directly use the crew result object
44
  report_text = str(crew_result)
45
  metrics = extract_data_points(report_text)
46
- expanded_content = expand_content(report_text, topic)
 
47
 
48
  return {
49
  'metrics': metrics,
50
- 'content': expanded_content
 
 
51
  }
52
  except Exception as e:
53
  st.error(f"Error processing report: {str(e)}")
54
- return {'metrics': {}, 'content': ''}
55
 
56
  def display_enhanced_report(report):
57
  try:
 
58
  if 'metrics' in report and report['metrics']:
59
- st.subheader("Key Market Metrics")
60
- cols = st.columns(4)
61
  metrics = report['metrics']
62
  cols[0].metric("Market Size", metrics.get('market_size', 'N/A'))
63
- cols[1].metric("CAGR", metrics.get('cagr', 'N/A'))
64
- cols[2].metric("Leader Share", metrics.get('market_share_leader', 'N/A'))
65
- cols[3].metric("Key Players", metrics.get('total_players', 'N/A'))
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- if 'content' in report and report['content']:
68
- st.markdown(report['content'])
 
 
 
 
 
 
69
  except Exception as e:
70
  st.error(f"Error displaying report: {str(e)}")
 
10
 
11
  def extract_data_points(text):
12
  llm = ChatOpenAI(temperature=0, model="gpt-4")
13
+ prompt = """Extract key market metrics as JSON:
14
  {text}
15
 
16
+ Include:
17
+ - Market size (with currency)
18
+ - CAGR
19
+ - Market leader share
20
+ - Number of major players
21
+ - Key regions
22
+ - Dominant segment
23
+
24
+ Format as JSON with these exact keys."""
25
 
26
  response = llm.invoke(prompt.format(text=text))
27
  try:
 
30
  except:
31
  return {}
32
 
33
+ def generate_executive_summary(base_content, topic):
34
+ llm = ChatOpenAI(temperature=0.7, model="gpt-4")
35
+ prompt = f"""Create an executive summary for {topic} market research:
36
+ {base_content}
37
+
38
+ Include:
39
+ 1. Brief market overview (2-3 sentences)
40
+ 2. Key findings (3-4 bullet points)
41
+ 3. Growth drivers
42
+ 4. Market challenges
43
+ 5. Strategic recommendations
44
+
45
+ Format in markdown with clear headers."""
46
+
47
+ return llm.invoke(prompt).content
48
+
49
+ def generate_detailed_analysis(base_content, topic):
50
  llm = ChatOpenAI(temperature=0.7, model="gpt-4")
51
+ prompt = f"""Create detailed market analysis for {topic}:
52
  {base_content}
53
 
54
+ Sections to include:
55
+ 1. Market Overview
56
+ - Current state
57
+ - Size and growth
58
+ - Geographic distribution
59
+
60
+ 2. Competitive Analysis
61
+ - Key players
62
+ - Market shares
63
+ - Competitive strategies
64
+
65
+ 3. Industry Trends
66
+ - Technology trends
67
+ - Consumer trends
68
+ - Regulatory trends
69
+
70
+ 4. Future Outlook
71
+ - Growth projections
72
+ - Emerging opportunities
73
+ - Potential challenges
74
 
75
+ 5. Strategic Recommendations
76
+ - Short-term actions
77
+ - Long-term strategy
78
+ - Risk mitigation
79
+
80
+ Format using markdown with clear sections and subsections.
81
+ Include sources where available."""
82
 
83
  return llm.invoke(prompt).content
84
 
85
  def process_crew_output(crew_result, topic):
86
  try:
 
87
  report_text = str(crew_result)
88
  metrics = extract_data_points(report_text)
89
+ exec_summary = generate_executive_summary(report_text, topic)
90
+ detailed_analysis = generate_detailed_analysis(report_text, topic)
91
 
92
  return {
93
  'metrics': metrics,
94
+ 'executive_summary': exec_summary,
95
+ 'detailed_analysis': detailed_analysis,
96
+ 'raw_content': report_text
97
  }
98
  except Exception as e:
99
  st.error(f"Error processing report: {str(e)}")
100
+ return {'metrics': {}, 'executive_summary': '', 'detailed_analysis': ''}
101
 
102
  def display_enhanced_report(report):
103
  try:
104
+ # Metrics Dashboard
105
  if 'metrics' in report and report['metrics']:
106
+ st.subheader("📊 Key Market Metrics")
107
+ cols = st.columns(3)
108
  metrics = report['metrics']
109
  cols[0].metric("Market Size", metrics.get('market_size', 'N/A'))
110
+ cols[0].metric("CAGR", metrics.get('cagr', 'N/A'))
111
+ cols[1].metric("Market Leader Share", metrics.get('market_leader_share', 'N/A'))
112
+ cols[1].metric("Major Players", metrics.get('major_players', 'N/A'))
113
+ cols[2].metric("Key Region", metrics.get('key_regions', 'N/A'))
114
+ cols[2].metric("Dominant Segment", metrics.get('dominant_segment', 'N/A'))
115
+
116
+ # Executive Summary
117
+ if 'executive_summary' in report and report['executive_summary']:
118
+ with st.expander("📑 Executive Summary", expanded=True):
119
+ st.markdown(report['executive_summary'])
120
+
121
+ # Detailed Analysis
122
+ if 'detailed_analysis' in report and report['detailed_analysis']:
123
+ st.header("📈 Detailed Market Analysis")
124
+ st.markdown(report['detailed_analysis'])
125
 
126
+ # Download Options
127
+ if 'raw_content' in report:
128
+ st.download_button(
129
+ "📥 Download Full Report",
130
+ report['raw_content'],
131
+ "market_research_report.md",
132
+ "text/markdown"
133
+ )
134
  except Exception as e:
135
  st.error(f"Error displaying report: {str(e)}")