File size: 23,347 Bytes
6df5c93
 
 
 
 
 
 
 
 
 
42309dc
 
 
 
 
6df5c93
6c87654
6df5c93
 
 
 
 
 
 
 
 
b4050b2
 
6df5c93
 
 
 
 
 
 
466808a
6df5c93
 
 
 
 
 
 
 
 
e7f96d6
 
 
 
 
bc25670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536cb6f
 
 
bc25670
 
 
 
 
 
536cb6f
 
bc25670
 
 
 
 
536cb6f
bc25670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d32d16
a2dbd7f
e75a82f
f59899c
 
 
 
 
 
 
 
6df5c93
f59899c
 
 
 
 
 
 
 
 
7f71568
f59899c
 
 
f79e678
f59899c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f79e678
4c7739f
906d13d
4c7739f
 
2bdee9f
906d13d
2bdee9f
5479cc7
2bdee9f
 
 
 
 
151e771
 
 
 
 
 
 
76330b3
906d13d
 
c9266e5
 
8c87ee7
9696f80
 
 
1bf0d16
8c87ee7
59d802c
 
 
 
 
 
 
9696f80
59d802c
9696f80
8e2a4c7
9696f80
8b672ba
906d13d
 
 
8a7c4d7
906d13d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8368902
906d13d
 
 
 
cdba93c
906d13d
 
 
68fd2f1
53d7dc9
 
 
68fd2f1
906d13d
5de28f4
 
 
906d13d
59d802c
76330b3
 
 
 
 
 
 
906d13d
 
dd28b48
6df5c93
 
 
8c87ee7
76330b3
f3a8770
 
 
 
 
 
 
 
 
151e771
f3a8770
 
 
 
 
1f16680
 
0c7d2d1
2bdee9f
0c7d2d1
2bdee9f
 
ab6e232
2bdee9f
 
ab6e232
2bdee9f
56d4785
151e771
 
 
56d4785
 
 
 
 
 
151e771
3276db6
151e771
3de41d8
151e771
 
 
 
 
 
 
 
 
 
 
 
 
328d341
f3a8770
 
151e771
 
6df5c93
151e771
6df5c93
34426fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3de41d8
34426fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db197d5
34426fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6df5c93
 
 
 
 
 
 
 
 
0fdd155
616f4b7
df02851
8aebf77
df02851
8aebf77
5f6c8c6
 
 
 
8aebf77
 
 
 
 
375de0d
df02851
 
6df5c93
 
 
 
 
0fdd155
6df5c93
 
 
 
 
 
 
 
 
99bb0aa
0c7d2d1
0fdd155
d7cd739
 
0fdd155
d7cd739
0fdd155
 
d066682
d7cd739
d066682
 
 
 
 
 
 
 
 
 
 
 
 
 
d7cd739
d066682
99bb0aa
d066682
 
 
7efc081
 
 
6956308
7efc081
 
 
 
99bb0aa
d066682
d7cd739
99bb0aa
d066682
 
 
99bb0aa
 
 
 
6df5c93
 
499e447
6df5c93
 
 
0fdd155
8917e60
acb3542
8917e60
 
 
76330b3
8917e60
 
906d13d
8917e60
 
698a083
52c968b
8917e60
 
698a083
0c35020
698a083
0c35020
8917e60
 
0c35020
8917e60
 
0c35020
8917e60
 
0fdd155
8917e60
1f16680
6df5c93
93e3091
 
6df5c93
 
 
 
 
 
 
 
 
 
 
506afb0
 
 
 
 
6df5c93
 
499e447
6df5c93
 
499e447
6df5c93
 
1afdee3
 
 
 
 
 
417adb9
40be4b1
1afdee3
 
 
 
 
 
 
 
 
 
 
8fde75c
 
 
1afdee3
 
 
 
 
 
 
 
 
499e447
 
 
 
1afdee3
 
 
8c715b2
 
 
 
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
import os
import json
import gradio as gr
import zipfile
import tempfile
import requests
import urllib.parse
import io

from huggingface_hub import HfApi, login
from PyPDF2 import PdfReader
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_groq import ChatGroq
from dotenv import load_dotenv
from langchain.docstore.document import Document

