File size: 28,364 Bytes
363526f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27ef145
363526f
8a7f2d8
363526f
 
 
27ef145
363526f
27ef145
 
363526f
 
27ef145
363526f
 
 
 
8a7f2d8
363526f
 
 
8a7f2d8
363526f
 
8a7f2d8
363526f
 
 
27ef145
363526f
 
 
 
 
27ef145
363526f
 
27ef145
363526f
 
 
 
 
27ef145
363526f
 
 
 
 
 
 
 
 
 
 
 
 
27ef145
363526f
 
27ef145
363526f
 
27ef145
363526f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27ef145
363526f
 
27ef145
363526f
 
 
27ef145
363526f
 
27ef145
363526f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9c6d67
363526f
 
 
 
 
 
 
 
 
 
 
 
 
 
27ef145
363526f
 
 
 
 
27ef145
363526f
 
27ef145
363526f
27ef145
363526f
 
 
 
 
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
 
 
 
27ef145
363526f
 
 
27ef145
363526f
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27ef145
363526f
 
 
 
 
 
 
 
 
 
27ef145
363526f
 
27ef145
363526f
27ef145
363526f
 
27ef145
363526f
 
 
 
 
 
 
 
 
 
 
 
27ef145
 
d784ff5
363526f
 
 
8a7f2d8
363526f
 
8a7f2d8
363526f
 
 
 
 
 
 
 
 
 
d784ff5
 
363526f
 
 
 
 
 
 
 
 
 
00262e5
363526f
 
 
 
 
 
 
 
d784ff5
 
363526f
 
 
 
 
 
 
 
 
 
00262e5
363526f
 
 
 
 
8a7f2d8
363526f
 
00262e5
363526f
 
 
 
 
 
 
 
 
f27d668
363526f
f27d668
27ef145
 
363526f
 
 
 
30e7daa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
import json
import numpy as np
import pandas as pd
import re
import os
import uuid
import logging
from io import StringIO
import sys
import traceback
from typing import Optional, Dict, Any, List
from pydantic import BaseModel, Field
from google.generativeai import GenerativeModel, configure
from dotenv import load_dotenv
import seaborn as sns 
import datetime as dt

from supabase_service import upload_file_to_supabase

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_colwidth', None)

load_dotenv()


API_KEYS = os.getenv("GEMINI_API_KEYS", "").split(",")[::-1]
MODEL_NAME = 'gemini-2.0-flash'

class FileProps(BaseModel):
    fileName: str
    filePath: str
    fileType: str  # 'csv' | 'image'

class Files(BaseModel):
    csv_files: List[FileProps]
    image_files: List[FileProps]

class FileBoxProps(BaseModel):
    files: Files

os.environ['MPLBACKEND'] = 'agg'
import matplotlib.pyplot as plt
plt.show = lambda: None

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

class GeminiKeyManager:
    """Manage multiple Gemini API keys with failover"""
    
    def __init__(self, api_keys: List[str]):
        self.original_keys = api_keys.copy()
        self.available_keys = api_keys.copy()
        self.active_key = None
        self.failed_keys = {}
        
    def configure(self) -> bool:
        while self.available_keys:
            key = self.available_keys.pop(0)
            try:
                configure(api_key=key)
                self.active_key = key
                logger.info(f"Configured with key: {self._mask_key(key)}")
                return True
            except Exception as e:
                self.failed_keys[key] = str(e)
                logger.error(f"Key failed: {self._mask_key(key)}. Error: {str(e)}")
        logger.critical("All API keys failed")
        return False
    
    def _mask_key(self, key: str) -> str:
        return f"{key[:8]}...{key[-4:]}" if key else ""

