Soumik555 commited on
Commit
a0d14fd
·
1 Parent(s): d6a6b5c

blank image issue on multiple req

Browse files
Files changed (1) hide show
  1. controller.py +126 -126
controller.py CHANGED
@@ -309,81 +309,81 @@ def handle_out_of_range_float(value):
309
 
310
  # CHART CODING STARTS FROM HERE
311
 
312
- # instructions = """
313
 
314
- # - 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).
315
- # - For multiple charts, arrange them in a grid format (2x2, 3x3, etc.)
316
- # - Use colorblind-friendly palette
317
- # - Read above instructions and follow them.
318
 
319
- # """
320
 
321
- # # Thread-safe configuration for chart endpoints
322
- # current_groq_chart_key_index = 0
323
- # current_groq_chart_lock = threading.Lock()
324
 
325
  # current_langchain_chart_key_index = 0
326
  # current_langchain_chart_lock = threading.Lock()
327
 
328
- # def model():
329
- # global current_groq_chart_key_index, current_groq_chart_lock
330
- # with current_groq_chart_lock:
331
- # if current_groq_chart_key_index >= len(groq_api_keys):
332
- # raise Exception("All API keys exhausted for chart generation")
333
- # api_key = groq_api_keys[current_groq_chart_key_index]
334
- # return ChatGroq(model=model_name, api_key=api_key)
335
 
336
- # def groq_chart(csv_url: str, question: str):
337
- # global current_groq_chart_key_index, current_groq_chart_lock
338
 
339
- # for attempt in range(len(groq_api_keys)):
340
- # try:
341
- # # Clean cache before processing
342
- # cache_db_path = "/workspace/cache/cache_db_0.11.db"
343
- # if os.path.exists(cache_db_path):
344
- # try:
345
- # os.remove(cache_db_path)
346
- # except Exception as e:
347
- # print(f"Cache cleanup error: {e}")
348
 
349
- # data = clean_data(csv_url)
350
- # with current_groq_chart_lock:
351
- # current_api_key = groq_api_keys[current_groq_chart_key_index]
352
 
353
- # llm = ChatGroq(model=model_name, api_key=current_api_key)
354
 
355
- # # Generate unique filename using UUID
356
- # chart_filename = f"chart_{uuid.uuid4()}.png"
357
- # chart_path = os.path.join("generated_charts", chart_filename)
358
 
359
- # # Configure SmartDataframe with chart settings
360
- # df = SmartDataframe(
361
- # data,
362
- # config={
363
- # 'llm': llm,
364
- # 'save_charts': True, # Enable chart saving
365
- # 'open_charts': False,
366
- # 'save_charts_path': os.path.dirname(chart_path), # Directory to save
367
- # 'custom_chart_filename': chart_filename # Unique filename
368
- # }
369
- # )
370
 
371
- # answer = df.chat(question + instructions)
372
 
373
- # if process_answer(answer):
374
- # return "Chart not generated"
375
- # return answer
376
 
377
- # except Exception as e:
378
- # error = str(e)
379
- # if "429" in error:
380
- # with current_groq_chart_lock:
381
- # current_groq_chart_key_index = (current_groq_chart_key_index + 1) % len(groq_api_keys)
382
- # else:
383
- # print(f"Chart generation error: {error}")
384
- # return {"error": error}
385
 
386
- # return {"error": "All API keys exhausted for chart generation"}
387
 
388
 
389
 
@@ -572,8 +572,8 @@ def handle_out_of_range_float(value):
572
 
573
 
574
  # Global locks for key rotation (chart endpoints)
575
- current_groq_chart_key_index = 0
576
- current_groq_chart_lock = threading.Lock()
577
  current_langchain_chart_key_index = 0
578
  current_langchain_chart_lock = threading.Lock()
579
 
@@ -584,83 +584,83 @@ print("Available CPUs:", max_cpus)
584
  process_executor = ProcessPoolExecutor(max_workers=4)
585
 
586
  # --- GROQ-BASED CHART GENERATION ---
587
- def groq_chart(csv_url: str, question: str):
588
- """
589
- Generate a chart using the groq-based method.
590
- Modifications:
591
- • No deletion of a shared cache file (avoid interference).
592
- • After chart generation, close all matplotlib figures.
593
- • Return the full path of the saved chart.
594
- """
595
- global current_groq_chart_key_index, current_groq_chart_lock
596
 
597
- for attempt in range(len(groq_api_keys)):
598
- try:
599
- # Instead of deleting a global cache file, you might later configure a per-request cache.
600
- cache_db_path = "/app/cache/cache_db_0.11.db"
601
- if os.path.exists(cache_db_path):
602
- try:
603
- os.remove(cache_db_path)
604
- print(f"Deleted cache DB file: {cache_db_path}")
605
- except Exception as e:
606
- print(f"Error deleting cache DB file: {e}")
607
 