# Load environment variables from .env file
load_dotenv()

# Load configuration from JSON file
with open('config.json') as config_file:
    config = json.load(config_file)


PERSIST_DOC_DIRECTORY = config["persist_doc_directory"]
PERSIST_CODE_DIRECTORY =config["persist_code_directory"]
CHUNK_SIZE = config["chunk_size"]
CHUNK_OVERLAP = config["chunk_overlap"]
EMBEDDING_MODEL_NAME = config["embedding_model"]
LLM_MODEL_NAME = config["llm_model"]
LLM_TEMPERATURE = config["llm_temperature"]
GITLAB_API_URL = config["gitlab_api_url"]
HF_SPACE_NAME = config["hf_space_name"]
DATA_DIR = config["data_dir"]

GROQ_API_KEY = os.environ["GROQ_API_KEY"]
HF_TOKEN = os.environ["HF_Token"]



login(HF_TOKEN)
api = HfApi()

def load_project_id(json_file):
    with open(json_file, 'r') as f:
        data = json.load(f)
    return data['project_id']


def download_gitlab_project_by_version():
    try:
        # Load the configuration from config.json
        with open("config2.json", "r") as file:
            config = json.load(file)

        # Extract GitLab project information from the config
        api_url = config['gitlab']['api_url']
        project_id = urllib.parse.quote(config['gitlab']['project']['id'], safe="")
        version = config['gitlab']['project']['version']
        
        # Construct the URL for the release's zip file
        url = f"{api_url}/projects/{project_id}/repository/archive.zip?sha={version}"

        # Send GET request to download the zip file
        response = requests.get(url, stream=True)
        archive_bytes = io.BytesIO(response.content)

        print(archive_bytes)
        
        if response.status_code == 200:
            # Extract filename from content-disposition header
            content_disposition = response.headers.get("content-disposition")
            if content_disposition and "filename=" in content_disposition:
                filename = content_disposition.split("filename=")[-1].strip('"')
        print(filename)
        
       # target_path = f"{DATA_DIR}/{filename}"
        
        # Check if the request was successful
        if response.status_code == 200:
            api.upload_file(
                path_or_fileobj= archive_bytes,
                path_in_repo= f"{DATA_DIR}/{filename}",
                repo_id=HF_SPACE_NAME,
                repo_type='space'
        )
            print(f"Release {version} downloaded successfully as {file_path}.")
        else:
            print(f"Failed to download the release: {response.status_code} - {response.reason}")
            print(response.text)

    except FileNotFoundError:
        print("The config.json file was not found. Please ensure it exists in the project directory.")
    except json.JSONDecodeError:
        print("Failed to parse the config.json file. Please ensure it contains valid JSON.")
    except Exception as e:
        print(f"An error occurred: {e}")



def download_gitlab_repo():
    print("Start the upload_gitRepository function")
    project_id = load_project_id('repository_ids.json')
    encoded_project_id = urllib.parse.quote_plus(project_id)
    
    # Define the URL to download the repository archive
    archive_url = f"{GITLAB_API_URL}/projects/{encoded_project_id}/repository/archive.zip"
    
    # Download the repository archive
    response = requests.get(archive_url)
    archive_bytes = io.BytesIO(response.content)
    
    # Retrieve the original file name from the response headers
    content_disposition = response.headers.get('content-disposition')
    if content_disposition:
        filename = content_disposition.split('filename=')[-1].strip('\"')
    else:
        filename = 'archive.zip'  # Fallback to a default name if not found

    # Check if the file already exists in the repository
    existing_files = api.list_repo_files(repo_id=HF_SPACE_NAME, repo_type='space')
    target_path = f"{DATA_DIR}/{filename}"

    print(f"Target Path: '{target_path}'")
    print(f"Existing Files: {[repr(file) for file in existing_files]}")
    
    if target_path in existing_files:
        print(f"File '{target_path}' already exists in the repository. Skipping upload...")
    else:
        # Upload the ZIP file to the new folder in the Hugging Face space repository
        print("Uploading File to directory:")
        print(f"Archive Bytes: {repr(archive_bytes.getvalue())[:100]}")  # Show a preview of bytes
        print(f"Target Path in Repo: '{target_path}'")

        api.upload_file(
            path_or_fileobj=archive_bytes,
            path_in_repo=target_path,
            repo_id=HF_SPACE_NAME,
            repo_type='space'
        )
        print("Upload complete")