class PythonREPL:
    """Secure Python REPL with file generation tracking"""
    
    def __init__(self, df: pd.DataFrame):
        self.df = df
        self.output_dir = os.path.abspath(f'generated_outputs/{uuid.uuid4()}')
        os.makedirs(self.output_dir, exist_ok=True)
        self.local_env = {
            "pd": pd,
            "df": self.df.copy(),
            "plt": plt,
            "os": os,
            "uuid": uuid,
            "sns": sns,
            "json": json,
            "dt": dt,
            "output_dir": self.output_dir
        }
        
    def execute(self, code: str) -> Dict[str, Any]:
        print('Executing code...', code)
        old_stdout = sys.stdout
        sys.stdout = mystdout = StringIO()
        file_tracker = {
            'csv_files': set(),
            'image_files': set()
        }

        try:
            code = f"""
import matplotlib.pyplot as plt
plt.switch_backend('agg')
{code}
plt.close('all')
            """
            exec(code, self.local_env)
            self.df = self.local_env.get('df', self.df)
            
            # Track generated files
            for fname in os.listdir(self.output_dir):
                if fname.endswith('.csv'):
                    file_tracker['csv_files'].add(fname)
                elif fname.lower().endswith(('.png', '.jpg', '.jpeg')):
                    file_tracker['image_files'].add(fname)
                    
            error = False
        except Exception as e:
            error_msg = traceback.format_exc()
            error = True
        finally:
            sys.stdout = old_stdout
            
        return {
            "output": mystdout.getvalue(),
            "error": error,
            "error_message": error_msg if error else None,
            "df": self.local_env.get('df', self.df),
            "output_dir": self.output_dir,
            "files": {
                "csv": [os.path.join(self.output_dir, f) for f in file_tracker['csv_files']],
                "images": [os.path.join(self.output_dir, f) for f in file_tracker['image_files']]
            }
        }

class RethinkAgent(BaseModel):
    df: pd.DataFrame
    max_retries: int = Field(default=5, ge=1)
    gemini_model: Optional[GenerativeModel] = None
    current_retry: int = Field(default=0, ge=0)
    repl: Optional[PythonREPL] = None
    key_manager: Optional[GeminiKeyManager] = None
    
    class Config:
        arbitrary_types_allowed = True
        
    def _extract_code(self, response: str) -> str:
        code_match = re.search(r'```python(.*?)```', response, re.DOTALL)
        return code_match.group(1).strip() if code_match else response.strip()
    
    def _generate_initial_prompt(self, query: str) -> str:
        return f"""Generate DIRECT EXECUTION CODE (no functions, no explanations) following STRICT RULES:
        
        MANDATORY REQUIREMENTS:
        1. Operate directly on existing 'df' variable
        2. Save ALL final DataFrames to CSV using: df.to_csv(f'{{output_dir}}/descriptive_name.csv')
        3. For visualizations: plt.savefig(f'{{output_dir}}/chart_name.png')
        4. Use EXACTLY this structure:
           # Data processing
           df_processed = df[...]  # filtering/grouping
           # Save results
           df_processed.to_csv(f'{{output_dir}}/result.csv')
           # Visualizations (if needed)
           plt.figure()
           ... plotting code ...
           plt.savefig(f'{{output_dir}}/chart.png')
           plt.close()

        FORBIDDEN:
        - Function definitions
        - Dummy data creation
        - Any code blocks besides pandas operations and matplotlib
        - Print statements showing dataframes
        - Using any visualization library other than matplotlib or seaborn

        DATAFRAME COLUMNS: {', '.join(self.df.columns)}
        DATAFRAME'S FIRST FIVE ROWS: {self.df.head().to_dict('records')}
        USER QUERY: {query}

        EXAMPLE RESPONSE FOR "Sales by region":
        # Data processing
        sales_by_region = df.groupby('region')['sales'].sum().reset_index()
        # Save results
        sales_by_region.to_csv(f'{{output_dir}}/sales_by_region.csv')
        """

    def _generate_retry_prompt(self, query: str, error: str, code: str) -> str:
        return f"""FIX THIS CODE (failed with: {error}) by STRICTLY FOLLOWING:
        
        1. REMOVE ALL FUNCTION DEFINITIONS
        2. ENSURE DIRECT DF OPERATIONS
        3. USE EXPLICIT output_dir PATHS
        4. ADD NECESSARY IMPORTS IF MISSING
        5. VALIDATE COLUMN NAMES EXIST

        BAD CODE:
        {code}

        CORRECTED CODE:"""

    def initialize_model(self, api_keys: List[str]) -> bool:
        self.key_manager = GeminiKeyManager(api_keys)
        if not self.key_manager.configure():
            raise RuntimeError("API key initialization failed")
        try:
            self.gemini_model = GenerativeModel(MODEL_NAME)
            return True
        except Exception as e:
            logger.error(f"Model init failed: {str(e)}")
            return False
    
    def generate_code(self, query: str, error: Optional[str] = None, previous_code: Optional[str] = None) -> str:
        prompt = self._generate_retry_prompt(query, error, previous_code) if error else self._generate_initial_prompt(query)
        try:
            response = self.gemini_model.generate_content(prompt)
            return self._extract_code(response.text)
        except Exception as e:
            if self.key_manager.available_keys and self.key_manager.configure():
                return self.generate_code(query, error, previous_code)
            raise
    
    def execute_query(self, query: str) -> Dict[str, Any]:
        self.repl = PythonREPL(self.df)
        result = None
        
        while self.current_retry < self.max_retries:
            try:
                code = self.generate_code(query, 
                                         result["error_message"] if result else None,
                                         result["code"] if result else None)
                execution_result = self.repl.execute(code)
                
                if execution_result["error"]:
                    self.current_retry += 1
                    result = {
                        "error_message": execution_result["error_message"],
                        "code": code
                    }
                else:
                    return {
                        "text": execution_result["output"],
                        "csv_files": execution_result["files"]["csv"],
                        "image_files": execution_result["files"]["images"]
                    }
            except Exception as e:
                return {
                    "error": f"Critical failure: {str(e)}",
                    "csv_files": [],
                    "image_files": []
                }
        
        return {
            "error": f"Failed after {self.max_retries} retries",
            "csv_files": [],
            "image_files": []
        }

