Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,60 +54,71 @@ except Exception as e:
|
|
| 54 |
def generate_quiz(text: str):
|
| 55 |
"""
|
| 56 |
Генерирует один вопрос и два варианта ответа на основе текста.
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
try:
|
| 60 |
-
# Разбиваем текст на предложения
|
| 61 |
sentences = [s.strip() for s in text.replace("!", ".").replace("?", ".").split(".") if s.strip()]
|
| 62 |
-
|
| 63 |
if len(sentences) < 1:
|
| 64 |
-
raise ValueError("Текст слишком
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
question = f"Что сказано в этом предложении?"
|
| 76 |
-
else:
|
| 77 |
-
# Формируем вопрос
|
| 78 |
-
first_word = words[0].lower()
|
| 79 |
-
if first_word in ["кто", "что", "где", "когда", "почему", "как", "сколько"]:
|
| 80 |
-
question = question_sentence + "?"
|
| 81 |
else:
|
| 82 |
question = "Что сказано в тексте?"
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
#
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
else:
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
# Перемешиваем варианты
|
| 102 |
options = [correct_answer, wrong_answer]
|
| 103 |
random.shuffle(options)
|
| 104 |
-
|
| 105 |
return question, options, correct_answer
|
| 106 |
-
|
| 107 |
except Exception as e:
|
| 108 |
raise ValueError(f"Ошибка генерации вопроса: {str(e)}")
|
| 109 |
|
| 110 |
|
|
|
|
| 111 |
def synthesize_audio(text_ru: str):
|
| 112 |
"""Переводит русскую строку на казахский, синтезирует аудио и возвращает путь к файлу .wav"""
|
| 113 |
translation = translator(text_ru, src_lang="rus_Cyrl", tgt_lang="kaz_Cyrl")
|
|
|
|
| 54 |
def generate_quiz(text: str):
|
| 55 |
"""
|
| 56 |
Генерирует один вопрос и два варианта ответа на основе текста.
|
| 57 |
+
Алгоритмы:
|
| 58 |
+
1. Базовый: случайное предложение и первые слова.
|
| 59 |
+
2. Пропуск ключевого слова.
|
| 60 |
+
3. Вопрос о числе/дате.
|
| 61 |
"""
|
| 62 |
try:
|
|
|
|
| 63 |
sentences = [s.strip() for s in text.replace("!", ".").replace("?", ".").split(".") if s.strip()]
|
|
|
|
| 64 |
if len(sentences) < 1:
|
| 65 |
+
raise ValueError("Текст слишком короткий")
|
| 66 |
+
|
| 67 |
+
algo = random.choice([1, 2, 3])
|
| 68 |
+
# ------------------------
|
| 69 |
+
if algo == 1:
|
| 70 |
+
# Базовый алгоритм
|
| 71 |
+
question_sentence = random.choice(sentences)
|
| 72 |
+
words = question_sentence.split()
|
| 73 |
+
if len(words) <= 3:
|
| 74 |
+
correct_answer = question_sentence
|
| 75 |
+
question = "Что сказано в этом предложении?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
else:
|
| 77 |
question = "Что сказано в тексте?"
|
| 78 |
+
correct_answer = " ".join(words[:6]) + ("..." if len(words) > 6 else "")
|
| 79 |
|
| 80 |
+
wrong_sentence = random.choice([s for s in sentences if s != question_sentence] or ["Другая информация"])
|
| 81 |
+
wrong_words = wrong_sentence.split()
|
| 82 |
+
wrong_answer = " ".join(wrong_words[:6]) + ("..." if len(wrong_words) > 6 else "")
|
| 83 |
|
| 84 |
+
# ------------------------
|
| 85 |
+
elif algo == 2:
|
| 86 |
+
# Пропуск ключевого слова
|
| 87 |
+
question_sentence = random.choice(sentences)
|
| 88 |
+
words = question_sentence.split()
|
| 89 |
+
if len(words) > 2:
|
| 90 |
+
key_word = random.choice(words)
|
| 91 |
+
question = question_sentence.replace(key_word, "_____")
|
| 92 |
+
correct_answer = key_word
|
| 93 |
+
wrong_answer = random.choice([w for w in words if w != key_word] or ["другое"])
|
| 94 |
else:
|
| 95 |
+
# fallback
|
| 96 |
+
return generate_quiz(text)
|
| 97 |
+
|
| 98 |
+
# ------------------------
|
| 99 |
+
elif algo == 3:
|
| 100 |
+
# Вопрос о числе или дате
|
| 101 |
+
import re
|
| 102 |
+
question_sentence = random.choice(sentences)
|
| 103 |
+
numbers = re.findall(r'\d+', question_sentence)
|
| 104 |
+
if numbers:
|
| 105 |
+
number = random.choice(numbers)
|
| 106 |
+
question = question_sentence.replace(number, "_____")
|
| 107 |
+
correct_answer = number
|
| 108 |
+
wrong_answer = str(int(number)+random.randint(1,5))
|
| 109 |
+
else:
|
| 110 |
+
# fallback к базовому
|
| 111 |
+
return generate_quiz(text)
|
| 112 |
|
|
|
|
| 113 |
options = [correct_answer, wrong_answer]
|
| 114 |
random.shuffle(options)
|
|
|
|
| 115 |
return question, options, correct_answer
|
| 116 |
+
|
| 117 |
except Exception as e:
|
| 118 |
raise ValueError(f"Ошибка генерации вопроса: {str(e)}")
|
| 119 |
|
| 120 |
|
| 121 |
+
|
| 122 |
def synthesize_audio(text_ru: str):
|
| 123 |
"""Переводит русскую строку на казахский, синтезирует аудио и возвращает путь к файлу .wav"""
|
| 124 |
translation = translator(text_ru, src_lang="rus_Cyrl", tgt_lang="kaz_Cyrl")
|