def get_all_files_in_folder(temp_dir, folder_path):
    
    all_files = [] 
    print("inner method of get all files in folder")
    target_dir = os.path.join(temp_dir, folder_path)
    print(target_dir)

    for root, dirs, files in os.walk(target_dir):
        print(f"Files in current directory ({root}): {files}")
        for file in files:
            print(f"Processing file: {file}")
            all_files.append(os.path.join(root, file))

    return all_files

def get_file(temp_dir, file_path):
    full_path = os.path.join(temp_dir, file_path)
    return full_path


#getFilesFromRepo
def process_directory(directory, folder_paths, file_paths):
    all_texts = []
    file_references = []

    zip_filename = next((file for file in os.listdir(directory) if file.endswith('.zip')), None) # zip_filename: kadi-apy-master-2a244f1af1483b48f8f9c0d99ce2744a0950c834.zip
    print("zip_filename:", zip_filename)
    zip_file_path = os.path.join(directory, zip_filename)  # zip_file_path: data/kadi-apy-master-2a244f1af1483b48f8f9c0d99ce2744a0950c834.zip
    print("zip_file_path:", zip_file_path)
   # zip_file_path = os.listdir(directory) if file.endswith('.zip')

    with tempfile.TemporaryDirectory() as tmpdirname:
        # Unzip the file into the temporary directory
        with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
            zip_ref.extractall(tmpdirname)
            
            files = []
            print("tmpdirname: " , tmpdirname)     # /tmp/tmpux1v52wy
            unzipped_root = os.listdir(tmpdirname)
            print("unzipped_root ", unzipped_root)  # ['kadi-apy-master-2a244f1af1483b48f8f9c0d99ce2744a0950c834']

            tmpsubdirpath= os.path.join(tmpdirname, unzipped_root[0])  # /tmp/tmpux1v52wy/kadi-apy-master-2a244f1af1483b48f8f9c0d99ce2744a0950c834
            print("tempsubdirpath: ", tmpsubdirpath)

            if folder_paths:
                for folder_path in folder_paths:
                    files += get_all_files_in_folder(tmpsubdirpath, folder_path) 
            if file_paths:
                files += [get_file(tmpsubdirpath, file_path) for file_path in file_paths] 


            print(f"Total number of files: {len(files)}")
            for file_path in files:
                #print(f"Paths of files: {iles}")
                file_ext = os.path.splitext(file_path)[1]

                if os.path.getsize(file_path) == 0:
                    print(f"Skipping an empty file: {file_path}")
                    continue

                with open(file_path, 'rb') as f:
                    if file_ext in ['.rst', '.md', '.txt', '.html', '.json', '.yaml', '.py']:
                        text = f.read().decode('utf-8')
                        
                    elif file_ext in ['.svg']:
                        text = f"SVG file content from {file_path}"
                    elif file_ext in ['.png', '.ico']:
                        text = f"Image metadata from {file_path}"
                    else:
                        continue

                    all_texts.append(text)
                    print("Filepaths brother:", file_path)
                    relative_path = os.path.relpath(file_path, tmpsubdirpath)
                    print("Relative Filepaths brother:", relative_path)
                    file_references.append(relative_path)
                    

                    # Add this snippet after the 'all_texts.append(text)' and 'file_references.append(file_path)' lines.


    return all_texts, file_references
                
 #   with tempfile.TemporaryDirectory() as tmpdirname:
    # Unzip the file into the temporary directory
 #   with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
 #       zip_ref.extractall(tmpdirname)


  #      unzipped_root = os.listdir(tmpdirname)

            