def gemini_llm_chat(csv_url: str, query: str) -> Dict[str, Any]:
    try:
        df = pd.read_csv(csv_url)
        agent = RethinkAgent(df=df)
        
        if not agent.initialize_model(API_KEYS):
            return {"error": "API configuration failed"}
        
        result = agent.execute_query(query)
        
        if "error" in result:
            return result
        
        return {
            "message": result["text"],
            "csv_files": result["csv_files"],
            "image_files": result["image_files"]
        }
    except Exception as e:
        logger.error(f"Processing failed: {str(e)}")
        return {
            "error": f"Processing error: {str(e)}",
            "csv_files": [],
            "image_files": []
        }
        

async def generate_csv_report(csv_url: str, query: str, chat_id: str) -> FileBoxProps:
    try:
        result = gemini_llm_chat(csv_url, query)
        logger.info(f"Raw result from gemini_llm_chat: {result}")
        
        csv_files = []
        image_files = []
        
        # Check if we got the expected response structure
        if isinstance(result, dict) and 'csv_files' in result and 'image_files' in result:
            # Process CSV files
            for csv_path in result['csv_files']:
                if os.path.exists(csv_path):
                    file_name = os.path.basename(csv_path)
                    try:
                        unique_file_name = f"{uuid.uuid4()}_{file_name}"
                        public_url = await upload_file_to_supabase(
                            file_path=csv_path,
                            file_name=unique_file_name,
                            chat_id=chat_id
                        )
                        csv_files.append(FileProps(
                            fileName=file_name,
                            filePath=public_url,
                            fileType="csv"
                        ))
                        os.remove(csv_path)  # Clean up
                    except Exception as upload_error:
                        logger.error(f"Failed to upload CSV {file_name}: {str(upload_error)}")
                        continue
            
            # Process image files
            for img_path in result['image_files']:
                if os.path.exists(img_path):
                    file_name = os.path.basename(img_path)
                    try:
                        unique_file_name = f"{uuid.uuid4()}_{file_name}"
                        public_url = await upload_file_to_supabase(
                            file_path=img_path,
                            file_name=unique_file_name,
                            chat_id=chat_id
                        )
                        image_files.append(FileProps(
                            fileName=file_name,
                            filePath=public_url,
                            fileType="image"
                        ))
                        os.remove(img_path)  # Clean up
                    except Exception as upload_error:
                        logger.error(f"Failed to upload image {file_name}: {str(upload_error)}")
                        continue
            
            return FileBoxProps(
                files=Files(
                    csv_files=csv_files,
                    image_files=image_files
                )
            )
        else:
            raise ValueError("Unexpected response format from gemini_llm_chat")
            
    except Exception as e:
        logger.error(f"Report generation failed: {str(e)}")
        # Return empty response but log the files we found
        if 'csv_files' in locals() and 'image_files' in locals():
            logger.info(f"Files that were generated but not processed: CSV: {result.get('csv_files', [])}, Images: {result.get('image_files', [])}")
        return FileBoxProps(
            files=Files(
                csv_files=[],
                image_files=[]
            )
        )



# if __name__ == "__main__":
#     result = gemini_llm_chat("./documents/enterprise_sales_data.csv", 
#                            "Generate a detailed sales report of the last 6 months from all the aspects and include a bar chart showing the sales by region.")
#     print(json.dumps(result, indent=2))




