File size: 11,973 Bytes
c57baf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3051e2
c57baf4
f3051e2
c57baf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8de7105
c57baf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8de7105
 
 
 
 
 
 
 
 
 
 
c57baf4
 
8de7105
 
 
 
 
 
 
 
 
 
 
 
 
 
c57baf4
8de7105
 
 
 
 
 
 
 
c57baf4
8de7105
 
 
 
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
import warnings
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
from happytransformer import HappyTextToText, TTSettings
from styleformer import Styleformer
from sentence_transformers import SentenceTransformer
import chromadb
import pandas as pd
import logging
import re
from threading import Thread
import hashlib
import diskcache as dc
import nltk 
nltk.download('punkt_tab')

warnings.filterwarnings("ignore")
logging.basicConfig(level=logging.INFO, # filename="py_log.log",filemode="w",
                    format="%(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")


# For chromadb collection
MAX_TOKENS = 512
client = chromadb.Client()
embedder = SentenceTransformer('all-MiniLM-L6-v2')
collection_name = 'papers'

# For grammar checker
happy_tt = HappyTextToText("T5", "vennify/t5-base-grammar-correction")
grammar_cache = dc.Cache('grammar_cache')

# For academic style checks
sf = Styleformer(style=0) 
style_cache = dc.Cache('style_cache')

# For text generation
model_name = "Qwen/Qwen2.5-1.5B-Instruct"

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)
model.generation_config.max_new_tokens = 2048
tokenizer = AutoTokenizer.from_pretrained(model_name)
model_cache = dc.Cache('model_cache')

def generate_key(text):
    return hashlib.md5(text.encode()).hexdigest()


def split_into_chunks(text, max_tokens=MAX_TOKENS):
    sentences = nltk.sent_tokenize(text)
    chunks, current = [], ""
    current_tokens = 0

    for sentence in sentences:
        sentence_tokens = len(sentence.split())
        if current_tokens + sentence_tokens <= max_tokens:
            current += sentence + ' '
            current_tokens += sentence_tokens
        else:
            chunks.append(current.strip())
            current, current_tokens = sentence + ' ', sentence_tokens
    if current:
        chunks.append(current.strip())
    return chunks


# def split_into_chunks(text, max_tokens=MAX_TOKENS):
#     sentences = text.split(". ")
#     chunks = []
#     current = ""
#     for sentence in sentences:
#         if len(current.split()) + len(sentence.split()) <= max_tokens:
#             current += sentence + '. '
#         else:
#             chunks.append(current.strip())
#             current = sentence + '. '
#     if current:
#         chunks.append(current.strip())
#     return chunks

def clean_text(text):
    # Remove newlines within sentences but keep paragraph breaks
    text = re.sub(r'\n(?!\n)', ' ', text)
    
    # Remove multiple newlines, keeping only double newlines for paragraphs
    text = re.sub(r'\n{2,}', '\n\n', text)
    
    # Rejoin hyphenated words split across lines
    text = re.sub(r'(\w)-\s+(\w)', r'\1\2', text)
    
    # Remove citation brackets and figure numbers
    text = re.sub(r'\[\d+\]', '', text)  # Removes [7], [6], etc.
    text = re.sub(r'Fig\.|Figure', '', text)  # Removes "Fig." or "Figure" references

    # Strip leading/trailing spaces from each paragraph
    paragraphs = text.split('\n')
    cleaned_paragraphs = [para.strip() for para in paragraphs if para.strip()]
    
    # Join cleaned paragraphs back with double newlines for readability
    cleaned_text = '\n\n'.join(cleaned_paragraphs)
    
    return cleaned_text

def get_collection() -> chromadb.Collection:
    collection_names = [collection.name for collection in client.list_collections()]
    logging.info(f"Client collection names: {collection_names}")
    if collection_name not in collection_names:
        logging.info(f"Creation of a collection...")
        collection = client.create_collection(name=collection_name)
        papers = pd.read_csv("hf://datasets/somosnlp-hackathon-2022/scientific_papers_en/scientific_paper_en.csv")
        logging.info(f"The data downloaded from url.")
        papers = papers.drop(['id'], axis=1)
        papers = papers.iloc[:200]

        for i in range(200):
            paper = papers.iloc[i]
            idx = paper.name

            full_text = clean_text('Abstract ' + paper['abstract'] + ' ' + paper['text_no_abstract'])
            chunks = split_into_chunks(full_text)
            
            for id, chunk in enumerate(chunks):
                embeddings = embedder.encode([chunk])
                collection.upsert(ids=f"paper{idx}_chunk_{id}", 
                                documents=[chunk], 
                                embeddings=embeddings,)
            logging.info(f"Collection upsert: The content of paper_{idx} was chunked and collected in vector db!")
        
        logging.info(f"Collection is filled!\n")
    else:
        collection = client.get_collection(name=collection_name)
        logging.info(f"Collection '{collection_name}' already exists!")
    return collection

