File size: 28,355 Bytes
b6529a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from mistralai import Mistral
from langchain_community.tools import TavilySearchResults, JinaSearch
import concurrent.futures
import json
import os
import arxiv 
from PIL import Image
import io
import base64
from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain
from langchain.text_splitter import CharacterTextSplitter
from langchain_mistralai import ChatMistralAI
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains.llm import LLMChain
from langchain_core.prompts import PromptTemplate
from json_repair import repair_json
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("mistral-community/pixtral-12b")

def count_tokens_in_text(text):
    tokens = tokenizer(text, return_tensors="pt", truncation=False, add_special_tokens=True)
    return len(tokens["input_ids"][0])

# Set environment variables for Tavily API
os.environ["TAVILY_API_KEY"] = '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'

# Mistral client API keys
client_1 = Mistral(api_key='eLES5HrVqduOE1OSWG6C5XyEUeR7qpXQ')
client_2 = Mistral(api_key='VPqG8sCy3JX5zFkpdiZ7bRSnTLKwngFJ')
client_3 = Mistral(api_key='cvyu5Rdk2lS026epqL4VB6BMPUcUMSgt')
api_key_4 = 'aYls8aj48SOEov8AY1dwp4hr07MsCRFb'
client_4 = ChatMistralAI(api_key=api_key_4, model="pixtral-12b-2409")

# Function to encode images in base64
def encode_image_bytes(image_bytes):
    return base64.b64encode(image_bytes).decode('utf-8')

# Function to decode base64 images
def decode_base64_image(base64_str):
    image_data = base64.b64decode(base64_str)
    return Image.open(io.BytesIO(image_data))

# Process text and images provided by the user
def process_input(text_input, images_base64):
    images = []
    if images_base64:
        for img_data in images_base64:
            try:
                img = decode_base64_image(img_data)
                buffered = io.BytesIO()
                img.save(buffered, format="JPEG")
                image_base64 = encode_image_bytes(buffered.getvalue())
                images.append({"type": "image_url", "image_url": f"data:image/jpeg;base64,{image_base64}"})
            except Exception as e:
                print(f"Error decoding image: {e}")

    return text_input, images

# Search setup function
def setup_search(question):
    try:
        tavily_tool = TavilySearchResults(max_results=20)
        results = tavily_tool.invoke({"query": f"{question}"})
        if isinstance(results, list):
            return results, 'tavily_tool'
    except Exception as e:
        print("Error with TavilySearchResults:", e)
    try:
        jina_tool = JinaSearch()
        results = json.loads(str(jina_tool.invoke({"query": f"{question}"})))
        if isinstance(results, list):
            return results, 'jina_tool'
    except Exception as e:
        print("Error with JinaSearch:", e)
    return [], ''



