CoolFace
Apppublic

saad810/lms_ai_module2_demo

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
module2Pipeline.py98 linesDownload Raw Back to root
1import os
2import logging
3from pathlib import Path
4from lib.process_docs import ProcessDocs
5from lib.embeddings_processor import EmbeddingsProcessor
6from lib.essay_processing import EssayProcessor
7from lib.grammer_check import LLMGrammarChecker
8
9
10MAX_FILES_PER_SUBJECT = 5  
11
12def indexing(subject_name, files):
13    print("Indexing...")
14    print(subject_name)
15    print(files)
16    if not subject_name:
17        logging.error("Subject name is required.")
18        return
19    
20    if not files:
21        logging.error("No files provided. Please upload at least one file.")
22        return
23    
24    if len(files) > MAX_FILES_PER_SUBJECT:
25        logging.error(f"Too many files! Limit is {MAX_FILES_PER_SUBJECT}.")
26        return
27    
28    embeddings_processor = EmbeddingsProcessor(persist_directory="chroma_db", model_name="text-embedding-3-large")
29
30    all_texts = []
31    metadata_list = []
32
33    for file in files:
34        
35        print("processing---",file)    
36        file_ = Path(file)
37        file_extension = file_.suffix
38        print("file_extension---",file_extension)
39
40        try:
41            docs_processor = ProcessDocs(file, file_type=file_extension.replace(".", ""))
42            full_text = docs_processor.process()
43
44            if not full_text:
45                logging.error(f"Failed to process document: {file}")
46                continue
47            
48            all_texts.append(full_text)
49            metadata_list.append({"source": os.path.basename(file), "subject": subject_name})
50
51        except Exception as e:
52            logging.error(f"Error processing file {file}: {e}")
53            continue
54
55    # Check if there is any valid text to process
56    if not all_texts:
57        logging.error("No valid files to process.")
58        return
59
60    # Split text into chunks
61    chunks = []
62    for text in all_texts:
63        chunks.extend(embeddings_processor.split_text(text))
64
65    # Store embeddings by subject (collection)
66    embeddings_processor.store_embeddings(
67        chunks, metadata_list, collection_name=subject_name, overwrite=False
68    )
69
70    print(f"✅ Successfully indexed {len(chunks)} chunks in '{subject_name}' collection.")
71
72    return full_text
73
74    
75
76def processing( grade, language, title, essay_text, subject_name,strictness=0.7):
77    print("Processing...")
78    essay_processor = EssayProcessor(
79        collection_name=subject_name,  # we use subject as the collection name
80        grade=grade,
81        subject=subject_name,
82        language=language,
83        title=title,
84        content=essay_text
85    )
86    analysis = essay_processor.analyze_essay(strictness=strictness)
87    print("=== Essay Analysis ===")
88    print(analysis)
89    
90    # Run grammar checking on the essay using LLMGrammarChecker.
91    grammar_checker = LLMGrammarChecker()
92    grammar_corrections = grammar_checker.check_grammar(essay_text, strictness=0.7, language=language)
93    print("\n=== Grammar Check ===")
94    print(grammar_corrections)
95
96
97
98