File size: 10,210 Bytes
4cc0ea8
 
 
 
 
 
 
 
 
42638f9
4cc0ea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42638f9
4cc0ea8
 
 
 
 
 
 
 
 
 
42638f9
 
 
 
 
4cc0ea8
 
 
 
 
 
 
42638f9
 
 
 
 
4cc0ea8
42638f9
 
 
 
 
4cc0ea8
42638f9
 
 
 
 
4cc0ea8
42638f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cc0ea8
42638f9
4cc0ea8
42638f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cc0ea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42638f9
4cc0ea8
 
 
 
 
 
 
 
42638f9
 
 
 
 
 
 
4cc0ea8
 
42638f9
 
4cc0ea8
 
 
 
 
 
 
 
 
42638f9
4cc0ea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import json
import time
import tempfile
from typing import Dict, Any, List, Optional
from transformers import AutoTokenizer
from sentence_transformers import SentenceTransformer
from huggingface_hub import login
from src.prompts import SUMMARY_PROMPT_TEMPLATE, QUIZ_PROMPT_TEMPLATE

GEMINI_MODEL = "gemini-2.0-flash"
DEFAULT_TEMPERATURE = 0.7

TOKENIZER_MODEL = "answerdotai/ModernBERT-base"
SENTENCE_TRANSFORMER_MODEL = "all-MiniLM-L6-v2"

hf_token = os.environ.get('HF_TOKEN', None)
login(token=hf_token)

tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_MODEL)
sentence_model = SentenceTransformer(SENTENCE_TRANSFORMER_MODEL)

def clean_text(text):
    text = re.sub(r'\[speaker_\d+\]', '', text)
    text = re.sub(r'\s+', ' ', text).strip()
    return text

def split_text_by_tokens(text, max_tokens=12000):
    text = clean_text(text)
    tokens = tokenizer.encode(text)
    
    if len(tokens) <= max_tokens:
        return [text]
    
    split_point = len(tokens) // 2
    
    sentences = re.split(r'(?<=[.!?])\s+', text)
    
    first_half = []
    second_half = []
    
    current_tokens = 0
    for sentence in sentences:
        sentence_tokens = len(tokenizer.encode(sentence))
        
        if current_tokens + sentence_tokens <= split_point:
            first_half.append(sentence)
            current_tokens += sentence_tokens
        else:
            second_half.append(sentence)
    
    return [" ".join(first_half), " ".join(second_half)]

def generate_with_gemini(text, api_key, language, content_type="summary"):
    from langchain_google_genai import ChatGoogleGenerativeAI
    os.environ["GOOGLE_API_KEY"] = api_key
    llm = ChatGoogleGenerativeAI(
        model=GEMINI_MODEL,
        temperature=DEFAULT_TEMPERATURE,
        max_retries=3
    )
    
    if content_type == "summary":
        base_prompt = SUMMARY_PROMPT_TEMPLATE.format(text=text)
    else:
        base_prompt = QUIZ_PROMPT_TEMPLATE.format(text=text)

    language_instruction = f"\nIMPORTANT: Generate ALL content in {language} language."
    prompt = base_prompt + language_instruction
    
    try:
        messages = [
            {"role": "system", "content": "You are a helpful AI assistant that creates high-quality text summaries and quizzes."},
            {"role": "user", "content": prompt}
        ]
        
        response = llm.invoke(messages)
        
        try:
            content = response.content
            json_match = re.search(r'```json\s*([\s\S]*?)\s*```', content)
            
            if json_match:
                json_str = json_match.group(1)
            else:
                json_match = re.search(r'(\{[\s\S]*\})', content)
                if json_match:
                    json_str = json_match.group(1)
                else:
                    json_str = content
            
            # Parse the JSON
            function_call = json.loads(json_str)
            return function_call
        except json.JSONDecodeError:
            raise Exception("Could not parse JSON from LLM response")
    except Exception as e:
        raise Exception(f"Error calling API: {str(e)}")