# import json
# import numpy as np
# import pandas as pd
# import re
# import os
# import uuid
# import logging
# from io import StringIO
# import sys
# import traceback
# from typing import Optional, Dict, Any, List, Tuple
# from pydantic import BaseModel, Field
# from google.api_core import exceptions as google_exceptions
# from google.generativeai import GenerativeModel, configure
# from dotenv import load_dotenv
# import seaborn as sns 
# import datetime as dt
# from supabase_service import upload_file_to_supabase

# pd.set_option('display.max_columns', None)
# pd.set_option('display.max_rows', None)
# pd.set_option('display.max_colwidth', None)

# load_dotenv()

# API_KEYS = os.getenv("GEMINI_API_KEYS", "").split(",")
# MODEL_NAME = 'gemini-2.0-flash'

# class FileProps(BaseModel):
#     fileName: str
#     filePath: str
#     fileType: str  # 'csv' | 'image'

# class Files(BaseModel):
#     csv_files: List[FileProps]
#     image_files: List[FileProps]

# class FileBoxProps(BaseModel):
#     files: Files

# os.environ['MPLBACKEND'] = 'agg'
# import matplotlib.pyplot as plt
# plt.show = lambda: None

# logging.basicConfig(
#     level=logging.INFO,
#     format='%(asctime)s - %(levelname)s - %(message)s'
# )
# logger = logging.getLogger(__name__)

# class GeminiInstance:
#     """Wrapper for a single Gemini API instance"""
    
#     def __init__(self, api_key: str):
#         self.api_key = api_key
#         self.model = None
#         self.active = False
#         self.failure_count = 0
#         self.last_error = None
        
#     def initialize(self) -> bool:
#         try:
#             configure(api_key=self.api_key)
#             self.model = GenerativeModel(MODEL_NAME)
#             self.active = True
#             logger.info(f"Initialized Gemini instance with key: {self._mask_key()}")
#             return True
#         except Exception as e:
#             self.last_error = str(e)
#             self.failure_count += 1
#             logger.error(f"Failed to initialize Gemini instance: {self._mask_key()}. Error: {str(e)}")
#             return False
    
#     def _mask_key(self) -> str:
#         return f"{self.api_key[:8]}...{self.api_key[-4:]}" if self.api_key else "None"
    
#     def generate_content(self, prompt: str) -> Tuple[Optional[str], Optional[Exception]]:
#         try:
#             response = self.model.generate_content(prompt)
#             return response.text, None
#         except Exception as e:
#             self.last_error = str(e)
#             self.failure_count += 1
#             return None, e

# class GeminiPool:
#     """Pool of Gemini API instances with automatic failover"""
    
#     def __init__(self, api_keys: List[str]):
#         self.instances = [GeminiInstance(key) for key in api_keys]
#         self.current_index = 0
#         self.total_attempts = 0
        
#     def get_active_instance(self) -> Optional[GeminiInstance]:
#         """Get next available instance with automatic rotation"""
#         if not self.instances:
#             return None
            
#         for _ in range(len(self.instances)):
#             instance = self.instances[self.current_index]
#             self.current_index = (self.current_index + 1) % len(self.instances)
#             self.total_attempts += 1
            
#             if instance.active or instance.initialize():
#                 return instance
                
#         return None
    
#     def should_retry(self, error: Exception) -> bool:
#         """Determine if the error is retryable"""
#         if isinstance(error, google_exceptions.ResourceExhausted):
#             return True
#         if isinstance(error, google_exceptions.TooManyRequests):
#             return True
#         if isinstance(error, google_exceptions.ServiceUnavailable):
#             return True
            
#         error_str = str(error).lower()
#         retry_phrases = [
#             'quota',
#             'limit',
#             'exhausted',
#             'retry',
#             'unavailable',
#             'overloaded',
#             '429',
#             '503'
#         ]
#         return any(phrase in error_str for phrase in retry_phrases)

# class PythonREPL:
#     """Secure Python REPL with file generation tracking"""
    
#     def __init__(self, df: pd.DataFrame):
#         self.df = df
#         self.output_dir = os.path.abspath(f'generated_outputs/{uuid.uuid4()}')
#         os.makedirs(self.output_dir, exist_ok=True)
#         self.local_env = {
#             "pd": pd,
#             "df": self.df.copy(),
#             "plt": plt,
#             "os": os,
#             "uuid": uuid,
#             "sns": sns,
#             "json": json,
#             "dt": dt,
#             "output_dir": self.output_dir
#         }
        