def process_directory5(directory, partial_paths=None, file_paths=None):
    all_texts = []
    file_references = []

    zip_files = [file for file in os.listdir(directory) if file.endswith('.zip')]
    
    if not zip_files:
        print("No zip file found in the directory.")
        return all_texts, file_references

    if len(zip_files) > 1:
        print("More than one zip file found.")
        return all_texts, file_references
    else:
        zip_file_path = os.path.join(directory, zip_files[0])

        # Create a temporary directory for the zip file
        with tempfile.TemporaryDirectory() as tmpdirname:
            # Unzip the file into the temporary directory
            with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
                zip_ref.extractall(tmpdirname)
            
            files = []
            print("tmpdirname: " , tmpdirname)
            unzipped_root = os.listdir(tmpdirname)
            print("unzipped_root ", unzipped_root)
            if len(unzipped_root) == 1 and os.path.isdir(os.path.join(tmpdirname, unzipped_root[0])):
                tmpsubdirpath= os.path.join(tmpdirname, unzipped_root[0])
                print("AYYYYYYY 11111")
            else:
                tmpsubdirpath = tmpdirname
                print("AYYYYYYY 22222")
                    
            if not partial_paths and not file_paths:
                for root, _, files_list in os.walk(tmpdirname):
                    for file in files_list:
                        files.append(os.path.join(root, file))
            else: 
                if partial_paths:
                    for partial_path in partial_paths:
                        files += get_all_files_in_folder(tmpsubdirpath, partial_path) 
                if file_paths:
                    files += [get_file(tmpsubdirpath, file_path) for file_path in file_paths] 

            print(f"Total number of files: {len(files)}")
            for file_path in files:
                #print(f"Paths of files: {iles}")
                file_ext = os.path.splitext(file_path)[1]

                if os.path.getsize(file_path) == 0:
                    print(f"Skipping an empty file: {file_path}")
                    continue

                with open(file_path, 'rb') as f:
                    if file_ext in ['.rst', '.md', '.txt', '.html', '.json', '.yaml', '.py']:
                        text = f.read().decode('utf-8')
                    elif file_ext in ['.svg']:
                        text = f"SVG file content from {file_path}"
                    elif file_ext in ['.png', '.ico']:
                        text = f"Image metadata from {file_path}"
                    else:
                        continue

                    all_texts.append(text)
                    file_references.append(file_path)

    return all_texts, file_references

import ast

def get_source_segment(source_lines, node):
    start_line, start_col = node.lineno - 1, node.col_offset
    end_line = node.end_lineno - 1 if hasattr(node, 'end_lineno') else node.lineno - 1
    end_col = node.end_col_offset if hasattr(node, 'end_col_offset') else len(source_lines[end_line])

    lines = source_lines[start_line:end_line + 1]
    lines[0] = lines[0][start_col:]
    lines[-1] = lines[-1][:end_col]

    return ''.join(lines)

from langchain.schema import Document

def chunk_python_file_content(content, char_limit=1572):
    source_lines = content.splitlines(keepends=True)
    
    # Parse the content into an abstract syntax tree (AST)
    tree = ast.parse(content)


    chunks = []
    current_chunk = ""
    current_chunk_size = 0
    
    # Find all class definitions and top-level functions in the AST
    class_nodes = [node for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]

    for class_node in class_nodes:
        method_nodes = [node for node in class_node.body if isinstance(node, ast.FunctionDef)]

        if method_nodes:
            first_method_start_line = method_nodes[0].lineno - 1
            class_def_lines = source_lines[class_node.lineno - 1:first_method_start_line]
        else:
            class_def_lines = source_lines[class_node.lineno - 1:class_node.end_lineno]

        class_def = ''.join(class_def_lines)
        class_def_size = len(class_def)
        
        # Add class definition to the current chunk if it fits
        if current_chunk_size + class_def_size <= char_limit:
            current_chunk += f"{class_def.strip()}\n"
            current_chunk_size += class_def_size
        else:
            # Start a new chunk if the class definition exceeds the limit
            if current_chunk:
                chunks.append(current_chunk.strip())
                current_chunk = ""
                current_chunk_size = 0
            current_chunk += f"{class_def.strip()}\n"
            current_chunk_size = class_def_size
        
        for method_node in method_nodes:
            method_def = get_source_segment(source_lines, method_node)
            method_def_size = len(method_def)
            
            # Add method definition to the current chunk if it fits
            if current_chunk_size + method_def_size <= char_limit:
                current_chunk += f"# This is a class method of class: {class_node.name}\n{method_def.strip()}\n"
                current_chunk_size += method_def_size
            else:
                # Start a new chunk if the method definition exceeds the limit
                if current_chunk:
                    chunks.append(current_chunk.strip())
                    current_chunk = ""
                    current_chunk_size = 0
                current_chunk += f"# This is a class method of class: {class_node.name}\n{method_def.strip()}\n"
                current_chunk_size = method_def_size

    if current_chunk:
        chunks.append(current_chunk.strip())

    return chunks