def lit_obr(text , crit):

    api_key = 'vjOgcQPigpidK7njWV5jPidP69CHg5Yg'
    model = "pixtral-12b-2409"
    client = Mistral(api_key=api_key)
    client_4 = ChatMistralAI(api_key=api_key, model=model)
    
    def count_tokens_in_text(text):
        tokens = tokenizer(text, return_tensors="pt", truncation=False, add_special_tokens=True)
        return len(tokens["input_ids"][0])
    
    prom = """
    #####
    # Выведи итог строго в формате JSON. Убедись, что:
    # - JSON является валидным и имеет правильную вложенность.
    # - Все строки (ключи и значения) заключены в двойные кавычки.
    # - Нет лишних запятых.
    # - Используй формат структуры, приведённой ниже.
    #####
    {"comparison_table": {"markdown": "| article title | criterion name 1 | criterion name 2 | criterion name 3 |\n|---------------|------------------|------------------|------------------|\n| article title 1 | result | result | result |\n| article title 2 | result | result | result |\n| article title 3 | result | result | result |"},
    "quotes": {
        "criterion name 1": {
        "article title 1": "citation",
        "article title 2": "citation",
        "article title 3": "citation"
        },
        "criterion name 2": {
        "article title 1": "citation",
        "article title 2": "citation",
        "article title 3": "citation"
        },
        "criterion name 3": {
        "article title 1": "citation",
        "article title 2": "citation",
        "article title 3": "citation"
        }
    },
    "conclusion": "result"
    }
    #####
    # Убедись, что:
    # - Поле "comparison_table.markdown" содержит корректно отформатированную таблицу с заголовками и данными.
    # - Поля "quotes" содержат цитаты по указанным критериям для каждой статьи.
    # - Поле "conclusion" включает краткое заключение о сравнении статей.
    # 
    # Если есть неуверенность, уточни формат или структуру перед генерацией.
    #####
    """
    
    def process_scientific_articles_for_analysis_1(text, criter_prompts=""):
        promt = f"""
            Analyze scientific articles based on the criteria provided by the user. Extract relevant data from the text and present a concise comparative review.
            Provide a brief literature review in the following format as a table, including article titles (not their indices) in the comparison row.

            Represent the comparison in the form of a table, where:

            - The first vertical column contains the titles of the articles in a shortened form without losing their meaning, strictly as text, and without indices.
            - Subsequent columns contain concise information for each criterion, formulated based on the text of the article. The information should be brief but capture the essence without directly copying the text.

            Additionally, below the table, provide full quotes from the text that confirm the data presented in the table:
            - Each quote should be presented without any changes or interpretation.
            - Quotes must be in the original language of the article.
            - Group quotes by articles: start with the article title, followed by the quotes for each criterion.

            Ensure the output is clear and useful.

            Result requirements:
            - The table should only contain concise information extracted from the text in the cells.
            - Full quotes must be provided separately, below the table.
            - Do not include author names or publication dates in the quotes.
            - Both the concise data and the quotes should be presented in the language in which the articles are written.

            Start numbering the articles from the first, excluding zero.

            Input data:
            Articles:
            {text}

            Criteria:
            {criter_prompts}

            Result format:
            {prom}
        """




        chat_response = client.chat.complete(
            model=model,
            messages= [{ "role": "user", "content":  [{ "type": "text", "text": promt}] }]
        )

        return chat_response.choices[0].message.content
    
    def process_scientific_articles_for_analysis_2(text, images=[], criter_prompts=""):
        map_template = f"""
            {{docs}}
            Analyze the scientific articles based on the criteria provided by the user. Extract the relevant data from the text and present a concise comparative review.
            Provide a summary literature review in the following format as a table, including the article titles (not their indices) in the comparison row.

            Present the comparisons in the form of a table where:

            The first vertical column lists the titles of the articles, shortened without losing their meaning, and in no other format.
            Subsequent columns represent the parameters provided below.
            Rows contain concise quotes extracted from the text.
            Additionally, below the table, provide direct quotes from the text without any summarization or changes that confirm the data presented in the table. These quotes must consist only of sentences from the text, excluding publication dates and author names. If no data is available, state "No data available." Present each quote on a separate line under the corresponding criterion in the table, group the quotes by article, and include the article titles (not indices). Write the quotes in the language in which they appear in the text.

            Start numbering the articles from the first (excluding zero). Do not include the authors or publication dates of the articles in the quotes, do not number each quote line, but present each quote on a new line:
            {{criter_prompts}}
            
            Give a brief literature review in the following format:
            Provide the following JSON structure:
            {{comparison_table}}
        """

        reduce_template = f"""Следующий текст состоит из нескольких кратких итогов:
            {{docs}}

            На основе этих кратких итогов, проведи анализ научных статей по введенным критериям, объединяя основные данные и выводя обобщающий литературный обзор.
            Выведи результат в следующем формате:

            1. Таблица, где:
            - Первая колонка по вертикали — это названия статей (сокращенные без потери смысла).
            - Последующие колонки — это критерии анализа.
            - Строки содержат краткие данные по тексту каждой статьи, соответствующие критериям.

            2. Под таблицей укажи прямые цитаты из текста, подтверждающие данные в таблице. Каждую цитату:
            - Группируй по статьям.
            - Пиши на языке оригинала текста.
            - Не включай авторов и даты написания статьи.
            - Если данных нет, укажи "Данных нет".

            Обязательно предоставь полезный и четкий вывод. 
            Результат:

            Приведи краткий обзор литературы в следующем формате:
            {{comparison_table}}
        """


        map_prompt = PromptTemplate.from_template(map_template)
        map_chain = LLMChain(llm=client_4, prompt=map_prompt)

        reduce_prompt = PromptTemplate.from_template(reduce_template)
        reduce_chain = LLMChain(llm=client_4, prompt=reduce_prompt)

        combine_documents_chain = StuffDocumentsChain(
            llm_chain=reduce_chain, document_variable_name="docs"
        )

        reduce_documents_chain = ReduceDocumentsChain(
            combine_documents_chain=combine_documents_chain,
            collapse_documents_chain=combine_documents_chain,
            token_max=128000,
        )

        map_reduce_chain = MapReduceDocumentsChain(
            llm_chain=map_chain,
            reduce_documents_chain=reduce_documents_chain,
            document_variable_name="docs",
            return_intermediate_steps=False,
        )

        text_splitter = CharacterTextSplitter.from_huggingface_tokenizer(
            tokenizer,
            chunk_size=100000,
            chunk_overlap=14000,
        )

        split_docs = text_splitter.create_documents([text])

        image_descriptions = "\n".join(
            [f"Изображение {i+1}: {img['image_url']}" for i, img in enumerate(images)]
        )

        result = map_reduce_chain.run({"input_documents": split_docs, "images": image_descriptions, "comparison_table": prom, 'criter_prompts': criter_prompts})
        return result
    
    def init(text_data, criter):

        if count_tokens_in_text(text_data) < 128000:
            rezult = process_scientific_articles_for_analysis_1(text_data, criter)
        else:
            rezult = process_scientific_articles_for_analysis_2(text_data, criter_prompts = criter)

        return json.loads(repair_json(rezult[7:-4]))  #repair_json(rezult[7:-4])
    
    return init(text , crit)
    