#     def execute(self, code: str) -> Dict[str, Any]:
#         old_stdout = sys.stdout
#         sys.stdout = mystdout = StringIO()
#         file_tracker = {
#             'csv_files': set(),
#             'image_files': set()
#         }

#         try:
#             code = f"""
# import matplotlib.pyplot as plt
# plt.switch_backend('agg')
# {code}
# plt.close('all')
#             """
#             exec(code, self.local_env)
#             self.df = self.local_env.get('df', self.df)
            
#             # Track generated files
#             for fname in os.listdir(self.output_dir):
#                 if fname.endswith('.csv'):
#                     file_tracker['csv_files'].add(fname)
#                 elif fname.lower().endswith(('.png', '.jpg', '.jpeg')):
#                     file_tracker['image_files'].add(fname)
                    
#             error = False
#             error_msg = None
#         except Exception as e:
#             error_msg = traceback.format_exc()
#             error = True
#         finally:
#             sys.stdout = old_stdout
            
#         return {
#             "output": mystdout.getvalue(),
#             "error": error,
#             "error_message": error_msg if error else None,
#             "df": self.local_env.get('df', self.df),
#             "output_dir": self.output_dir,
#             "files": {
#                 "csv": [os.path.join(self.output_dir, f) for f in file_tracker['csv_files']],
#                 "images": [os.path.join(self.output_dir, f) for f in file_tracker['image_files']]
#             }
#         }

# class RethinkAgent(BaseModel):
#     df: pd.DataFrame
#     max_retries: int = Field(default=5, ge=1)
#     current_retry: int = Field(default=0, ge=0)
#     repl: Optional[PythonREPL] = None
#     gemini_pool: Optional[GeminiPool] = None
    
#     class Config:
#         arbitrary_types_allowed = True
        
#     def _extract_code(self, response: str) -> str:
#         code_match = re.search(r'```python(.*?)```', response, re.DOTALL)
#         return code_match.group(1).strip() if code_match else response.strip()
    
#     def _generate_initial_prompt(self, query: str) -> str:
#         return f"""Generate DIRECT EXECUTION CODE (no functions, no explanations) following STRICT RULES:
        
#         MANDATORY REQUIREMENTS:
#         1. Operate directly on existing 'df' variable
#         2. Save ALL final DataFrames to CSV using: df.to_csv(f'{{output_dir}}/descriptive_name.csv')
#         3. For visualizations: plt.savefig(f'{{output_dir}}/chart_name.png')
#         4. Use EXACTLY this structure:
#            # Data processing
#            df_processed = df[...]  # filtering/grouping
#            # Save results
#            df_processed.to_csv(f'{{output_dir}}/result.csv')
#            # Visualizations (if needed)
#            plt.figure()
#            ... plotting code ...
#            plt.savefig(f'{{output_dir}}/chart.png')
#            plt.close()

#         FORBIDDEN:
#         - Function definitions
#         - Dummy data creation
#         - Any code blocks besides pandas operations and matplotlib
#         - Print statements showing dataframes

#         DATAFRAME COLUMNS: {', '.join(self.df.columns)}
#         DATAFRAME'S FIRST FIVE ROWS: {self.df.head().to_dict('records')}
#         USER QUERY: {query}

#         EXAMPLE RESPONSE FOR "Sales by region":
#         # Data processing
#         sales_by_region = df.groupby('region')['sales'].sum().reset_index()
#         # Save results
#         sales_by_region.to_csv(f'{{output_dir}}/sales_by_region.csv')
#         """

#     def _generate_retry_prompt(self, query: str, error: str, code: str) -> str:
#         return f"""FIX THIS CODE (failed with: {error}) by STRICTLY FOLLOWING:
        
#         1. REMOVE ALL FUNCTION DEFINITIONS
#         2. ENSURE DIRECT DF OPERATIONS
#         3. USE EXPLICIT output_dir PATHS
#         4. ADD NECESSARY IMPORTS IF MISSING
#         5. VALIDATE COLUMN NAMES EXIST

#         BAD CODE:
#         {code}

#         CORRECTED CODE:"""

#     def initialize_pool(self) -> bool:
#         self.gemini_pool = GeminiPool(API_KEYS)
#         return True
    
#     def generate_code(self, query: str, error: Optional[str] = None, previous_code: Optional[str] = None) -> str:
#         prompt = self._generate_retry_prompt(query, error, previous_code) if error else self._generate_initial_prompt(query)
        