# Split python code into chunks
def split_pythoncode_into_chunks(texts, references, chunk_size, chunk_overlap):
    chunks = []
    
    for text, reference in zip(texts, references):
        file_chunks = chunk_python_file_content(text, char_limit=chunk_size)
        
        for chunk in file_chunks:
            document = Document(page_content=chunk, metadata={"source": reference})
            chunks.append(document)
    return chunks


# Split text into chunks
def split_into_chunks(texts, references, chunk_size, chunk_overlap):
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
    chunks = []

    for text, reference in zip(texts, references):
        chunks.extend([Document(page_content=chunk, metadata={"source": reference}) for chunk in text_splitter.split_text(text)])
    return chunks

# Setup Vectorstore
def setup_vectorstore(chunks, model_name, persist_directory):
    print("Start setup_vectorstore_function")
    embedding_model = HuggingFaceEmbeddings(model_name=model_name)
    
    vectorstore = Chroma.from_documents(chunks, embedding=embedding_model, persist_directory=persist_directory)
    path = persist_directory

    root_dir = os.path.splitdrive(path)[0] or os.sep
    print("Root directory:", root_dir)
    print("test1", vectorstore._persist_directory)
    print("test2",vectorstore.__dir__)
    return vectorstore





# Setup LLM
def setup_llm(model_name, temperature, api_key):
    llm = ChatGroq(model=model_name, temperature=temperature, api_key=api_key)
    return llm

def retrieve_from_vectorstore(vectorstore, query, k):
    results = vectorstore.similarity_search(query, k=k)
    chunks_with_references = [(result.page_content, result.metadata["source"]) for result in results]
    # Print the chosen chunks and their sources to the console
    print("\nChosen chunks and their sources for the query:")
    for chunk, source in chunks_with_references:
        print(f"Source: {source}\nChunk: {chunk}\n")
        print("-" * 50)
    return chunks_with_references

def rag_workflow(query):
    retrieved_doc_chunks = retrieve_from_vectorstore (docstore, query, k=5)
    retrieved_code_chunks = retrieve_from_vectorstore(codestore, query, k=5)
    
    doc_context = "\n\n".join([doc_chunk for doc_chunk, _ in retrieved_doc_chunks])
    code_context = "\n\n".join([code_chunk for code_chunk, _ in retrieved_code_chunks]) 
    
    doc_references = "\n".join([f"[{i+1}] {ref}" for i, (_, ref) in enumerate(retrieved_doc_chunks)])
    code_references = "\n".join([f"[{i+1}] {ref}" for i, (_, ref) in enumerate(retrieved_code_chunks)])

    
    print("Document Chunks:\n")
    print("\n\n".join(["="*80 + "\n" + doc_chunk for doc_chunk, _ in retrieved_doc_chunks]))
    print("\nDocument References:\n")
    print(doc_references)

    print("\n" + "="*80 + "\n")  # Separator between doc and code

    print("Code Chunks:\n")
    print("\n\n".join(["="*80 + "\n" + code_chunk for code_chunk, _ in retrieved_code_chunks]))
    print("\nCode References:\n")
    print(code_references)


   # print(f"Context for the query:\n{doc_context}\n")
    
   # print(f"References for the query:\n{references}\n")
    
    prompt = f"""You are an expert python developer. You are assisting in generating code for users who wants to make use of "kadi-apy", an API library. 
                    "Doc-context:" provides you with information how to use this API library by givnig code examples and code documentation.
                    "Code-context:" provides you information of API methods and classes from the "kadi-apy" library. 
                    Based on the retrieved contexts and the guidelines answer the query.  
                    

                    General Guidelines:
                    - If no related information is found from the contexts to answer the query, reply that you do not know.
                    
                    Guidelines when generating code:
                    - First display the full code and then follow with a well structured explanation of the generated code.

            Doc-context:
            {doc_context}

            Code-context:
            {code_context}
            
            Query: 
            {query}
    """

    
    response = llm.invoke(prompt)
    return response.content


