Update app4.py
Browse files
app4.py
CHANGED
@@ -8,6 +8,7 @@ from langchain_openai import ChatOpenAI
|
|
8 |
from langchain.chains import RetrievalQA
|
9 |
from langchain.schema import Document
|
10 |
import os
|
|
|
11 |
|
12 |
# Set title
|
13 |
st.title("Data Analyzer")
|
@@ -141,6 +142,18 @@ if "df" in st.session_state and api_key and pandasai_api_key:
|
|
141 |
st.write("PandasAI Intermediate Output:", agent.last_output)
|
142 |
except Exception as e:
|
143 |
st.error(f"PandasAI encountered an error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
with tab2:
|
146 |
st.subheader("Q&A with RAG")
|
@@ -158,8 +171,6 @@ if "df" in st.session_state and api_key and pandasai_api_key:
|
|
158 |
if viz_question:
|
159 |
try:
|
160 |
result = agent.chat(viz_question)
|
161 |
-
# Extract Python code from PandasAI response
|
162 |
-
import re
|
163 |
code_pattern = r'```python\n(.*?)\n```'
|
164 |
code_match = re.search(code_pattern, result, re.DOTALL)
|
165 |
|
|
|
8 |
from langchain.chains import RetrievalQA
|
9 |
from langchain.schema import Document
|
10 |
import os
|
11 |
+
import re
|
12 |
|
13 |
# Set title
|
14 |
st.title("Data Analyzer")
|
|
|
142 |
st.write("PandasAI Intermediate Output:", agent.last_output)
|
143 |
except Exception as e:
|
144 |
st.error(f"PandasAI encountered an error: {str(e)}")
|
145 |
+
# Fallback: Direct pandas filtering
|
146 |
+
if "patent_number" in pandas_question.lower() and "decision" in pandas_question.lower():
|
147 |
+
try:
|
148 |
+
match = re.search(r'\d{7,}', pandas_question)
|
149 |
+
if match:
|
150 |
+
patent_number = match.group()
|
151 |
+
decision = df.loc[df['patent_number'] == int(patent_number), 'decision']
|
152 |
+
st.write(f"Fallback Answer: The decision for patent {patent_number} is '{decision.iloc[0]}'.")
|
153 |
+
else:
|
154 |
+
st.write("Could not extract patent number from the query.")
|
155 |
+
except Exception as fallback_error:
|
156 |
+
st.error(f"Fallback processing failed: {fallback_error}")
|
157 |
|
158 |
with tab2:
|
159 |
st.subheader("Q&A with RAG")
|
|
|
171 |
if viz_question:
|
172 |
try:
|
173 |
result = agent.chat(viz_question)
|
|
|
|
|
174 |
code_pattern = r'```python\n(.*?)\n```'
|
175 |
code_match = re.search(code_pattern, result, re.DOTALL)
|
176 |
|