|
|
|
from concurrent.futures import ProcessPoolExecutor |
|
import logging |
|
import os |
|
import asyncio |
|
import threading |
|
import uuid |
|
from fastapi import FastAPI, HTTPException, Header |
|
from fastapi.encoders import jsonable_encoder |
|
from typing import Dict, List |
|
from fastapi.responses import FileResponse |
|
import numpy as np |
|
import pandas as pd |
|
from pandasai import SmartDataframe |
|
from langchain_groq.chat_models import ChatGroq |
|
from dotenv import load_dotenv |
|
from pydantic import BaseModel |
|
from csv_service import clean_data, extract_chart_filenames, generate_csv_data, get_csv_basic_info |
|
from urllib.parse import unquote |
|
from langchain_groq import ChatGroq |
|
import pandas as pd |
|
from langchain_experimental.tools import PythonAstREPLTool |
|
from langchain_experimental.agents import create_pandas_dataframe_agent |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import matplotlib |
|
import seaborn as sns |
|
from gemini_report_generator import generate_csv_report |
|
from intitial_q_handler import if_initial_chart_question, if_initial_chat_question |
|
from orchestrator_agent import csv_orchestrator_chat |
|
from supabase_service import upload_file_to_supabase |
|
from util_service import _prompt_generator, process_answer |
|
from fastapi.middleware.cors import CORSMiddleware |
|
import matplotlib |
|
matplotlib.use('Agg') |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
max_cpus = os.cpu_count() |
|
logger.info(f"Max CPUs: {max_cpus}") |
|
|
|
|
|
os.makedirs("/app/cache", exist_ok=True) |
|
|
|
os.makedirs("/app", exist_ok=True) |
|
open("/app/pandasai.log", "a").close() |
|
|
|
|
|
os.makedirs("/app/generated_charts", exist_ok=True) |
|
|
|
load_dotenv() |
|
|
|
image_file_path = os.getenv("IMAGE_FILE_PATH") |
|
image_not_found = os.getenv("IMAGE_NOT_FOUND") |
|
allowed_hosts = os.getenv("ALLOWED_HOSTS", "").split(",") |
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=allowed_hosts, |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
groq_api_keys = os.getenv("GROQ_API_KEYS").split(",") |
|
model_name = os.getenv("GROQ_LLM_MODEL") |
|
|
|
class CsvUrlRequest(BaseModel): |
|
csv_url: str |
|
|
|
class ImageRequest(BaseModel): |
|
image_path: str |
|
chat_id: str |
|
|
|
class FileProps(BaseModel): |
|
fileName: str |
|
filePath: str |
|
fileType: str |
|
|
|
class Files(BaseModel): |
|
csv_files: List[FileProps] |
|
image_files: List[FileProps] |
|
|
|
class FileBoxProps(BaseModel): |
|
files: Files |
|
|
|
dummy_response = FileBoxProps( |
|
files=Files( |
|
csv_files=[ |
|
FileProps( |
|
fileName="sales_data.csv", |
|
filePath="/downloads/sales_data.csv", |
|
fileType="csv" |
|
), |
|
FileProps( |
|
fileName="customer_data.csv", |
|
filePath="/downloads/customer_data.csv", |
|
fileType="csv" |
|
) |
|
], |
|
image_files=[ |
|
FileProps( |
|
fileName="chart.png", |
|
filePath="/downloads/chart.png", |
|
fileType="image" |
|
), |
|
FileProps( |
|
fileName="graph.jpg", |
|
filePath="/downloads/graph.jpg", |
|
fileType="image" |
|
) |
|
] |
|
) |
|
) |
|
|
|
|
|
|
|
current_groq_key_index = 0 |
|
current_groq_key_lock = threading.Lock() |
|
|
|
|
|
current_langchain_key_index = 0 |
|
current_langchain_key_lock = threading.Lock() |
|
|
|
|
|
|
|
@app.get("/ping") |
|
async def root(): |
|
return {"message": "Pong !!"} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/basic_csv_data") |
|
async def basic_csv_data(request: CsvUrlRequest): |
|
try: |
|
decoded_url = unquote(request.csv_url) |
|
logger.info(f"Fetching CSV data from URL: {decoded_url}") |
|
|
|
|
|
loop = asyncio.get_running_loop() |
|
csv_data = await loop.run_in_executor( |
|
process_executor, get_csv_basic_info, decoded_url |
|
) |
|
logger.info(f"CSV data fetched successfully: {csv_data}") |
|
return {"data": csv_data} |
|
except Exception as e: |
|
logger.error(f"Error while fetching CSV data: {e}") |
|
raise HTTPException(status_code=400, detail=f"Failed to retrieve CSV data: {str(e)}") |
|
|
|
|
|
|
|
@app.post("/api/get-chart") |
|
async def get_image(request: ImageRequest, authorization: str = Header(None)): |
|
if not authorization: |
|
raise HTTPException(status_code=401, detail="Authorization header missing") |
|
|
|
if not authorization.startswith("Bearer "): |
|
raise HTTPException(status_code=401, detail="Invalid authorization header format") |
|
|
|
token = authorization.split(" ")[1] |
|
if not token: |
|
raise HTTPException(status_code=401, detail="Token missing") |
|
if token != os.getenv("AUTH_TOKEN"): |
|
raise HTTPException(status_code=403, detail="Invalid token") |
|
|
|
try: |
|
logger.info("Groq Chat created a chat for the user query...") |
|
image_file_path = request.image_path |
|
unique_file_name =f'{str(uuid.uuid4())}.png' |
|
logger.info("Uploading the chart to supabase...") |
|
image_public_url = await upload_file_to_supabase(f"{image_file_path}", unique_file_name, chat_id=request.chat_id) |
|
logger.info("Image uploaded to Supabase and Image URL is... ", {image_public_url}) |
|
os.remove(image_file_path) |
|
return {"image_url": image_public_url} |
|
|
|
except Exception as e: |
|
logger.error(f"Error: {e}") |
|
return {"answer": "error"} |
|
|
|
|
|
|
|
@app.post("/api/csv_data") |
|
async def get_csv_data(request: CsvUrlRequest): |
|
try: |
|
decoded_url = unquote(request.csv_url) |
|
logger.info(f"Fetching CSV data from URL: {decoded_url}") |
|
|
|
loop = asyncio.get_running_loop() |
|
csv_data = await loop.run_in_executor( |
|
process_executor, generate_csv_data, decoded_url |
|
) |
|
return csv_data |
|
except Exception as e: |
|
logger.error(f"Error while fetching CSV data: {e}") |
|
raise HTTPException(status_code=400, detail=f"Failed to retrieve CSV data: {str(e)}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def groq_chat(csv_url: str, question: str): |
|
global current_groq_key_index, current_groq_key_lock |
|
|
|
while True: |
|
with current_groq_key_lock: |
|
if current_groq_key_index >= len(groq_api_keys): |
|
return {"error": "All API keys exhausted."} |
|
current_api_key = groq_api_keys[current_groq_key_index] |
|
|
|
try: |
|
|
|
data = clean_data(csv_url) |
|
llm = ChatGroq(model=model_name, api_key=current_api_key) |
|
|
|
chart_filename = f"chart_{uuid.uuid4()}.png" |
|
chart_path = os.path.join("generated_charts", chart_filename) |
|
|
|
|
|
df = SmartDataframe( |
|
data, |
|
config={ |
|
'llm': llm, |
|
'save_charts': True, |
|
'open_charts': False, |
|
'save_charts_path': os.path.dirname(chart_path), |
|
'custom_chart_filename': chart_filename, |
|
'enable_cache': False |
|
} |
|
) |
|
|
|
answer = df.chat(question) |
|
|
|
|
|
if isinstance(answer, pd.DataFrame): |
|
processed = answer.apply(handle_out_of_range_float).to_dict(orient="records") |
|
elif isinstance(answer, pd.Series): |
|
processed = answer.apply(handle_out_of_range_float).to_dict() |
|
elif isinstance(answer, list): |
|
processed = [handle_out_of_range_float(item) for item in answer] |
|
elif isinstance(answer, dict): |
|
processed = {k: handle_out_of_range_float(v) for k, v in answer.items()} |
|
else: |
|
processed = {"answer": str(handle_out_of_range_float(answer))} |
|
|
|
return processed |
|
|
|
except Exception as e: |
|
error_message = str(e) |
|
if error_message != "": |
|
logger.warning("Rate limit exceeded. Switching to next API key.") |
|
with current_groq_key_lock: |
|
current_groq_key_index += 1 |
|
if current_groq_key_index >= len(groq_api_keys): |
|
return {"error": "All API keys exhausted."} |
|
else: |
|
logger.error("Error in groq_chat: %s", e) |
|
return {"error": error_message} |
|
|
|
|
|
def langchain_csv_chat(csv_url: str, question: str, chart_required: bool): |
|
global current_langchain_key_index, current_langchain_key_lock |
|
|
|
data = clean_data(csv_url) |
|
attempts = 0 |
|
|
|
while attempts < len(groq_api_keys): |
|
with current_langchain_key_lock: |
|
if current_langchain_key_index >= len(groq_api_keys): |
|
current_langchain_key_index = 0 |
|
api_key = groq_api_keys[current_langchain_key_index] |
|
current_key = current_langchain_key_index |
|
current_langchain_key_index += 1 |
|
attempts += 1 |
|
|
|
try: |
|
llm = ChatGroq(model=model_name, api_key=api_key) |
|
tool = PythonAstREPLTool(locals={ |
|
"df": data, |
|
"pd": pd, |
|
"np": np, |
|
"plt": plt, |
|
"sns": sns, |
|
"matplotlib": matplotlib |
|
}) |
|
|
|
agent = create_pandas_dataframe_agent( |
|
llm, |
|
data, |
|
agent_type="tool-calling", |
|
verbose=True, |
|
allow_dangerous_code=True, |
|
extra_tools=[tool], |
|
return_intermediate_steps=True |
|
) |
|
|
|
prompt = _prompt_generator(question, chart_required) |
|
result = agent.invoke({"input": prompt}) |
|
return result.get("output") |
|
|
|
except Exception as e: |
|
error_message = str(e) |
|
|
|
if error_message != "": |
|
with current_langchain_chart_lock: |
|
current_langchain_chart_key_index = (current_langchain_chart_key_index + 1) |
|
logger.warning(f"Rate limit exceeded. Switching to next API key: {groq_api_keys[current_langchain_chart_key_index]}") |
|
else: |
|
logger.error(f"Error with API key {api_key}: {error_message}") |
|
return {"error": error_message} |
|
|
|
return {"error": "All API keys exhausted"} |
|
|
|
|
|
@app.post("/api/csv-chat") |
|
async def csv_chat(request: Dict, authorization: str = Header(None)): |
|
|
|
if not authorization or not authorization.startswith("Bearer "): |
|
raise HTTPException(status_code=401, detail="Invalid authorization") |
|
|
|
token = authorization.split(" ")[1] |
|
if token != os.getenv("AUTH_TOKEN"): |
|
raise HTTPException(status_code=403, detail="Invalid token") |
|
|
|
try: |
|
query = request.get("query") |
|
csv_url = request.get("csv_url") |
|
decoded_url = unquote(csv_url) |
|
detailed_answer = request.get("detailed_answer") |
|
conversation_history = request.get("conversation_history", []) |
|
generate_report = request.get("generate_report") |
|
chat_id = request.get("chat_id") |
|
|
|
if generate_report is True: |
|
report_files = await generate_csv_report(csv_url, query, chat_id) |
|
if report_files is not None: |
|
return {"answer": jsonable_encoder(report_files)} |
|
|
|
if if_initial_chat_question(query): |
|
answer = await asyncio.to_thread( |
|
langchain_csv_chat, decoded_url, query, False |
|
) |
|
logger.info("langchain_answer:", answer) |
|
return {"answer": jsonable_encoder(answer)} |
|
|
|
|
|
if detailed_answer is True: |
|
orchestrator_answer = await asyncio.to_thread( |
|
csv_orchestrator_chat, decoded_url, query, conversation_history, chat_id |
|
) |
|
if orchestrator_answer is not None: |
|
return {"answer": jsonable_encoder(orchestrator_answer)} |
|
|
|
|
|
groq_answer = await asyncio.to_thread(groq_chat, decoded_url, query) |
|
logger.info("groq_answer:", groq_answer) |
|
|
|
if process_answer(groq_answer) == "Empty response received.": |
|
return {"answer": "Sorry, I couldn't find relevant data..."} |
|
|
|
if process_answer(groq_answer): |
|
lang_answer = await asyncio.to_thread( |
|
langchain_csv_chat, decoded_url, query, False |
|
) |
|
if process_answer(lang_answer): |
|
return {"answer": "error"} |
|
return {"answer": jsonable_encoder(lang_answer)} |
|
|
|
return {"answer": jsonable_encoder(groq_answer)} |
|
|
|
except Exception as e: |
|
logger.error(f"Error processing request: {str(e)}") |
|
return {"answer": "error"} |
|
|
|
def handle_out_of_range_float(value): |
|
if isinstance(value, float): |
|
if np.isnan(value): |
|
return None |
|
elif np.isinf(value): |
|
return "Infinity" |
|
return value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
instructions = """ |
|
|
|
- Please ensure that each value is clearly visible, You may need to adjust the font size, rotate the labels, or use truncation to improve readability (if needed). |
|
- For multiple charts, arrange them in a grid format (2x2, 3x3, etc.) |
|
- Use colorblind-friendly palette |
|
- Read above instructions and follow them. |
|
|
|
""" |
|
|
|
|
|
current_groq_chart_key_index = 0 |
|
current_groq_chart_lock = threading.Lock() |
|
|
|
|
|
|
|
|
|
def model(): |
|
global current_groq_chart_key_index, current_groq_chart_lock |
|
with current_groq_chart_lock: |
|
if current_groq_chart_key_index >= len(groq_api_keys): |
|
raise Exception("All API keys exhausted for chart generation") |
|
api_key = groq_api_keys[current_groq_chart_key_index] |
|
return ChatGroq(model=model_name, api_key=api_key) |
|
|
|
def groq_chart(csv_url: str, question: str): |
|
global current_groq_chart_key_index, current_groq_chart_lock |
|
|
|
for attempt in range(len(groq_api_keys)): |
|
try: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = clean_data(csv_url) |
|
with current_groq_chart_lock: |
|
current_api_key = groq_api_keys[current_groq_chart_key_index] |
|
|
|
llm = ChatGroq(model=model_name, api_key=current_api_key) |
|
|
|
|
|
chart_filename = f"chart_{uuid.uuid4()}.png" |
|
chart_path = os.path.join("generated_charts", chart_filename) |
|
|
|
|
|
df = SmartDataframe( |
|
data, |
|
config={ |
|
'llm': llm, |
|
'save_charts': True, |
|
'open_charts': False, |
|
'save_charts_path': os.path.dirname(chart_path), |
|
'custom_chart_filename': chart_filename, |
|
'enable_cache': False |
|
} |
|
) |
|
|
|
answer = df.chat(question + instructions) |
|
|
|
if process_answer(answer): |
|
return "Chart not generated" |
|
return answer |
|
|
|
except Exception as e: |
|
error = str(e) |
|
|
|
if error != "": |
|
with current_groq_chart_lock: |
|
current_groq_chart_key_index = (current_groq_chart_key_index + 1) |
|
else: |
|
logger.error(f"Chart generation error: {error}") |
|
return {"error": error} |
|
|
|
return {"error": "All API keys exhausted for chart generation"} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current_langchain_chart_key_index = 0 |
|
current_langchain_chart_lock = threading.Lock() |
|
|
|
|
|
|
|
process_executor = ProcessPoolExecutor(max_workers=max_cpus-2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def langchain_csv_chart(csv_url: str, question: str, chart_required: bool): |
|
""" |
|
Generate a chart using the langchain-based method. |
|
Modifications: |
|
• No shared deletion of cache. |
|
• Close matplotlib figures after generation. |
|
• Return a list of full chart file paths. |
|
""" |
|
global current_langchain_chart_key_index, current_langchain_chart_lock |
|
|
|
data = clean_data(csv_url) |
|
|
|
for attempt in range(len(groq_api_keys)): |
|
try: |
|
with current_langchain_chart_lock: |
|
api_key = groq_api_keys[current_langchain_chart_key_index] |
|
current_key = current_langchain_chart_key_index |
|
current_langchain_chart_key_index = (current_langchain_chart_key_index + 1) % len(groq_api_keys) |
|
|
|
llm = ChatGroq(model=model_name, api_key=api_key) |
|
tool = PythonAstREPLTool(locals={ |
|
"df": data, |
|
"pd": pd, |
|
"np": np, |
|
"plt": plt, |
|
"sns": sns, |
|
"matplotlib": matplotlib, |
|
"uuid": uuid |
|
}) |
|
|
|
agent = create_pandas_dataframe_agent( |
|
llm, |
|
data, |
|
agent_type="tool-calling", |
|
verbose=True, |
|
allow_dangerous_code=True, |
|
extra_tools=[tool], |
|
return_intermediate_steps=True |
|
) |
|
|
|
result = agent.invoke({"input": _prompt_generator(question, True)}) |
|
output = result.get("output", "") |
|
|
|
|
|
plt.close('all') |
|
|
|
|
|
chart_files = extract_chart_filenames(output) |
|
if len(chart_files) > 0: |
|
|
|
return [os.path.join(image_file_path, f) for f in chart_files] |
|
|
|
if attempt < len(groq_api_keys) - 1: |
|
logger.info(f"Langchain chart error (key {current_key}): {output}") |
|
|
|
except Exception as e: |
|
error_message = str(e) |
|
if error_message != "": |
|
with current_langchain_chart_lock: |
|
current_langchain_chart_key_index = (current_langchain_chart_key_index + 1) |
|
logger.warning(f"Rate limit exceeded. Switching to next API key: {groq_api_keys[current_langchain_chart_key_index]}") |
|
else: |
|
logger.error(f"Error with API key {api_key}: {error_message}") |
|
return {"error": error_message} |
|
|
|
logger.error("All API keys exhausted for chart generation") |
|
return "Chart generation failed after all retries" |
|
|
|
|
|
|
|
@app.post("/api/csv-chart") |
|
async def csv_chart(request: dict, authorization: str = Header(None)): |
|
""" |
|
Endpoint for generating a chart from CSV data. |
|
This endpoint uses a ProcessPoolExecutor to run the (CPU-bound) chart generation |
|
functions in separate processes so that multiple requests can run in parallel. |
|
""" |
|
|
|
if not authorization or not authorization.startswith("Bearer "): |
|
raise HTTPException(status_code=401, detail="Authorization required") |
|
|
|
token = authorization.split(" ")[1] |
|
if token != os.getenv("AUTH_TOKEN"): |
|
raise HTTPException(status_code=403, detail="Invalid credentials") |
|
|
|
try: |
|
query = request.get("query", "") |
|
csv_url = unquote(request.get("csv_url", "")) |
|
detailed_answer = request.get("detailed_answer", False) |
|
conversation_history = request.get("conversation_history", []) |
|
generate_report = request.get("generate_report", False) |
|
chat_id = request.get("chat_id", "") |
|
|
|
if generate_report is True: |
|
report_files = await generate_csv_report(csv_url, query, chat_id) |
|
if report_files is not None: |
|
return {"orchestrator_response": jsonable_encoder(report_files)} |
|
|
|
loop = asyncio.get_running_loop() |
|
|
|
if if_initial_chart_question(query): |
|
langchain_result = await loop.run_in_executor( |
|
process_executor, langchain_csv_chart, csv_url, query, True |
|
) |
|
logger.info("Langchain chart result:", langchain_result) |
|
if isinstance(langchain_result, list) and len(langchain_result) > 0: |
|
unique_file_name =f'{str(uuid.uuid4())}.png' |
|
logger.info("Uploading the chart to supabase...") |
|
image_public_url = await upload_file_to_supabase(f"{langchain_result[0]}", unique_file_name, chat_id=chat_id) |
|
logger.info("Image uploaded to Supabase and Image URL is... ", {image_public_url}) |
|
os.remove(langchain_result[0]) |
|
return {"image_url": image_public_url} |
|
|
|
|
|
|
|
if detailed_answer is True: |
|
orchestrator_answer = await asyncio.to_thread( |
|
csv_orchestrator_chat, csv_url, query, conversation_history, chat_id |
|
) |
|
|
|
if orchestrator_answer is not None: |
|
return {"orchestrator_response": jsonable_encoder(orchestrator_answer)} |
|
|
|
|
|
groq_result = await loop.run_in_executor( |
|
process_executor, groq_chart, csv_url, query |
|
) |
|
logger.info(f"Groq chart result: {groq_result}") |
|
if isinstance(groq_result, str) and groq_result != "Chart not generated": |
|
unique_file_name =f'{str(uuid.uuid4())}.png' |
|
logger.info("Uploading the chart to supabase...") |
|
image_public_url = await upload_file_to_supabase(f"{groq_result}", unique_file_name, chat_id=chat_id) |
|
logger.info("Image uploaded to Supabase and Image URL is... ", {image_public_url}) |
|
os.remove(groq_result) |
|
return {"image_url": image_public_url} |
|
|
|
|
|
|
|
logger.error("Groq chart generation failed, trying langchain....") |
|
langchain_paths = await loop.run_in_executor( |
|
process_executor, langchain_csv_chart, csv_url, query, True |
|
) |
|
logger.info("Fallback langchain chart result:", langchain_paths) |
|
if isinstance(langchain_paths, list) and len(langchain_paths) > 0: |
|
unique_file_name =f'{str(uuid.uuid4())}.png' |
|
logger.info("Uploading the chart to supabase...") |
|
image_public_url = await upload_file_to_supabase(f"{langchain_paths[0]}", unique_file_name, chat_id=chat_id) |
|
logger.info("Image uploaded to Supabase and Image URL is... ", {image_public_url}) |
|
os.remove(langchain_paths[0]) |
|
return {"image_url": image_public_url} |
|
|
|
else: |
|
logger.error("All chart generation methods failed") |
|
return {"answer": "error"} |
|
|
|
except Exception as e: |
|
logger.error(f"Critical chart error: {str(e)}") |
|
return {"answer": "error"} |
|
|
|
|