def initialize():
    global docstore, codestore, chunks, llm

    download_gitlab_project_by_version()
    #download_gitlab_repo()
    #code_partial_paths = ['kadi_apy/lib/']
    #code_file_paths = []
    #doc_partial_paths = []
    #doc_partial_paths = ['docs/source/setup/']
    #doc_file_paths = ['docs/source/usage/lib.rst']
    
    #code_files, code_file_references = process_directory(DATA_DIR,code_partial_paths, code_file_paths)
    #print("LEEEEEEEEEEEENGTH of code_files: ", len(code_files))

    
    #doc_files, doc_file_references = process_directory(DATA_DIR,doc_partial_paths, doc_file_paths)
    #print("LEEEEEEEEEEEENGTH of doc_files: ", len(doc_files))
    #code_files, code_file_references = process_directory5(DATA_DIR, code_partial_paths, code_file_path)
    
    #doc_files, doc_file_references = process_directory5(DATA_DIR, doc_partial_paths, doc_file_paths)
    
    #code_chunks = split_pythoncode_into_chunks(code_files, code_file_references, 1500, 0)
    #doc_chunks = split_into_chunks(doc_files, doc_file_references, CHUNK_SIZE, CHUNK_OVERLAP)

    #print(f"Total number of code_chunks: {len(code_chunks)}")
    #print(f"Total number of doc_chunks: {len(doc_chunks)}")

    #docstore = setup_vectorstore(doc_chunks, EMBEDDING_MODEL_NAME, PERSIST_DOC_DIRECTORY)
    #codestore = setup_vectorstore(code_chunks, EMBEDDING_MODEL_NAME, PERSIST_CODE_DIRECTORY)
    
    #llm = setup_llm(LLM_MODEL_NAME, LLM_TEMPERATURE, GROQ_API_KEY)


initialize()

# Gradio utils
def check_input_text(text):
    if not text:
        gr.Warning("Please input a question.")
        raise TypeError
    return True

def add_text(history, text):
    history = history + [(text, None)]
    yield history, ""



import gradio as gr


def bot_kadi(history):
    user_query = history[-1][0]
    response = rag_workflow(user_query)
    history[-1] = (user_query, response)

    yield history  

def main():
    with gr.Blocks() as demo:
        gr.Markdown("## Kadi4Mat - AI Chat-Bot")
        gr.Markdown("AI assistant for Kadi4Mat based on RAG architecture powered by LLM")

        with gr.Tab("Kadi4Mat - AI Assistant"):
            with gr.Row():
                with gr.Column(scale=10):
                    chatbot = gr.Chatbot([], elem_id="chatbot", label="Kadi Bot", bubble_full_width=False, show_copy_button=True, height=600)
                    user_txt = gr.Textbox(label="Question", placeholder="Type in your question and press Enter or click Submit")

                    with gr.Row():
                        with gr.Column(scale=1):
                            submit_btn = gr.Button("Submit", variant="primary")
                        with gr.Column(scale=1):
                            clear_btn = gr.Button("Clear", variant="stop")

                    gr.Examples(
                        examples=[
                            "Who is working on Kadi4Mat?",
                            "How do i install the Kadi-Apy library?",
                            "How do i install the Kadi-Apy library for development?",
                            "I need a method to upload a file to a record",
                        ],
                        inputs=user_txt,
                        outputs=chatbot,
                        fn=add_text,
                        label="Try asking...",
                        cache_examples=False,
                        examples_per_page=3,
                    )

            user_txt.submit(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(bot_kadi, [chatbot], [chatbot])
            submit_btn.click(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(bot_kadi, [chatbot], [chatbot])
            #user_txt.submit(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(bot_kadi, [chatbot], [chatbot, doc_citation])
            #submit_btn.click(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(bot_kadi, [chatbot], [chatbot, doc_citation])
            clear_btn.click(lambda: None, None, chatbot, queue=False)

    demo.launch() 

    
if __name__ == "__main__":
    main()