Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,48 +6,58 @@ from fpdf import FPDF
|
|
6 |
import pandas as pd
|
7 |
import plotly.express as px
|
8 |
import time
|
9 |
-
import datetime
|
10 |
-
from patent_data_api import fetch_patent_data
|
11 |
|
|
|
12 |
st.title("🤖 Patent Insights Consultant")
|
13 |
|
14 |
st.sidebar.write(
|
15 |
"This Patent Insights Consultant uses a multi-agent system to provide actionable insights and data analysis for the patent domain."
|
16 |
)
|
17 |
|
|
|
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 |
st.sidebar.subheader("Advanced Options")
|
22 |
|
23 |
-
|
24 |
-
start_date = st.sidebar.date_input("Start Date", value=datetime.date(2018, 1, 1))
|
25 |
-
end_date = st.sidebar.date_input("End Date", value=datetime.date.today())
|
26 |
-
|
27 |
enable_advanced_analysis = st.sidebar.checkbox("Enable Advanced Analysis", value=True)
|
28 |
enable_custom_visualization = st.sidebar.checkbox("Enable Custom Visualizations", value=True)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
)
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile")
|
50 |
|
|
|
|
|
|
|
|
|
51 |
planner = Agent(
|
52 |
role="Patent Research Consultant",
|
53 |
goal=planner_goal,
|
@@ -81,6 +91,10 @@ analyst = Agent(
|
|
81 |
llm=llm
|
82 |
)
|
83 |
|
|
|
|
|
|
|
|
|
84 |
plan = Task(
|
85 |
description=(
|
86 |
"1. Research recent trends in {topic} patent filings and innovation.\n"
|
@@ -113,6 +127,10 @@ analyse = Task(
|
|
113 |
agent=analyst
|
114 |
)
|
115 |
|
|
|
|
|
|
|
|
|
116 |
crew = Crew(
|
117 |
agents=[planner, analyst, writer],
|
118 |
tasks=[plan, analyse, write],
|
@@ -120,29 +138,34 @@ crew = Crew(
|
|
120 |
)
|
121 |
|
122 |
def generate_pdf_report(result):
|
|
|
123 |
pdf = FPDF()
|
124 |
pdf.add_page()
|
125 |
pdf.set_font("Arial", size=12)
|
126 |
pdf.set_auto_page_break(auto=True, margin=15)
|
127 |
|
|
|
128 |
pdf.set_font("Arial", size=16, style="B")
|
129 |
pdf.cell(200, 10, txt="Patent Insights Report", ln=True, align="C")
|
130 |
pdf.ln(10)
|
131 |
|
|
|
132 |
pdf.set_font("Arial", size=12)
|
133 |
pdf.multi_cell(0, 10, txt=result)
|
134 |
|
|
|
135 |
report_path = "Patent_Insights_Report.pdf"
|
136 |
pdf.output(report_path)
|
137 |
return report_path
|
138 |
|
139 |
def create_visualizations(analyst_output):
|
|
|
140 |
if enable_custom_visualization and analyst_output:
|
141 |
try:
|
142 |
data = pd.DataFrame(analyst_output)
|
143 |
st.markdown("### Advanced Visualization")
|
144 |
|
145 |
-
fig = px.
|
146 |
st.plotly_chart(fig)
|
147 |
except Exception as e:
|
148 |
st.warning(f"Failed to create visualizations: {e}")
|
@@ -151,14 +174,10 @@ if st.button("Generate Patent Insights"):
|
|
151 |
with st.spinner('Processing...'):
|
152 |
try:
|
153 |
start_time = time.time()
|
154 |
-
|
155 |
-
real_time_data = fetch_patent_data(
|
156 |
-
technology_area=patent_area, region=selected_region, start_date=start_date, end_date=end_date
|
157 |
-
)
|
158 |
-
|
159 |
-
results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder, "data": real_time_data})
|
160 |
elapsed_time = time.time() - start_time
|
161 |
|
|
|
162 |
st.markdown("### Final Report:")
|
163 |
writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
|
164 |
if writer_output:
|
@@ -166,28 +185,34 @@ if st.button("Generate Patent Insights"):
|
|
166 |
else:
|
167 |
st.warning("No final report available.")
|
168 |
|
|
|
169 |
with st.expander("Explore Detailed Insights"):
|
170 |
tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
|
171 |
|
|
|
172 |
with tab1:
|
173 |
st.markdown("### Planner's Insights")
|
174 |
planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
|
175 |
st.write(planner_output)
|
176 |
|
|
|
177 |
with tab2:
|
178 |
st.markdown("### Analyst's Analysis")
|
179 |
analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
|
180 |
st.write(analyst_output)
|
181 |
|
|
|
182 |
if enable_advanced_analysis:
|
183 |
create_visualizations(analyst_output)
|
184 |
|
|
|
185 |
st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
|
186 |
token_usage = getattr(results, "token_usage", None)
|
187 |
if token_usage:
|
188 |
st.markdown("#### Token Usage")
|
189 |
st.json(token_usage)
|
190 |
|
|
|
191 |
if writer_output:
|
192 |
report_path = generate_pdf_report(writer_output)
|
193 |
with open(report_path, "rb") as report_file:
|
@@ -196,5 +221,6 @@ if st.button("Generate Patent Insights"):
|
|
196 |
except Exception as e:
|
197 |
st.error(f"An error occurred during execution: {e}")
|
198 |
|
|
|
199 |
st.sidebar.markdown("---")
|
200 |
st.sidebar.markdown("### Reference:")
|
|
|
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 |
+
# Advanced Options
|
22 |
st.sidebar.subheader("Advanced Options")
|
23 |
|
24 |
+
# Enable/Disable Advanced Features
|
|
|
|
|
|
|
25 |
enable_advanced_analysis = st.sidebar.checkbox("Enable Advanced Analysis", value=True)
|
26 |
enable_custom_visualization = st.sidebar.checkbox("Enable Custom Visualizations", value=True)
|
27 |
|
28 |
+
# Optional Customization
|
29 |
+
st.sidebar.subheader("Agent Customization")
|
30 |
+
|
31 |
+
# Display customization section in a collapsible expander
|
32 |
+
with st.sidebar.expander("Customize Agent Goals", expanded=False):
|
33 |
+
enable_customization = st.checkbox("Enable Custom Goals")
|
34 |
+
if enable_customization:
|
35 |
+
planner_goal = st.text_area(
|
36 |
+
"Planner Goal",
|
37 |
+
value="Research trends in patent filings and technological innovation, identify key players, and provide strategic recommendations."
|
38 |
+
)
|
39 |
+
writer_goal = st.text_area(
|
40 |
+
"Writer Goal",
|
41 |
+
value="Craft a professional insights document summarizing trends, strategies, and actionable outcomes for stakeholders."
|
42 |
+
)
|
43 |
+
analyst_goal = st.text_area(
|
44 |
+
"Analyst Goal",
|
45 |
+
value="Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution."
|
46 |
+
)
|
47 |
+
else:
|
48 |
+
planner_goal = "Research trends in patent filings and technological innovation, identify key players, and provide strategic recommendations."
|
49 |
+
writer_goal = "Craft a professional insights document summarizing trends, strategies, and actionable outcomes for stakeholders."
|
50 |
+
analyst_goal = "Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution."
|
51 |
+
|
52 |
+
#=================
|
53 |
+
# LLM Object
|
54 |
+
#=================
|
55 |
llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile")
|
56 |
|
57 |
+
#=================
|
58 |
+
# Crew Agents
|
59 |
+
#=================
|
60 |
+
|
61 |
planner = Agent(
|
62 |
role="Patent Research Consultant",
|
63 |
goal=planner_goal,
|
|
|
91 |
llm=llm
|
92 |
)
|
93 |
|
94 |
+
#=================
|
95 |
+
# Crew Tasks
|
96 |
+
#=================
|
97 |
+
|
98 |
plan = Task(
|
99 |
description=(
|
100 |
"1. Research recent trends in {topic} patent filings and innovation.\n"
|
|
|
127 |
agent=analyst
|
128 |
)
|
129 |
|
130 |
+
#=================
|
131 |
+
# Execution
|
132 |
+
#=================
|
133 |
+
|
134 |
crew = Crew(
|
135 |
agents=[planner, analyst, writer],
|
136 |
tasks=[plan, analyse, write],
|
|
|
138 |
)
|
139 |
|
140 |
def generate_pdf_report(result):
|
141 |
+
"""Generate a professional PDF report from the Crew output."""
|
142 |
pdf = FPDF()
|
143 |
pdf.add_page()
|
144 |
pdf.set_font("Arial", size=12)
|
145 |
pdf.set_auto_page_break(auto=True, margin=15)
|
146 |
|
147 |
+
# Title
|
148 |
pdf.set_font("Arial", size=16, style="B")
|
149 |
pdf.cell(200, 10, txt="Patent Insights Report", ln=True, align="C")
|
150 |
pdf.ln(10)
|
151 |
|
152 |
+
# Content
|
153 |
pdf.set_font("Arial", size=12)
|
154 |
pdf.multi_cell(0, 10, txt=result)
|
155 |
|
156 |
+
# Save PDF
|
157 |
report_path = "Patent_Insights_Report.pdf"
|
158 |
pdf.output(report_path)
|
159 |
return report_path
|
160 |
|
161 |
def create_visualizations(analyst_output):
|
162 |
+
"""Create visualizations for advanced insights."""
|
163 |
if enable_custom_visualization and analyst_output:
|
164 |
try:
|
165 |
data = pd.DataFrame(analyst_output)
|
166 |
st.markdown("### Advanced Visualization")
|
167 |
|
168 |
+
fig = px.bar(data, x="Category", y="Values", title="Patent Trends by Category")
|
169 |
st.plotly_chart(fig)
|
170 |
except Exception as e:
|
171 |
st.warning(f"Failed to create visualizations: {e}")
|
|
|
174 |
with st.spinner('Processing...'):
|
175 |
try:
|
176 |
start_time = time.time()
|
177 |
+
results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
|
|
|
|
|
|
|
|
|
|
|
178 |
elapsed_time = time.time() - start_time
|
179 |
|
180 |
+
# Display Final Report (Writer's Output)
|
181 |
st.markdown("### Final Report:")
|
182 |
writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
|
183 |
if writer_output:
|
|
|
185 |
else:
|
186 |
st.warning("No final report available.")
|
187 |
|
188 |
+
# Option for Detailed Insights
|
189 |
with st.expander("Explore Detailed Insights"):
|
190 |
tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
|
191 |
|
192 |
+
# Planner's Output
|
193 |
with tab1:
|
194 |
st.markdown("### Planner's Insights")
|
195 |
planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
|
196 |
st.write(planner_output)
|
197 |
|
198 |
+
# Analyst's Output
|
199 |
with tab2:
|
200 |
st.markdown("### Analyst's Analysis")
|
201 |
analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
|
202 |
st.write(analyst_output)
|
203 |
|
204 |
+
# Generate visualizations if enabled
|
205 |
if enable_advanced_analysis:
|
206 |
create_visualizations(analyst_output)
|
207 |
|
208 |
+
# Display Token Usage and Execution Time
|
209 |
st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
|
210 |
token_usage = getattr(results, "token_usage", None)
|
211 |
if token_usage:
|
212 |
st.markdown("#### Token Usage")
|
213 |
st.json(token_usage)
|
214 |
|
215 |
+
# Generate PDF Report
|
216 |
if writer_output:
|
217 |
report_path = generate_pdf_report(writer_output)
|
218 |
with open(report_path, "rb") as report_file:
|
|
|
221 |
except Exception as e:
|
222 |
st.error(f"An error occurred during execution: {e}")
|
223 |
|
224 |
+
# Add reference and credits in the sidebar
|
225 |
st.sidebar.markdown("---")
|
226 |
st.sidebar.markdown("### Reference:")
|