# Function to extract key topics
def extract_key_topics(content, images=[]):
    prompt = f"""
    Extract the primary themes from the text below. List each theme in as few words as possible, focusing on essential concepts only. Format as a concise, unordered list with no extraneous words.
    ```{content}```
    LIST IN ENGLISH:
    - 
    """
    message_content = [{"type": "text", "text": prompt}] + list(images)
    response = client_1.chat.complete(
        model="pixtral-12b-2409",
        messages=[{"role": "user", "content": message_content}]
    )
    return response.choices[0].message.content

def extract_key_topics_with_large_text(content, images=[]):
    # Map prompt template for extracting key themes
    map_template = f"""
        Текст: {{docs}}
        Изображения: {{images}}
        Extract the primary themes from the text below. List each theme in as few words as possible, focusing on essential concepts only. Format as a concise, unordered list with no extraneous words.
        LIST IN ENGLISH:
        - 
        :"""

    map_prompt = PromptTemplate.from_template(map_template)
    map_chain = LLMChain(llm=client_4, prompt=map_prompt)

    # Reduce prompt template to further refine and extract key themes
    reduce_template = f"""Следующий текст состоит из нескольких кратких итогов:
        {{docs}}
        Extract the primary themes from the text below. List each theme in as few words as possible, focusing on essential concepts only. Format as a concise, unordered list with no extraneous words.
        LIST IN ENGLISH:
        - 
        :"""

    reduce_prompt = PromptTemplate.from_template(reduce_template)
    reduce_chain = LLMChain(llm=client_4, prompt=reduce_prompt)

    # Combine documents chain for Reduce step
    combine_documents_chain = StuffDocumentsChain(
        llm_chain=reduce_chain, document_variable_name="docs"
    )

    # ReduceDocumentsChain configuration
    reduce_documents_chain = ReduceDocumentsChain(
        combine_documents_chain=combine_documents_chain,
        collapse_documents_chain=combine_documents_chain,
        token_max=128000,
    )

    # MapReduceDocumentsChain combining Map and Reduce
    map_reduce_chain = MapReduceDocumentsChain(
        llm_chain=map_chain,
        reduce_documents_chain=reduce_documents_chain,
        document_variable_name="docs",
        return_intermediate_steps=False,
    )

    # Text splitter configuration
    text_splitter = CharacterTextSplitter.from_huggingface_tokenizer(
        tokenizer,
        chunk_size=100000,
        chunk_overlap=14000,
    )

    # Split the text into documents
    split_docs = text_splitter.create_documents([content])

    # Include image descriptions (optional, if required by the prompt)
    image_descriptions = "\n".join(
        [f"Изображение {i+1}: {img['image_url']}" for i, img in enumerate(images)]
    )

    # Run the summarization chain to extract key themes
    key_topics = map_reduce_chain.run({"input_documents": split_docs, "images": image_descriptions})
    return key_topics

