added csv-code executor
Browse files- controller.py +16 -3
controller.py
CHANGED
@@ -14,7 +14,7 @@ import pandas as pd
|
|
14 |
from pandasai import SmartDataframe
|
15 |
from langchain_groq.chat_models import ChatGroq
|
16 |
from dotenv import load_dotenv
|
17 |
-
from pydantic import BaseModel, Field
|
18 |
from csv_service import clean_data, extract_chart_filenames, generate_csv_data, get_csv_basic_info
|
19 |
from urllib.parse import unquote
|
20 |
from langchain_groq import ChatGroq
|
@@ -212,24 +212,37 @@ class ExecutionRequest(BaseModel):
|
|
212 |
response_type: Literal["casual", "data_analysis", "visualization", "mixed"]
|
213 |
codeExecutionPayload: CsvChatResult
|
214 |
|
|
|
215 |
@app.post("/api/code_execution_csv")
|
216 |
async def code_execution_csv(
|
217 |
-
|
218 |
authorization: Optional[str] = Header(None)
|
219 |
):
|
|
|
220 |
expected_token = os.environ.get("AUTH_TOKEN")
|
221 |
if not authorization or not expected_token or authorization.replace("Bearer ", "") != expected_token:
|
222 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
223 |
|
224 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
decoded_url = unquote(request.csv_url)
|
226 |
df = clean_data(decoded_url)
|
227 |
executor = PythonExecutor(df)
|
228 |
-
|
229 |
formatted_output = await executor.process_response(request.codeExecutionPayload, request.chat_id)
|
230 |
return {"answer": formatted_output}
|
231 |
|
232 |
except Exception as e:
|
|
|
233 |
return {"error": "Failed to execute request", "message": str(e)}
|
234 |
|
235 |
|
|
|
14 |
from pandasai import SmartDataframe
|
15 |
from langchain_groq.chat_models import ChatGroq
|
16 |
from dotenv import load_dotenv
|
17 |
+
from pydantic import BaseModel, Field, ValidationError
|
18 |
from csv_service import clean_data, extract_chart_filenames, generate_csv_data, get_csv_basic_info
|
19 |
from urllib.parse import unquote
|
20 |
from langchain_groq import ChatGroq
|
|
|
212 |
response_type: Literal["casual", "data_analysis", "visualization", "mixed"]
|
213 |
codeExecutionPayload: CsvChatResult
|
214 |
|
215 |
+
|
216 |
@app.post("/api/code_execution_csv")
|
217 |
async def code_execution_csv(
|
218 |
+
request_data: dict, # Change from ExecutionRequest to dict to see raw input
|
219 |
authorization: Optional[str] = Header(None)
|
220 |
):
|
221 |
+
# Auth check remains the same
|
222 |
expected_token = os.environ.get("AUTH_TOKEN")
|
223 |
if not authorization or not expected_token or authorization.replace("Bearer ", "") != expected_token:
|
224 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
225 |
|
226 |
try:
|
227 |
+
# First log the incoming request data
|
228 |
+
print("Incoming request data:", request_data)
|
229 |
+
|
230 |
+
# Then validate
|
231 |
+
try:
|
232 |
+
request = ExecutionRequest(**request_data)
|
233 |
+
except ValidationError as e:
|
234 |
+
print("Validation error:", e.json())
|
235 |
+
raise HTTPException(status_code=422, detail=e.errors())
|
236 |
+
|
237 |
+
# Rest of your processing logic...
|
238 |
decoded_url = unquote(request.csv_url)
|
239 |
df = clean_data(decoded_url)
|
240 |
executor = PythonExecutor(df)
|
|
|
241 |
formatted_output = await executor.process_response(request.codeExecutionPayload, request.chat_id)
|
242 |
return {"answer": formatted_output}
|
243 |
|
244 |
except Exception as e:
|
245 |
+
print("Processing error:", str(e))
|
246 |
return {"error": "Failed to execute request", "message": str(e)}
|
247 |
|
248 |
|