608
- chart_dir = "generated_charts"
609
- if not os.path.exists(chart_dir):
610
- os.makedirs(chart_dir)
611
 
612
- data = clean_data(csv_url)
613
- with current_groq_chart_lock:
614
- current_api_key = groq_api_keys[current_groq_chart_key_index]
615
 
616
- llm = ChatGroq(model=model_name, api_key=current_api_key)
617
 
618
- # Generate a unique filename and full path for the chart
619
- chart_filename = f"chart_{uuid.uuid4().hex}.png"
620
- chart_path = os.path.join("generated_charts", chart_filename)
621
 
622
- # Configure your dataframe tool (e.g. using SmartDataframe) to save charts.
623
- # (Assuming your SmartDataframe uses these settings to save charts.)
624
- from pandasai import SmartDataframe # Import here if not already imported
625
- df = SmartDataframe(
626
- data,
627
- config={
628
- 'llm': llm,
629
- 'save_charts': True,
630
- 'open_charts': False,
631
- 'save_charts_path': os.path.dirname(chart_path),
632
- 'custom_chart_filename': chart_filename
633
- }
634
- )
635
 
636
- # Append any extra instructions if needed
637
- instructions = """
638
- - Ensure each value is clearly visible.
639
- - Adjust font sizes, rotate labels if necessary.
640
- - Use a colorblind-friendly palette.
641
- - Arrange multiple charts in a grid if needed.
642
- """
643
- answer = df.chat(question + instructions)
644
 
645
- # Make sure to close figures so they don't conflict between processes
646
- plt.close('all')
647
 
648
- # If process_answer indicates a problem, return a failure message.
649
- if process_answer(answer):
650
- return "Chart not generated"
651
- # Return the chart path that was used for saving
652
- return chart_path
653
 
654
- except Exception as e:
655
- error = str(e)
656
- if "429" in error:
657
- with current_groq_chart_lock:
658
- current_groq_chart_key_index = (current_groq_chart_key_index + 1) % len(groq_api_keys)
659
- else:
660
- print(f"Groq chart generation error: {error}")
661
- return {"error": error}
662
 
663
- return {"error": "All API keys exhausted for chart generation"}
664
 
665
 
666
  # --- LANGCHAIN-BASED CHART GENERATION ---
 
309
 
310
  # CHART CODING STARTS FROM HERE
311
 
312
+ instructions = """
313
 
314
+ - 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).
315
+ - For multiple charts, arrange them in a grid format (2x2, 3x3, etc.)
316
+ - Use colorblind-friendly palette
317
+ - Read above instructions and follow them.
318
 
319
+ """
320
 
321
+ # Thread-safe configuration for chart endpoints
322
+ current_groq_chart_key_index = 0
323
+ current_groq_chart_lock = threading.Lock()
324
 
325
  # current_langchain_chart_key_index = 0
326
  # current_langchain_chart_lock = threading.Lock()
327
 
328
+ def model():
329
+ global current_groq_chart_key_index, current_groq_chart_lock
330
+ with current_groq_chart_lock:
331
+ if current_groq_chart_key_index >= len(groq_api_keys):
332
+ raise Exception("All API keys exhausted for chart generation")
333
+ api_key = groq_api_keys[current_groq_chart_key_index]
334
+ return ChatGroq(model=model_name, api_key=api_key)
335
 
336
+ def groq_chart(csv_url: str, question: str):
337
+ global current_groq_chart_key_index, current_groq_chart_lock
338
 
339
+ for attempt in range(len(groq_api_keys)):
340
+ try:
341
+ # Clean cache before processing
342
+ cache_db_path = "/workspace/cache/cache_db_0.11.db"
343
+ if os.path.exists(cache_db_path):
344
+ try:
345
+ os.remove(cache_db_path)
346
+ except Exception as e:
347
+ print(f"Cache cleanup error: {e}")
348
 
349
+ data = clean_data(csv_url)
350
+ with current_groq_chart_lock:
351
+ current_api_key = groq_api_keys[current_groq_chart_key_index]
352
 
353
+ llm = ChatGroq(model=model_name, api_key=current_api_key)
354
 
355
+ # Generate unique filename using UUID
356
+ chart_filename = f"chart_{uuid.uuid4()}.png"
357
+ chart_path = os.path.join("generated_charts", chart_filename)
358
 
359
+ # Configure SmartDataframe with chart settings
360
+ df = SmartDataframe(
361
+ data,
362
+ config={
363
+ 'llm': llm,
364
+ 'save_charts': True, # Enable chart saving
365
+ 'open_charts': False,
366
+ 'save_charts_path': os.path.dirname(chart_path), # Directory to save
367
+ 'custom_chart_filename': chart_filename # Unique filename
368
+ }
369
+ )
370
 