def search_relevant_articles_arxiv(key_topics, max_articles=10):
    articles_by_topic = {}
    final_topics = []

    def fetch_articles_for_topic(topic):
        topic_articles = []
        try:
            # Fetch articles using arxiv.py based on the topic
            search = arxiv.Search(
                query=topic,
                max_results=max_articles,
                sort_by=arxiv.SortCriterion.Relevance
            )
            for result in search.results():
                article_data = {
                    "title": result.title,
                    "doi": result.doi,
                    "summary": result.summary,
                    "url": result.entry_id,
                    "pdf_url": result.pdf_url
                }
                topic_articles.append(article_data)
            final_topics.append(topic)
        except Exception as e:
            print(f"Error fetching articles for topic '{topic}': {e}")

        return topic, topic_articles

    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        # Use threads to fetch articles for each topic
        futures = {executor.submit(fetch_articles_for_topic, topic): topic for topic in key_topics}
        for future in concurrent.futures.as_completed(futures):
            topic, articles = future.result()
            if articles:
                articles_by_topic[topic] = articles

    return articles_by_topic, list(set(final_topics))

def init(content, images=[]):
    if count_tokens_in_text(text=content) < 128_000:
        key_topics = extract_key_topics(content, images)
        key_topics = [topic.strip("- ") for topic in key_topics.split("\n") if topic]
        articles_by_topic, final_topics = search_relevant_articles_arxiv(key_topics)
        result_json = json.dumps(articles_by_topic, indent=4)
        return final_topics, result_json
    else:
        key_topics = extract_key_topics_with_large_text(content, images) 
        key_topics = [topic.strip("- ") for topic in key_topics.split("\n") if topic]
        articles_by_topic, final_topics = search_relevant_articles_arxiv(key_topics)
        result_json = json.dumps(articles_by_topic, indent=4)
        return final_topics, result_json

def process_article_for_summary(text, images=[], compression_percentage=30):
    prompt = f"""
    You are a commentator.
    # article:
    {text}
    # Instructions:
    ## Summarize IN RUSSIAN:
    In clear and concise language, summarize the key points and themes presented in the article by cutting it by {compression_percentage} percent.
    """

    if len(images) >= 8 : 
        images = images[:7]
    
    message_content = [{"type": "text", "text": prompt}] + images
    response = client_3.chat.complete(
        model="pixtral-12b-2409",
        messages=[{"role": "user", "content": message_content}]
    )
    return response.choices[0].message.content