def fix_grammar(text: str):
    logging.info(f"\n---Fix Grammar input:---\n{text}")
    key = generate_key(text)
    if key in  grammar_cache:
        logging.info(f"Similar request was found in 'grammar_cache' and retrieved from it!")
        yield grammar_cache[key]

    else:
        args = TTSettings(num_beams=5, min_length=1)
        chunks = split_into_chunks(text=text, max_tokens=40)
        corrected_text = ""
        error_flag = False
        for chunk in chunks:
            try:
                result = happy_tt.generate_text(f"grammar: {chunk}", args=args)
                corrected_part = f"{result.text} "
            except Exception as e:
                error_flag = True
                logging.error(f"Error correcting grammar: {e}")
                corrected_part = f"{chunk} "
            corrected_text += corrected_part
            yield corrected_text

        if not error_flag:
            grammar_cache.set(key, corrected_text, expire=86400)
            logging.info(f"The result was cached in 'grammar_cache'!")

def fix_academic_style(informal_text: str):
    logging.info(f"\n---Fix Academic Style input:---\n{informal_text}")
    key = generate_key(informal_text)
    if key in style_cache:
        logging.info(f"Similar request was found in 'style_cache' and retrieved from it!")
        yield style_cache[key]
    
    else:
        chunks = split_into_chunks(text=informal_text, max_tokens=25)
        formal_text = ""
        error_flag = False
        for chunk in chunks:
            try:
                corrected_part = sf.transfer(chunk)
                if corrected_part is None:
                    error_flag = True
                    corrected_part = f"{chunk} "
                    logging.warning("---COULD NOT FIX ACADEMIC STYLE!\n")
                else:
                    corrected_part = f"{corrected_part} "
            except Exception as e:
                error_flag = True
                logging.error(f"Error in academic style transformation: {e}")
                corrected_part = f"{chunk} "
            formal_text += corrected_part
            yield formal_text

        if not error_flag:
            style_cache.set(key, formal_text, expire=86400)
            logging.info(f"The result was cached in 'style_cache'!")

def _chat_stream(initial_text: str, parts: list):
    logging.info(f"\n---Generate Article input:---\n{initial_text}")
    parts = ", ".join(parts).lower()
    for_cache = initial_text + ' ' + parts
    key = generate_key(for_cache)
    if key in model_cache:
        logging.info(f"Similar request was found in 'model_cache' and retrieved from it!")
        yield model_cache[key]
    else: 
        text_embedding = embedder.encode([initial_text])
        chroma_collection = get_collection()
        results = chroma_collection.query(
            query_embeddings=text_embedding,
            n_results=1
        )
        context = results['documents'][0] if results['documents'] else ""
        if context == "":
            logging.warning(f"COLLECTION QUERY: No context was found in the database!")

        messages = [
        {"role": "system", "content": """You are helpful Academic Research Assistant which helps to generate 
                                    necessary parts of the reserch based on the provided context.
                                    The context is the following: 'written text' - this is the text that user
                                    has for now and want to complete, 'parts' - those are the parts of paper 
                                    user needs to complete (it could be the abstract, introduction, methodology,
                                    discussion, conclusion, or full text), 'context' - the similar article 
                                    the structure of which can be used as a base for the text (it can be empty
                                    in case of absence of similar papers in the database.). The output should be
                                    only generated article (or parts of it). The responce must be provided as a 
                                    raw text. Be precise and follow the structure of academic papers parts."""},
        {"role": "user", "content": f"'written text': {initial_text}\n 'parts': {parts}\n 'context': {context}"},
        ]
        input_text = tokenizer.apply_chat_template(
            messages,
            add_generation_prompt=True,
            tokenize=False,
        )
        inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
        streamer = TextIteratorStreamer(
            tokenizer=tokenizer, skip_prompt=True, timeout=160.0, skip_special_tokens=True
        )
        generation_kwargs = {
            **inputs,
            "streamer": streamer,
        }
        thread = Thread(target=model.generate, kwargs=generation_kwargs)
        thread.start()

        response = ""
        for new_text in streamer:
            response += new_text
            yield response
        model_cache.set(key, response, expire=86400)
        logging.info(f"The result was cached in 'model_cache'!")

def predict(goal: str, parts: list, context: str):
        if context == "":
            yield "Write your text first!"
            logging.info("No context was provided!")
        elif goal == 'Fix Academic Style':
            formal_text = ""
            try:
                for new_text in fix_academic_style(context):
                    formal_text = new_text
                    yield formal_text
                if not formal_text:
                    yield "Generation failed or timed out. Please try again!"

                logging.info(f"\n---Academic style corrected:---\n {formal_text}\n")
            except Exception as e:
                logging.error(f"Error in 'Fix Academic Style' occured: {e}")
                yield "Try to wait a little bit and resend your request!"

        elif goal == 'Fix Grammar':
            try:
                full_response = ""
                for new_text in fix_grammar(context):
                    full_response = new_text
                    yield full_response
                
                if not full_response:
                    yield "Generation failed or timed out. Please try again!"
                
                logging.info(f"\n---Grammar corrected:---\n{full_response}\n")
            except Exception as e:
                logging.error(f"Error in 'Fix Grammar' occured: {e}")
                yield "Try to wait a little bit and resend your request!"

        else:
            try:
                full_response = ""
                for new_text in _chat_stream(context, parts):
                    full_response = new_text
                    yield full_response
                
                if not full_response:
                    yield "Generation failed or timed out. Please try again!"

                logging.info(f"\nThe text was generated!\n{full_response}")
            except Exception as e:
                logging.error(f"Error in 'Write Text' occured: {e}")
                yield "Try to wait a little bit and resend your request!"