DrishtiSharma commited on
Commit
3209527
·
verified ·
1 Parent(s): 2e1a8ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py CHANGED
@@ -70,3 +70,93 @@ elif input_option == "Upload CSV File":
70
  st.session_state.df = pd.read_csv(uploaded_file)
71
  st.success("File uploaded successfully!")
72
  st.dataframe(st.session_state.df.head())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  st.session_state.df = pd.read_csv(uploaded_file)
71
  st.success("File uploaded successfully!")
72
  st.dataframe(st.session_state.df.head())
73
+
74
+
75
+ if st.session_state.df is not None:
76
+ # Database setup
77
+ temp_dir = tempfile.TemporaryDirectory()
78
+ db_path = os.path.join(temp_dir.name, "patent_data.db")
79
+ connection = sqlite3.connect(db_path)
80
+ st.session_state.df.to_sql("patents", connection, if_exists="replace", index=False)
81
+ db = SQLDatabase.from_uri(f"sqlite:///{db_path}")
82
+
83
+ # SQL Tools
84
+ @tool("list_tables")
85
+ def list_tables() -> str:
86
+ """List all tables in the patent database."""
87
+ return ListSQLDatabaseTool(db=db).invoke("")
88
+
89
+ @tool("tables_schema")
90
+ def tables_schema(tables: str) -> str:
91
+ """Get schema and sample rows for given tables."""
92
+ return InfoSQLDatabaseTool(db=db).invoke(tables)
93
+
94
+ @tool("execute_sql")
95
+ def execute_sql(sql_query: str) -> str:
96
+ """Execute a SQL query against the patent database."""
97
+ return QuerySQLDataBaseTool(db=db).invoke(sql_query)
98
+
99
+ # --- CrewAI Agents for Patent Analysis ---
100
+ sql_dev = Agent(
101
+ role="Patent Data Analyst",
102
+ goal="Extract patent data using optimized SQL queries.",
103
+ backstory="An expert in writing optimized SQL queries for complex patent databases.",
104
+ llm=llm,
105
+ tools=[list_tables, tables_schema, execute_sql],
106
+ )
107
+
108
+ data_analyst = Agent(
109
+ role="Patent Analyst",
110
+ goal="Analyze patent claims and decisions for insights.",
111
+ backstory="Experienced in analyzing patent trends and risk factors.",
112
+ llm=llm,
113
+ )
114
+
115
+ report_writer = Agent(
116
+ role="Patent Report Writer",
117
+ goal="Summarize patent insights into a clear report.",
118
+ backstory="Expert in summarizing patent data into comprehensive reports.",
119
+ llm=llm,
120
+ )
121
+
122
+ # --- Crew Tasks ---
123
+ extract_data = Task(
124
+ description="Extract patents related to the query: {query}.",
125
+ expected_output="Patent data matching the query.",
126
+ agent=sql_dev,
127
+ )
128
+
129
+ analyze_data = Task(
130
+ description="Analyze the extracted patent data.",
131
+ expected_output="Detailed analysis of patents.",
132
+ agent=data_analyst,
133
+ context=[extract_data],
134
+ )
135
+
136
+ write_report = Task(
137
+ description="Summarize analysis into an executive report.",
138
+ expected_output="Markdown report of patent insights.",
139
+ agent=report_writer,
140
+ context=[analyze_data],
141
+ )
142
+
143
+ # Assemble Crew
144
+ crew = Crew(
145
+ agents=[sql_dev, data_analyst, report_writer],
146
+ tasks=[extract_data, analyze_data, write_report],
147
+ process=Process.sequential,
148
+ verbose=True,
149
+ )
150
+
151
+ #Query Input for Patent Analysis
152
+ query = st.text_area("Enter Patent Analysis Query:", placeholder="e.g., 'List AI patents filed after 2018.'")
153
+ if st.button("Submit Query"):
154
+ with st.spinner("Processing your query..."):
155
+ inputs = {"query": query}
156
+ result = crew.kickoff(inputs=inputs)
157
+ st.markdown("### 📊 Patent Analysis Report")
158
+ st.markdown(result)
159
+
160
+ temp_dir.cleanup()
161
+ else:
162
+ st.info("Please load a patent dataset to proceed.")