def process_large_article_for_summary(text, images=[], compression_percentage=30):
    # Map prompt template
    map_template = f"""Следующий текст состоит из текста и изображений:
        Текст: {{docs}}
        Изображения: {{images}}
        На основе приведенного материала, выполните сжатие текста, выделяя основные темы и важные моменты. 
        Уровень сжатия: {compression_percentage}%. 
        Ответ предоставьте на русском языке в формате Markdown.
        Полезный ответ:"""

    map_prompt = PromptTemplate.from_template(map_template)
    map_chain = LLMChain(llm=client_4, prompt=map_prompt)

    # Reduce prompt template
    reduce_template = f"""Следующий текст состоит из нескольких кратких итогов:
        {{docs}}
        На основе этих кратких итогов, выполните финальное сжатие текста, объединяя основные темы и ключевые моменты. 
        Уровень сжатия: {compression_percentage}%. 
        Результат предоставьте на русском языке в формате Markdown.
        Полезный ответ:"""

    reduce_prompt = PromptTemplate.from_template(reduce_template)
    reduce_chain = LLMChain(llm=client_4, prompt=reduce_prompt)

    # Combine documents chain for Reduce step
    combine_documents_chain = StuffDocumentsChain(
        llm_chain=reduce_chain, document_variable_name="docs"
    )

    # ReduceDocumentsChain configuration
    reduce_documents_chain = ReduceDocumentsChain(
        combine_documents_chain=combine_documents_chain,
        collapse_documents_chain=combine_documents_chain,
        token_max=128000,
    )

    # MapReduceDocumentsChain combining Map and Reduce
    map_reduce_chain = MapReduceDocumentsChain(
        llm_chain=map_chain,
        reduce_documents_chain=reduce_documents_chain,
        document_variable_name="docs",
        return_intermediate_steps=False,
    )

    # Text splitter configuration
    text_splitter = CharacterTextSplitter.from_huggingface_tokenizer(
        tokenizer,
        chunk_size=100000,
        chunk_overlap=14000,
    )

    # Split the text into documents
    split_docs = text_splitter.create_documents([text])
    # Include image descriptions
    image_descriptions = "\n".join(
        [f"Изображение {i+1}: {img['image_url']}" for i, img in enumerate(images)]
    )

    # Run the summarization chain
    summary = map_reduce_chain.run({"input_documents": split_docs, "images": image_descriptions})
    return summary

def ask_question_to_mistral(text, question, context , images=[]):
    prompt = f"Answer the following question without mentioning it or repeating the original text on which the question is asked in style markdown.IN RUSSIAN:\nQuestion: {question}\n\nText:\n{text}"

    if len(images) >= 8 : 
        images = images[:7]
    
    message_content = [{"type": "text", "text": prompt}] + images
    response = client_2.chat.complete(
        model="pixtral-12b-2409",
        messages=[{"role": "user", "content": f'{message_content}\n\nAdditional Context from Web Search:\n{context}'}]
    )
    return response.choices[0].message.content

def ask_question_to_mistral_with_large_text(text, question, context , images=[]):
    # Prompts for QA
    map_template = """Следующий текст содержит статью/произведение:
    Текст: {docs}
    Изображения: {{images}}
    На основе приведенного текста, ответьте на следующий вопрос:
    Вопрос: {{question}}
    Ответ должен быть точным. Пожалуйста, ответьте на русском языке в формате Markdown.
    Информация из интернета: {{context}}
    Полезный ответ:"""

    reduce_template = """Следующий текст содержит несколько кратких ответов на вопрос:
    {docs}
    Объедините их в финальный ответ. Ответ предоставьте на русском языке в формате Markdown.
    Полезный ответ:"""

    map_prompt = PromptTemplate.from_template(map_template)
    map_chain = LLMChain(llm=client_4, prompt=map_prompt)

    reduce_prompt = PromptTemplate.from_template(reduce_template)
    reduce_chain = LLMChain(llm=client_4, prompt=reduce_prompt)

    # Combine documents chain for Reduce step
    combine_documents_chain = StuffDocumentsChain(
        llm_chain=reduce_chain, document_variable_name="docs"
    )

    # ReduceDocumentsChain configuration
    reduce_documents_chain = ReduceDocumentsChain(
        combine_documents_chain=combine_documents_chain,
        collapse_documents_chain=combine_documents_chain,
        token_max=128000,
    )

    # MapReduceDocumentsChain combining Map and Reduce
    map_reduce_chain = MapReduceDocumentsChain(
        llm_chain=map_chain,
        reduce_documents_chain=reduce_documents_chain,
        document_variable_name="docs",
        return_intermediate_steps=False,
    )

    # Text splitter configuration
    text_splitter = CharacterTextSplitter.from_huggingface_tokenizer(
        tokenizer,
        chunk_size=100000,
        chunk_overlap=14000,
    )

    # Split the text into documents
    split_docs = text_splitter.create_documents([text])

    # Include image descriptions
    image_descriptions = "\n".join(
        [f"Изображение {i+1}: {img['image_url']}" for i, img in enumerate(images)]
    )    

    answer = map_reduce_chain.run({"input_documents": split_docs, "question": question , 'context': context , "images": image_descriptions})
    return answer