def format_summary_for_display(results, language="English"):
    output = []
    
    if language == "Uzbek":
        title_header = "SARLAVHA"
        overview_header = "UMUMIY KO'RINISH"
        key_points_header = "ASOSIY NUQTALAR"
        key_entities_header = "ASOSIY SHAXSLAR VA TUSHUNCHALAR"
        conclusion_header = "XULOSA"
    elif language == "Russian":
        title_header = "ЗАГОЛОВОК"
        overview_header = "ОБЗОР"
        key_points_header = "КЛЮЧЕВЫЕ МОМЕНТЫ"
        key_entities_header = "КЛЮЧЕВЫЕ ОБЪЕКТЫ"
        conclusion_header = "ЗАКЛЮЧЕНИЕ"
    else:
        title_header = "TITLE"
        overview_header = "OVERVIEW"
        key_points_header = "KEY POINTS"
        key_entities_header = "KEY ENTITIES"
        conclusion_header = "CONCLUSION"
    
    if "summary" not in results:
        if "segments" in results:
            segments = results.get("segments", [])
            for i, segment in enumerate(segments):
                topic = segment.get("topic_name", f"Section {i+1}")
                segment_num = i + 1
                output.append(f"\n\n{'='*40}")
                output.append(f"SEGMENT {segment_num}: {topic}")
                output.append(f"{'='*40}\n")
                
                if "key_concepts" in segment:
                    output.append("KEY CONCEPTS:")
                    for concept in segment["key_concepts"]:
                        output.append(f"• {concept}")
                
                if "summary" in segment:
                    output.append("\nSUMMARY:")
                    output.append(segment["summary"])
            
            return "\n".join(output)
        else:
            return "Error: Could not parse summary results. Invalid format received."
    
    summary = results["summary"]
    if "title" in summary:
        output.append(f"\n\n{'='*40}")
        output.append(f"{title_header}: {summary['title']}")
        output.append(f"{'='*40}\n")
    
    # Overview
    if "overview" in summary:
        output.append(f"{overview_header}:")
        output.append(f"{summary['overview']}\n")
    
    # Key Points
    if "key_points" in summary and summary["key_points"]:
        output.append(f"{key_points_header}:")
        for theme_group in summary["key_points"]:
            if "theme" in theme_group:
                output.append(f"\n{theme_group['theme']}:")
            if "points" in theme_group:
                for point in theme_group["points"]:
                    output.append(f"• {point}")
    
    # Key Entities
    if "key_entities" in summary and summary["key_entities"]:
        output.append(f"\n{key_entities_header}:")
        for entity in summary["key_entities"]:
            if "name" in entity and "description" in entity:
                output.append(f"• **{entity['name']}**: {entity['description']}")
    
    # Conclusion
    if "conclusion" in summary:
        output.append(f"\n{conclusion_header}:")
        output.append(summary["conclusion"])
    
    return "\n".join(output)

def format_quiz_for_display(results, language="English"):
    output = []
    
    if language == "Uzbek":
        quiz_questions_header = "TEST SAVOLLARI"
    elif language == "Russian":
        quiz_questions_header = "ТЕСТОВЫЕ ВОПРОСЫ"
    else:
        quiz_questions_header = "QUIZ QUESTIONS"
    
    output.append(f"{'='*40}")
    output.append(f"{quiz_questions_header}")
    output.append(f"{'='*40}\n")
    
    quiz_questions = results.get("quiz_questions", [])
    for i, q in enumerate(quiz_questions):
        output.append(f"\n{i+1}. {q['question']}")
        for j, option in enumerate(q['options']):
            letter = chr(97 + j).upper()
            correct_marker = " ✓" if option["correct"] else ""
            output.append(f"   {letter}. {option['text']}{correct_marker}")
    
    return "\n".join(output)

def analyze_document(text, gemini_api_key, language, content_type="summary"):
    try:
        start_time = time.time()
        text_parts = split_text_by_tokens(text)

        input_tokens = 0        
        output_tokens = 0
        
        if content_type == "summary":
            all_results = {}
            
            for part in text_parts:
                actual_prompt = SUMMARY_PROMPT_TEMPLATE.format(text=part)
                prompt_tokens = len(tokenizer.encode(actual_prompt))
                input_tokens += prompt_tokens
                
                analysis = generate_with_gemini(part, gemini_api_key, language, "summary")
                
                if not all_results and "summary" in analysis:
                    all_results = analysis
                elif "summary" in analysis:
                    if "key_points" in analysis["summary"] and "key_points" in all_results["summary"]:
                        all_results["summary"]["key_points"].extend(analysis["summary"]["key_points"])
                    if "key_entities" in analysis["summary"] and "key_entities" in all_results["summary"]:
                        all_results["summary"]["key_entities"].extend(analysis["summary"]["key_entities"])
            
            formatted_output = format_summary_for_display(all_results, language)
                
        else:  
            all_results = {"quiz_questions": []}
            
            for part in text_parts:
                actual_prompt = QUIZ_PROMPT_TEMPLATE.format(text=part)
                prompt_tokens = len(tokenizer.encode(actual_prompt))
                input_tokens += prompt_tokens
                
                analysis = generate_with_gemini(part, gemini_api_key, language, "quiz")
                
                if "quiz_questions" in analysis:
                    remaining_slots = 10 - len(all_results["quiz_questions"])
                    if remaining_slots > 0:
                        questions_to_add = analysis["quiz_questions"][:remaining_slots]
                        all_results["quiz_questions"].extend(questions_to_add)
            
            formatted_output = format_quiz_for_display(all_results, language)
        
        end_time = time.time()
        total_time = end_time - start_time
        
        output_tokens = len(tokenizer.encode(formatted_output))
        token_info = f"Input tokens: {input_tokens}\nOutput tokens: {output_tokens}\nTotal tokens: {input_tokens + output_tokens}\n"
        formatted_text = f"Total Processing time: {total_time:.2f}s\n{token_info}\n" + formatted_output

        json_path = tempfile.mktemp(suffix='.json')
        with open(json_path, 'w', encoding='utf-8') as json_file:
            json.dump(all_results, json_file, indent=2)
        
        txt_path = tempfile.mktemp(suffix='.txt')
        with open(txt_path, 'w', encoding='utf-8') as txt_file:
            txt_file.write(formatted_text)
            
        return formatted_text, json_path, txt_path
    except Exception as e:
        error_message = f"Error processing document: {str(e)}"
        return error_message, None, None