added csv-code executor
Browse files- controller.py +10 -45
- python_code_executor_service.py +1 -0
controller.py
CHANGED
@@ -7,14 +7,14 @@ import threading
|
|
7 |
import uuid
|
8 |
from fastapi import FastAPI, HTTPException, Header
|
9 |
from fastapi.encoders import jsonable_encoder
|
10 |
-
from typing import Dict, List, Optional
|
11 |
from fastapi.responses import FileResponse
|
12 |
import numpy as np
|
13 |
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
|
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
|
@@ -28,7 +28,7 @@ import seaborn as sns
|
|
28 |
from gemini_report_generator import generate_csv_report
|
29 |
from intitial_q_handler import if_initial_chart_question, if_initial_chat_question
|
30 |
from orchestrator_agent import csv_orchestrator_chat
|
31 |
-
from python_code_executor_service import PythonExecutor
|
32 |
from supabase_service import upload_file_to_supabase
|
33 |
from cerebras_csv_agent import query_csv_agent
|
34 |
from util_service import _prompt_generator, process_answer
|
@@ -206,66 +206,31 @@ async def get_csv_data(request: CsvUrlRequest):
|
|
206 |
raise HTTPException(status_code=400, detail=f"Failed to retrieve CSV data: {str(e)}")
|
207 |
|
208 |
# EXECUTE THE PYTHON CODE
|
209 |
-
class CodeResponse(BaseModel):
|
210 |
-
language: str
|
211 |
-
code: str
|
212 |
-
|
213 |
-
class AnalysisOperation(BaseModel):
|
214 |
-
code: CodeResponse
|
215 |
-
resultVar: str
|
216 |
-
|
217 |
-
class ChartSpecification(BaseModel):
|
218 |
-
imageDescription: str
|
219 |
-
code: Optional[str] = None
|
220 |
-
|
221 |
-
class CsvChatResult(BaseModel):
|
222 |
-
casualResponse: str
|
223 |
-
analysisOperations: List[AnalysisOperation]
|
224 |
-
charts: Optional[List[ChartSpecification]] = None
|
225 |
-
|
226 |
class ExecutionRequest(BaseModel):
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
class CsvChatResultPython(BaseModel):
|
232 |
-
"""Structured response for CSV-related AI interactions"""
|
233 |
-
casual_response: str
|
234 |
-
analysis_operations: List[AnalysisOperation]
|
235 |
-
charts: Optional[List[ChartSpecification]] = None
|
236 |
|
237 |
@app.post("/api/code_execution_csv")
|
238 |
async def code_execution_csv(
|
239 |
request: ExecutionRequest,
|
240 |
authorization: Optional[str] = Header(None)
|
241 |
):
|
242 |
-
# Token validation
|
243 |
expected_token = os.environ.get("AUTH_TOKEN")
|
244 |
if not authorization or not expected_token or authorization.replace("Bearer ", "") != expected_token:
|
245 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
246 |
|
247 |
try:
|
248 |
-
|
249 |
-
# Initialize executor with DataFrame
|
250 |
-
csv_url = request.csvUrl
|
251 |
-
decoded_url = unquote(csv_url)
|
252 |
df = clean_data(decoded_url)
|
253 |
executor = PythonExecutor(df)
|
254 |
-
result_body = CsvChatResultPython(
|
255 |
-
casual_response=request.result.casualResponse,
|
256 |
-
analysis_operations=request.result.analysisOperations,
|
257 |
-
charts=request.result.charts
|
258 |
-
)
|
259 |
|
260 |
-
formatted_output = await executor.process_response(
|
261 |
-
logger.info("Formatted Output:", formatted_output)
|
262 |
return {"answer": formatted_output}
|
263 |
|
264 |
except Exception as e:
|
265 |
-
return {
|
266 |
-
"error": "Failed to execute request",
|
267 |
-
"message": str(e)
|
268 |
-
}
|
269 |
|
270 |
|
271 |
# CHAT CODING STARTS FROM HERE
|
|
|
7 |
import uuid
|
8 |
from fastapi import FastAPI, HTTPException, Header
|
9 |
from fastapi.encoders import jsonable_encoder
|
10 |
+
from typing import Dict, List, Literal, Optional
|
11 |
from fastapi.responses import FileResponse
|
12 |
import numpy as np
|
13 |
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
|
|
|
28 |
from gemini_report_generator import generate_csv_report
|
29 |
from intitial_q_handler import if_initial_chart_question, if_initial_chat_question
|
30 |
from orchestrator_agent import csv_orchestrator_chat
|
31 |
+
from python_code_executor_service import CsvChatResult, PythonExecutor
|
32 |
from supabase_service import upload_file_to_supabase
|
33 |
from cerebras_csv_agent import query_csv_agent
|
34 |
from util_service import _prompt_generator, process_answer
|
|
|
206 |
raise HTTPException(status_code=400, detail=f"Failed to retrieve CSV data: {str(e)}")
|
207 |
|
208 |
# EXECUTE THE PYTHON CODE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
class ExecutionRequest(BaseModel):
|
210 |
+
chat_id: str = Field(..., alias="chat_id")
|
211 |
+
csv_url: str = Field(..., alias="csv_url")
|
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 |
request: ExecutionRequest,
|
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 |
|
236 |
# CHAT CODING STARTS FROM HERE
|
python_code_executor_service.py
CHANGED
@@ -42,6 +42,7 @@ class AnalysisOperation(BaseModel):
|
|
42 |
|
43 |
class CsvChatResult(BaseModel):
|
44 |
"""Structured response for CSV-related AI interactions"""
|
|
|
45 |
casual_response: str
|
46 |
analysis_operations: List[AnalysisOperation]
|
47 |
charts: Optional[List[ChartSpecification]] = None
|
|
|
42 |
|
43 |
class CsvChatResult(BaseModel):
|
44 |
"""Structured response for CSV-related AI interactions"""
|
45 |
+
response_type: Literal["casual", "data_analysis", "visualization", "mixed"]
|
46 |
casual_response: str
|
47 |
analysis_operations: List[AnalysisOperation]
|
48 |
charts: Optional[List[ChartSpecification]] = None
|