def gradio_interface(text_input, images_base64, task, question, crit, compression_percentage):
    text, images = process_input(text_input, images_base64)

    if task == "Summarization":

        if count_tokens_in_text(text=text) < 128_000:
            summary = process_article_for_summary(text, images, compression_percentage)
            return {"Summary": summary }
        
        else:
            summary= process_large_article_for_summary(text, images, compression_percentage)
            return {"Summary": summary, }
        
    elif task == "Question Answering":

        if question:

            search_tool, tool = setup_search(question)
            context = ''
            if search_tool:
                if tool == 'tavily_tool':
                    for result in search_tool:
                        context += f"{result.get('url', 'N/A')} : {result.get('content', 'No content')} \n"
                elif tool == 'jina_tool':
                    for result in search_tool:
                        context += f"{result.get('link', 'N/A')} : {result.get('snippet', 'No snippet')} : {result.get('content', 'No content')} \n"


            if count_tokens_in_text(text + context) < 128_000:
                answer = ask_question_to_mistral(text, question, context , images)
                return {"Answer": answer }
            else:
                answer = ask_question_to_mistral_with_large_text(text, question, context , images)
                return {"Answer": answer}
        else:
            return {"Answer": "No question provided." }

    elif task == 'Search Article' :
        return init(text , images_base64)

    elif task == 'Lit Obzor' :
        return lit_obr(text , crit)


with gr.Blocks() as demo:
    gr.Markdown("## Text Analysis: Summarization or Question Answering")
    
    with gr.Row():
        text_input = gr.Textbox(label="Input Text")
        images_base64 = gr.Textbox(label="Base64 Images (comma-separated, if any)", placeholder="data:image/jpeg;base64,...", lines=2)
        task_choice = gr.Radio(["Summarization", "Question Answering", "Search Article", "Lit Obzor"], label="Select Task")
        question_input = gr.Textbox(label="Question (for Question Answering)", visible=False)
        lit_crit = gr.Textbox(label="Критерии для лит обзора", visible=False, placeholder="Введите критерии для литературного обзора.")
        compression_input = gr.Slider(label="Compression Percentage (for Summarization)", minimum=10, maximum=90, value=30, visible=False)

    # Скрытие или отображение компонентов в зависимости от выбора задачи
    task_choice.change(lambda choice: (
        gr.update(visible=choice == "Question Answering"),  # For question input visibility
        gr.update(visible=choice == "Summarization"),  # For compression percentage visibility
        gr.update(visible=choice == "Lit Obzor")  # For literary review criteria visibility
    ), inputs=task_choice, outputs=[question_input, compression_input, lit_crit])


    with gr.Row():
        result_output = gr.JSON(label="Results")

    submit_button = gr.Button("Submit")
    submit_button.click(gradio_interface, 
                        inputs=[text_input, images_base64, task_choice, question_input, lit_crit, compression_input], 
                        outputs=result_output)

demo.launch(show_error=True)