#         instance = self.gemini_pool.get_active_instance()
#         if not instance:
#             raise RuntimeError("No available Gemini instances")
            
#         response_text, error = instance.generate_content(prompt)
        
#         if error:
#             if self.gemini_pool.should_retry(error):
#                 logger.warning(f"Retryable error from Gemini: {str(error)}")
#                 return self.generate_code(query, error, previous_code)
#             raise error
            
#         return self._extract_code(response_text)
    
#     def execute_query(self, query: str) -> Dict[str, Any]:
#         self.repl = PythonREPL(self.df)
#         result = None
        
#         while self.current_retry < self.max_retries:
#             try:
#                 code = self.generate_code(query, 
#                                          result["error_message"] if result else None,
#                                          result["code"] if result else None)
#                 execution_result = self.repl.execute(code)
                
#                 if execution_result["error"]:
#                     self.current_retry += 1
#                     result = {
#                         "error_message": execution_result["error_message"],
#                         "code": code
#                     }
#                 else:
#                     return {
#                         "text": execution_result["output"],
#                         "csv_files": execution_result["files"]["csv"],
#                         "image_files": execution_result["files"]["images"]
#                     }
#             except Exception as e:
#                 return {
#                     "error": f"Critical failure: {str(e)}",
#                     "csv_files": [],
#                     "image_files": []
#                 }
        
#         return {
#             "error": f"Failed after {self.max_retries} retries",
#             "csv_files": [],
#             "image_files": []
#         }

# def gemini_llm_chat(csv_url: str, query: str) -> Dict[str, Any]:
#     try:
#         df = pd.read_csv(csv_url)
#         agent = RethinkAgent(df=df)
        
#         if not agent.initialize_pool():
#             return {"error": "API pool initialization failed"}
        
#         result = agent.execute_query(query)
        
#         if "error" in result:
#             return result
        
#         return {
#             "message": result["text"],
#             "csv_files": result["csv_files"],
#             "image_files": result["image_files"]
#         }
#     except Exception as e:
#         logger.error(f"Processing failed: {str(e)}", exc_info=True)
#         return {
#             "error": f"Processing error: {str(e)}",
#             "csv_files": [],
#             "image_files": []
#         }

# async def generate_csv_report(csv_url: str, query: str) -> FileBoxProps:
#     try:
#         result = gemini_llm_chat(csv_url, query)
#         logger.info(f"Raw result from gemini_llm_chat: {result}")
        
#         csv_files = []
#         image_files = []
        
#         if isinstance(result, dict) and 'csv_files' in result and 'image_files' in result:
#             # Process CSV files
#             for csv_path in result['csv_files']:
#                 if os.path.exists(csv_path):
#                     file_name = os.path.basename(csv_path)
#                     try:
#                         unique_file_name = f"{uuid.uuid4()}_{file_name}"
#                         public_url = await upload_file_to_supabase(
#                             file_path=csv_path,
#                             file_name=unique_file_name
#                         )
#                         csv_files.append(FileProps(
#                             fileName=file_name,
#                             filePath=public_url,
#                             fileType="csv"
#                         ))
#                         os.remove(csv_path)
#                     except Exception as upload_error:
#                         logger.error(f"Failed to upload CSV {file_name}: {str(upload_error)}")
#                         continue
            
#             # Process image files
#             for img_path in result['image_files']:
#                 if os.path.exists(img_path):
#                     file_name = os.path.basename(img_path)
#                     try:
#                         unique_file_name = f"{uuid.uuid4()}_{file_name}"
#                         public_url = await upload_file_to_supabase(
#                             file_path=img_path,
#                             file_name=unique_file_name
#                         )
#                         image_files.append(FileProps(
#                             fileName=file_name,
#                             filePath=public_url,
#                             fileType="image"
#                         ))
#                         os.remove(img_path)
#                     except Exception as upload_error:
#                         logger.error(f"Failed to upload image {file_name}: {str(upload_error)}")
#                         continue
            
#             return FileBoxProps(
#                 files=Files(
#                     csv_files=csv_files,
#                     image_files=image_files
#                 )
#             )
#         else:
#             raise ValueError("Unexpected response format from gemini_llm_chat")
            
#     except Exception as e:
#         logger.error(f"Report generation failed: {str(e)}", exc_info=True)
#         return FileBoxProps(
#             files=Files(
#                 csv_files=[],
#                 image_files=[]
#             )
#         )