371
+ answer = df.chat(question + instructions)
372
 
373
+ if process_answer(answer):
374
+ return "Chart not generated"
375
+ return answer
376
 
377
+ except Exception as e:
378
+ error = str(e)
379
+ if "429" in error:
380
+ with current_groq_chart_lock:
381
+ current_groq_chart_key_index = (current_groq_chart_key_index + 1) % len(groq_api_keys)
382
+ else:
383
+ print(f"Chart generation error: {error}")
384
+ return {"error": error}
385
 
386
+ return {"error": "All API keys exhausted for chart generation"}
387
 
388
 
389
 
 
572
 
573
 
574
  # Global locks for key rotation (chart endpoints)
575
+ # current_groq_chart_key_index = 0
576
+ # current_groq_chart_lock = threading.Lock()
577
  current_langchain_chart_key_index = 0
578
  current_langchain_chart_lock = threading.Lock()
579
 
 
584
  process_executor = ProcessPoolExecutor(max_workers=4)
585
 
586
  # --- GROQ-BASED CHART GENERATION ---
587
+ # def groq_chart(csv_url: str, question: str):
588
+ # """
589
+ # Generate a chart using the groq-based method.
590
+ # Modifications:
591
+ # • No deletion of a shared cache file (avoid interference).
592
+ # • After chart generation, close all matplotlib figures.
593
+ # • Return the full path of the saved chart.
594
+ # """
595
+ # global current_groq_chart_key_index, current_groq_chart_lock
596
 
597
+ # for attempt in range(len(groq_api_keys)):
598
+ # try:
599
+ # # Instead of deleting a global cache file, you might later configure a per-request cache.
600
+ # cache_db_path = "/app/cache/cache_db_0.11.db"
601
+ # if os.path.exists(cache_db_path):
602
+ # try:
603
+ # os.remove(cache_db_path)
604
+ # print(f"Deleted cache DB file: {cache_db_path}")
605
+ # except Exception as e:
606
+ # print(f"Error deleting cache DB file: {e}")
607
 
608
+ # chart_dir = "generated_charts"
609
+ # if not os.path.exists(chart_dir):
610
+ # os.makedirs(chart_dir)
611
 
612
+ # data = clean_data(csv_url)
613
+ # with current_groq_chart_lock:
614
+ # current_api_key = groq_api_keys[current_groq_chart_key_index]
615
 
616
+ # llm = ChatGroq(model=model_name, api_key=current_api_key)
617
 
618
+ # # Generate a unique filename and full path for the chart
619
+ # chart_filename = f"chart_{uuid.uuid4().hex}.png"
620
+ # chart_path = os.path.join("generated_charts", chart_filename)
621
 
622
+ # # Configure your dataframe tool (e.g. using SmartDataframe) to save charts.
623
+ # # (Assuming your SmartDataframe uses these settings to save charts.)
624
+ # from pandasai import SmartDataframe # Import here if not already imported
625
+ # df = SmartDataframe(
626
+ # data,
627
+ # config={
628
+ # 'llm': llm,
629
+ # 'save_charts': True,
630
+ # 'open_charts': False,
631
+ # 'save_charts_path': os.path.dirname(chart_path),
632
+ # 'custom_chart_filename': chart_filename
633
+ # }
634
+ # )
635
 
636
+ # # Append any extra instructions if needed
637
+ # instructions = """
638
+ # - Ensure each value is clearly visible.
639
+ # - Adjust font sizes, rotate labels if necessary.
640
+ # - Use a colorblind-friendly palette.
641
+ # - Arrange multiple charts in a grid if needed.
642
+ # """
643
+ # answer = df.chat(question + instructions)
644
 
645
+ # # Make sure to close figures so they don't conflict between processes
646
+ # plt.close('all')
647
 
648
+ # # If process_answer indicates a problem, return a failure message.
649
+ # if process_answer(answer):
650
+ # return "Chart not generated"
651
+ # # Return the chart path that was used for saving
652
+ # return chart_path
653
 
654
+ # except Exception as e:
655
+ # error = str(e)
656
+ # if "429" in error:
657
+ # with current_groq_chart_lock:
658
+ # current_groq_chart_key_index = (current_groq_chart_key_index + 1) % len(groq_api_keys)
659
+ # else:
660
+ # print(f"Groq chart generation error: {error}")
661
+ # return {"error": error}
662
 
663
+ # return {"error": "All API keys exhausted for chart generation"}
664
 
665
 
666
  # --- LANGCHAIN-BASED CHART GENERATION ---