DrishtiSharma commited on
Commit
2c8ec9f
·
verified ·
1 Parent(s): 13808af

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +203 -0
app.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from crewai import Agent, Task, Crew
3
+ import os
4
+ from langchain_groq import ChatGroq
5
+ from fpdf import FPDF
6
+ import pandas as pd
7
+ import plotly.express as px
8
+ import time
9
+
10
+ # Title and Sidebar
11
+ st.title("Patent Insights Consultant")
12
+
13
+ st.sidebar.write(
14
+ "This Patent Insights Consultant uses a multi-agent system to provide actionable insights and data analysis for the patent domain."
15
+ )
16
+
17
+ # User Inputs
18
+ patent_area = st.text_input('Enter Patent Technology Area', value="Artificial Intelligence")
19
+ stakeholder = st.text_input('Enter Stakeholder', value="Patent Attorneys")
20
+
21
+ # Optional Customization
22
+ st.sidebar.subheader("Agent Customization")
23
+
24
+ # Display customization section in a collapsible expander
25
+ with st.sidebar.expander("Customize Agent Goals", expanded=False):
26
+ enable_customization = st.checkbox("Enable Custom Goals")
27
+ if enable_customization:
28
+ planner_goal = st.text_area(
29
+ "Planner Goal",
30
+ value="Research trends in patent filings and technological innovation, identify key players, and provide strategic recommendations."
31
+ )
32
+ writer_goal = st.text_area(
33
+ "Writer Goal",
34
+ value="Craft a professional insights document summarizing trends, strategies, and actionable outcomes for stakeholders."
35
+ )
36
+ analyst_goal = st.text_area(
37
+ "Analyst Goal",
38
+ value="Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution."
39
+ )
40
+ else:
41
+ planner_goal = "Research trends in patent filings and technological innovation, identify key players, and provide strategic recommendations."
42
+ writer_goal = "Craft a professional insights document summarizing trends, strategies, and actionable outcomes for stakeholders."
43
+ analyst_goal = "Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution."
44
+
45
+ #=================
46
+ # LLM Object
47
+ #=================
48
+ llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile")
49
+
50
+ #=================
51
+ # Crew Agents
52
+ #=================
53
+
54
+ planner = Agent(
55
+ role="Patent Research Consultant",
56
+ goal=planner_goal,
57
+ backstory=(
58
+ "You're tasked with researching {topic} patents and identifying key trends and players. Your work supports the Patent Writer and Data Analyst."
59
+ ),
60
+ allow_delegation=False,
61
+ verbose=True,
62
+ llm=llm
63
+ )
64
+
65
+ writer = Agent(
66
+ role="Patent Insights Writer",
67
+ goal=writer_goal,
68
+ backstory=(
69
+ "Using the research from the Planner and data from the Analyst, craft a professional document summarizing patent insights for {stakeholder}."
70
+ ),
71
+ allow_delegation=False,
72
+ verbose=True,
73
+ llm=llm
74
+ )
75
+
76
+ analyst = Agent(
77
+ role="Patent Data Analyst",
78
+ goal=analyst_goal,
79
+ backstory=(
80
+ "Analyze patent filing data and innovation trends in {topic} to provide statistical insights. Your analysis will guide the Writer's final report."
81
+ ),
82
+ allow_delegation=False,
83
+ verbose=True,
84
+ llm=llm
85
+ )
86
+
87
+ #=================
88
+ # Crew Tasks
89
+ #=================
90
+
91
+ plan = Task(
92
+ description=(
93
+ "1. Research recent trends in {topic} patent filings and innovation.\n"
94
+ "2. Identify key players and emerging technologies.\n"
95
+ "3. Provide recommendations for stakeholders on strategic directions.\n"
96
+ "4. Limit the output to 500 words."
97
+ ),
98
+ expected_output="A research document with structured insights and strategic recommendations.",
99
+ agent=planner
100
+ )
101
+
102
+ write = Task(
103
+ description=(
104
+ "1. Use the Planner's and Analyst's outputs to craft a professional patent insights document.\n"
105
+ "2. Include key findings, visual aids, and actionable strategies.\n"
106
+ "3. Align content with stakeholder goals.\n"
107
+ "4. Limit the document to 400 words."
108
+ ),
109
+ expected_output="A polished, stakeholder-ready patent insights document.",
110
+ agent=writer
111
+ )
112
+
113
+ analyse = Task(
114
+ description=(
115
+ "1. Perform statistical analysis of patent filing trends, innovation hot spots, and growth projections.\n"
116
+ "2. Collaborate with the Planner and Writer to align on data needs.\n"
117
+ "3. Present findings in an actionable format."
118
+ ),
119
+ expected_output="A detailed statistical analysis with actionable insights for stakeholders.",
120
+ agent=analyst
121
+ )
122
+
123
+ #=================
124
+ # Execution
125
+ #=================
126
+
127
+ crew = Crew(
128
+ agents=[planner, analyst, writer],
129
+ tasks=[plan, analyse, write],
130
+ verbose=True
131
+ )
132
+
133
+ def generate_pdf_report(result):
134
+ """Generate a professional PDF report from the Crew output."""
135
+ pdf = FPDF()
136
+ pdf.add_page()
137
+ pdf.set_font("Arial", size=12)
138
+ pdf.set_auto_page_break(auto=True, margin=15)
139
+
140
+ # Title
141
+ pdf.set_font("Arial", size=16, style="B")
142
+ pdf.cell(200, 10, txt="Patent Insights Report", ln=True, align="C")
143
+ pdf.ln(10)
144
+
145
+ # Content
146
+ pdf.set_font("Arial", size=12)
147
+ pdf.multi_cell(0, 10, txt=result)
148
+
149
+ # Save PDF
150
+ report_path = "Patent_Insights_Report.pdf"
151
+ pdf.output(report_path)
152
+ return report_path
153
+
154
+ if st.button("Generate Patent Insights"):
155
+ with st.spinner('Processing...'):
156
+ try:
157
+ start_time = time.time()
158
+ results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
159
+ elapsed_time = time.time() - start_time
160
+
161
+ # Display Final Report (Writer's Output)
162
+ st.markdown("### Final Report:")
163
+ writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
164
+ if writer_output:
165
+ st.write(writer_output)
166
+ else:
167
+ st.warning("No final report available.")
168
+
169
+ # Option for Detailed Insights
170
+ with st.expander("Explore Detailed Insights"):
171
+ tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
172
+
173
+ # Planner's Output
174
+ with tab1:
175
+ st.markdown("### Planner's Insights")
176
+ planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
177
+ st.write(planner_output)
178
+
179
+ # Analyst's Output
180
+ with tab2:
181
+ st.markdown("### Analyst's Analysis")
182
+ analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
183
+ st.write(analyst_output)
184
+
185
+ # Display Token Usage and Execution Time
186
+ st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
187
+ token_usage = getattr(results, "token_usage", None)
188
+ if token_usage:
189
+ st.markdown("#### Token Usage")
190
+ st.json(token_usage)
191
+
192
+ # Generate PDF Report
193
+ if writer_output:
194
+ report_path = generate_pdf_report(writer_output)
195
+ with open(report_path, "rb") as report_file:
196
+ st.download_button("Download Report", data=report_file, file_name="Patent_Report.pdf")
197
+
198
+ except Exception as e:
199
+ st.error(f"An error occurred during execution: {e}")
200
+
201
+ # Add reference and credits in the sidebar
202
+ st.sidebar.markdown("---")
203
+ st.sidebar.markdown("### Reference:")