Spaces:
Sleeping
Sleeping
File size: 26,466 Bytes
29be5ba df8ede2 29be5ba df8ede2 29be5ba df8ede2 29be5ba df8ede2 29be5ba df8ede2 29be5ba 30093d4 df8ede2 29be5ba df8ede2 | 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 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | import gradio as gr
import tempfile
import os
import json
import shutil
import base64
try:
from moviepy.editor import VideoFileClip
MOVIEPY_AVAILABLE = True
except ImportError:
print("MoviePy не установлен, используем альтернативный метод")
MOVIEPY_AVAILABLE = False
# Глобальная переменная для хранения созданных уроков
current_lesson_dir = None
def add_segment(lecture_video, correct_video, wrong_video, option1_text, option2_text, correct_option, segments_state):
"""Добавляет сегмент: видео лекции + видео реакций + текст вариантов"""
if lecture_video is None or correct_video is None or wrong_video is None:
return segments_state, "⚠️ Загрузите все три видео (лекция, правильная реакция, неправильная реакция)", None, None, None
if not option1_text or not option2_text:
return segments_state, "⚠️ Введите оба варианта ответа", None, None, None
if correct_option is None:
return segments_state, "⚠️ Выберите правильный вариант", None, None, None
# Получаем длительность лекции
try:
if MOVIEPY_AVAILABLE:
clip = VideoFileClip(lecture_video)
duration = clip.duration
clip.close()
else:
# Альтернативный метод через subprocess
import subprocess
result = subprocess.run(
['ffprobe', '-v', 'error', '-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1', lecture_video],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
duration = float(result.stdout)
except Exception as e:
print(f"Ошибка получения длительности: {e}")
duration = 10 # Значение по умолчанию
# Сохраняем оригинальные пути
new_segment = {
'lecture_path': lecture_video,
'correct_path': correct_video,
'wrong_path': wrong_video,
'options': [option1_text, option2_text],
'correct_answer': 0 if correct_option == "Вариант 1" else 1, # Переименовали ключ
'duration': duration
}
segments_state.append(new_segment)
info = f"✅ **Всего сегментов: {len(segments_state)}**\n\n"
for i, seg in enumerate(segments_state, 1):
info += f"**Сегмент {i}:** ({seg['duration']:.1f} сек)\n"
info += f"- 🎯 Вариант 1: {seg['options'][0]}\n"
info += f"- 🎯 Вариант 2: {seg['options'][1]}\n"
info += f"- ✅ Правильный: Вариант {seg['correct_answer'] + 1}\n\n"
# Очищаем поля ввода
return segments_state, info, None, None, None
def create_lesson(segments_state):
"""Подготавливает урок - копирует файлы и создает метаданные"""
global current_lesson_dir
if not segments_state:
return "⚠️ Нет добавленных сегментов", None
# Создаем временную директорию для урока
lesson_dir = tempfile.mkdtemp(prefix="lesson_")
current_lesson_dir = lesson_dir # Сохраняем глобально
timestamps = []
for i, seg in enumerate(segments_state):
# Копируем видео в директорию урока
lecture_new = os.path.join(lesson_dir, f"lecture_{i}.mp4")
correct_new = os.path.join(lesson_dir, f"correct_{i}.mp4")
wrong_new = os.path.join(lesson_dir, f"wrong_{i}.mp4")
# Копируем файлы
shutil.copy2(seg['lecture_path'], lecture_new)
shutil.copy2(seg['correct_path'], correct_new)
shutil.copy2(seg['wrong_path'], wrong_new)
timestamps.append({
'index': i,
'lecture_file': f"lecture_{i}.mp4", # Переименовали ключи для ясности
'correct_file': f"correct_{i}.mp4",
'wrong_file': f"wrong_{i}.mp4",
'duration': seg['duration'],
'options': seg['options'],
'correct_answer': seg['correct_answer'] # Здесь храним индекс правильного ответа
})
# Сохраняем метаданные
metadata = {
'timestamps': timestamps,
'total_segments': len(segments_state),
'lesson_dir': lesson_dir # Сохраняем путь к директории
}
meta_path = os.path.join(lesson_dir, 'metadata.json')
with open(meta_path, 'w', encoding='utf-8') as f:
json.dump(metadata, f, ensure_ascii=False, indent=2)
total_duration = sum(seg['duration'] for seg in segments_state)
return (f"✅ Урок создан!\n- Всего сегментов: {len(segments_state)}\n- Примерная длительность: {total_duration:.1f} сек\n- Путь: {lesson_dir}",
meta_path)
def generate_player_html(meta_path):
"""Генерирует HTML плеер с интерактивными кнопками"""
global current_lesson_dir
if not meta_path or not os.path.exists(meta_path):
return "<p style='color: red; text-align: center; padding: 40px;'>⚠️ Сначала создайте урок!</p>"
with open(meta_path, 'r', encoding='utf-8') as f:
metadata = json.load(f)
# Используем сохраненную директорию
lesson_dir = metadata.get('lesson_dir', current_lesson_dir)
if not lesson_dir or not os.path.exists(lesson_dir):
return "<p style='color: red; text-align: center; padding: 40px;'>⚠️ Файлы урока не найдены. Создайте урок заново.</p>"
# Конвертируем все видео в base64
video_data = {}
for segment in metadata['timestamps']:
idx = segment['index']
# Используем правильные ключи для имен файлов
lecture_path = os.path.join(lesson_dir, segment['lecture_file'])
correct_path = os.path.join(lesson_dir, segment['correct_file'])
wrong_path = os.path.join(lesson_dir, segment['wrong_file'])
try:
with open(lecture_path, 'rb') as f:
video_data[f'lecture_{idx}'] = base64.b64encode(f.read()).decode()
with open(correct_path, 'rb') as f:
video_data[f'correct_{idx}'] = base64.b64encode(f.read()).decode()
with open(wrong_path, 'rb') as f:
video_data[f'wrong_{idx}'] = base64.b64encode(f.read()).decode()
except Exception as e:
print(f"Ошибка чтения файла: {e}")
return f"<p style='color: red; text-align: center; padding: 40px;'>⚠️ Ошибка загрузки видео: {str(e)}</p>"
# Создаем структуру данных для JavaScript
segments_json = json.dumps([{
'index': seg['index'],
'duration': seg['duration'],
'options': seg['options'],
'correct': seg['correct_answer'] # Используем правильный ключ
} for seg in metadata['timestamps']], ensure_ascii=False)
# Создаем словарь с видео для JavaScript
videos_json = json.dumps(video_data)
html = f"""
<style>
* {{
margin: 0;
padding: 0;
box-sizing: border-box;
}}
.player-wrapper {{
width: 100%;
max-width: 900px;
margin: 20px auto;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}}
.player-container {{
position: relative;
width: 100%;
background: #000;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}}
#mainVideo {{
width: 100%;
height: auto;
display: block;
min-height: 400px;
}}
.quiz-overlay {{
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: linear-gradient(to top, rgba(0,0,0,0.95) 0%, rgba(0,0,0,0.8) 50%, transparent 100%);
padding: 40px 30px 30px;
display: none;
}}
.quiz-question {{
color: #ffffff;
font-size: 20px;
font-weight: 600;
text-align: center;
margin-bottom: 20px;
text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}}
.quiz-options {{
display: flex;
gap: 15px;
flex-direction: column;
max-width: 600px;
margin: 0 auto;
}}
.option-btn {{
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 18px 30px;
border-radius: 12px;
font-size: 17px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}}
.option-btn:hover {{
transform: translateY(-3px);
box-shadow: 0 6px 25px rgba(102, 126, 234, 0.6);
}}
.option-btn:active {{
transform: translateY(-1px);
}}
.timer {{
color: #ffd700;
font-size: 15px;
font-weight: 600;
text-align: center;
margin-top: 15px;
text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}}
.timer.warning {{
color: #ff6b6b;
}}
.progress-bar {{
position: absolute;
top: 0;
left: 0;
height: 4px;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
width: 0%;
transition: width 0.1s linear;
z-index: 10;
}}
.segment-info {{
position: absolute;
top: 15px;
right: 15px;
background: rgba(0,0,0,0.7);
color: white;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
font-weight: 500;
z-index: 10;
}}
.loading {{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 18px;
display: none;
z-index: 20;
}}
.controls {{
padding: 15px;
background: #1a1a1a;
display: flex;
align-items: center;
gap: 15px;
}}
.play-pause-btn {{
background: #667eea;
border: none;
color: white;
width: 40px;
height: 40px;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s;
font-size: 16px;
}}
.play-pause-btn:hover {{
background: #764ba2;
transform: scale(1.1);
}}
.time-display {{
color: #ffffff;
font-size: 14px;
font-weight: 500;
}}
</style>
<div class="player-wrapper">
<div class="player-container">
<div class="progress-bar" id="progressBar"></div>
<div class="segment-info" id="segmentInfo">Сегмент 1 из {metadata['total_segments']}</div>
<div class="loading" id="loading">Загрузка...</div>
<video id="mainVideo" preload="auto">
Ваш браузер не поддерживает видео
</video>
<div class="quiz-overlay" id="quizOverlay">
<div class="quiz-question">Выберите правильный ответ:</div>
<div class="quiz-options">
<button class="option-btn" id="option1"></button>
<button class="option-btn" id="option2"></button>
</div>
<div class="timer" id="timer">⏱ Времени осталось: <span id="timeLeft">7</span> сек</div>
</div>
</div>
<div class="controls">
<button class="play-pause-btn" id="playPauseBtn">▶</button>
<div class="time-display" id="timeDisplay">0:00</div>
</div>
</div>
<script>
(function() {{
const mainVideo = document.getElementById('mainVideo');
const quizOverlay = document.getElementById('quizOverlay');
const option1Btn = document.getElementById('option1');
const option2Btn = document.getElementById('option2');
const timerDisplay = document.getElementById('timeLeft');
const progressBar = document.getElementById('progressBar');
const segmentInfo = document.getElementById('segmentInfo');
const loadingDiv = document.getElementById('loading');
const playPauseBtn = document.getElementById('playPauseBtn');
const timeDisplay = document.getElementById('timeDisplay');
const timerElement = document.getElementById('timer');
const segments = {segments_json};
const videosBase64 = {videos_json};
let currentSegmentIndex = 0;
let isPlayingReaction = false;
let quizShown = false;
let answered = false;
let timerInterval = null;
let timeRemaining = 7;
function formatTime(seconds) {{
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return mins + ':' + (secs < 10 ? '0' : '') + secs;
}}
function updateSegmentInfo() {{
segmentInfo.textContent = 'Сегмент ' + (currentSegmentIndex + 1) + ' из ' + segments.length;
}}
function showLoading() {{
loadingDiv.style.display = 'block';
}}
function hideLoading() {{
loadingDiv.style.display = 'none';
}}
function loadVideo(base64Data) {{
return new Promise(function(resolve, reject) {{
showLoading();
mainVideo.src = 'data:video/mp4;base64,' + base64Data;
mainVideo.load();
mainVideo.onloadeddata = function() {{
hideLoading();
resolve();
}};
mainVideo.onerror = function() {{
hideLoading();
reject(new Error('Ошибка загрузки видео'));
}};
}});
}}
function showQuiz(segment) {{
quizOverlay.style.display = 'block';
option1Btn.textContent = segment.options[0];
option2Btn.textContent = segment.options[1];
quizShown = true;
answered = false;
timeRemaining = 7;
timerDisplay.textContent = timeRemaining;
if (timerInterval) {{
clearInterval(timerInterval);
}}
timerInterval = setInterval(function() {{
timeRemaining--;
timerDisplay.textContent = timeRemaining;
if (timeRemaining <= 3) {{
timerElement.classList.add('warning');
}} else {{
timerElement.classList.remove('warning');
}}
if (timeRemaining <= 0) {{
clearInterval(timerInterval);
if (!answered) {{
handleAnswer(-1, segment);
}}
}}
}}, 1000);
}}
function hideQuiz() {{
quizOverlay.style.display = 'none';
quizShown = false;
timerElement.classList.remove('warning');
if (timerInterval) {{
clearInterval(timerInterval);
timerInterval = null;
}}
}}
function playReaction(isCorrect) {{
return new Promise(function(resolve) {{
isPlayingReaction = true;
hideQuiz();
const segment = segments[currentSegmentIndex];
const reactionKey = isCorrect ? ('correct_' + segment.index) : ('wrong_' + segment.index);
loadVideo(videosBase64[reactionKey]).then(function() {{
mainVideo.play();
mainVideo.onended = function() {{
isPlayingReaction = false;
mainVideo.onended = null;
resolve();
}};
}}).catch(function(error) {{
console.error('Ошибка воспроизведения реакции:', error);
isPlayingReaction = false;
resolve();
}});
}});
}}
function loadNextSegment() {{
if (currentSegmentIndex >= segments.length) {{
alert('🎉 Поздравляем! Вы завершили урок!');
currentSegmentIndex = 0;
return;
}}
const segment = segments[currentSegmentIndex];
const lectureKey = 'lecture_' + segment.index;
loadVideo(videosBase64[lectureKey]).then(function() {{
updateSegmentInfo();
mainVideo.play();
playPauseBtn.textContent = '⏸';
}}).catch(function(error) {{
console.error('Ошибка загрузки лекции:', error);
alert('Ошибка загрузки видео');
}});
}}
function handleAnswer(choice, segment) {{
if (answered) return;
answered = true;
const isCorrect = choice === segment.correct;
playReaction(isCorrect).then(function() {{
currentSegmentIndex++;
if (currentSegmentIndex < segments.length) {{
loadNextSegment();
}} else {{
alert('🎉 Поздравляем! Вы завершили урок!');
}}
}});
}}
option1Btn.addEventListener('click', function() {{
if (currentSegmentIndex >= 0 && currentSegmentIndex < segments.length && !answered) {{
handleAnswer(0, segments[currentSegmentIndex]);
}}
}});
option2Btn.addEventListener('click', function() {{
if (currentSegmentIndex >= 0 && currentSegmentIndex < segments.length && !answered) {{
handleAnswer(1, segments[currentSegmentIndex]);
}}
}});
mainVideo.addEventListener('timeupdate', function() {{
if (isPlayingReaction) return;
const currentTime = mainVideo.currentTime;
const duration = mainVideo.duration;
if (!isNaN(currentTime)) {{
timeDisplay.textContent = formatTime(currentTime);
}}
if (duration > 0 && !isNaN(duration)) {{
const progress = (currentTime / duration) * 100;
progressBar.style.width = progress + '%';
const timeLeft = duration - currentTime;
if (!quizShown && timeLeft <= 7 && timeLeft > 0 && !answered) {{
showQuiz(segments[currentSegmentIndex]);
}}
}}
}});
playPauseBtn.addEventListener('click', function() {{
if (mainVideo.paused) {{
mainVideo.play();
playPauseBtn.textContent = '⏸';
}} else {{
mainVideo.pause();
playPauseBtn.textContent = '▶';
}}
}});
mainVideo.addEventListener('play', function() {{
playPauseBtn.textContent = '⏸';
}});
mainVideo.addEventListener('pause', function() {{
playPauseBtn.textContent = '▶';
}});
// Запускаем первый сегмент
loadNextSegment();
}})();
</script>
"""
return html
# Создаем интерфейс
with gr.Blocks(title="Интерактивный Урок") as demo:
gr.Markdown("""
# 🎓 Интерактивный Урок (Coursera-style)
### Инструкция:
1. **Загрузите 3 видео для каждого сегмента:**
- 🎥 Видео лекции (в конце которого задается вопрос)
- ✅ Видео реакции на правильный ответ
- ❌ Видео реакции на неправильный ответ
2. **Введите варианты ответов** и выберите правильный
3. Нажмите **"Добавить Сегмент"** (повторите для каждого фрагмента)
4. Когда все сегменты добавлены, нажмите **"Создать Урок"**
5. Нажмите **"Запустить Плеер"** для прохождения урока
""")
# Состояния
segments_state = gr.State([])
meta_state = gr.State(None)
with gr.Tab("📝 Создание Урока"):
gr.Markdown("### Добавление сегмента")
with gr.Row():
with gr.Column(scale=1):
lecture_input = gr.Video(label="🎥 Видео Лекции")
correct_input = gr.Video(label="✅ Реакция: Правильный ответ")
wrong_input = gr.Video(label="❌ Реакция: Неправильный ответ")
with gr.Column(scale=1):
option1_input = gr.Textbox(
label="Вариант 1",
placeholder="Например: Париж"
)
option2_input = gr.Textbox(
label="Вариант 2",
placeholder="Например: Лондон"
)
correct_radio = gr.Radio(
["Вариант 1", "Вариант 2"],
label="✅ Правильный вариант",
value="Вариант 1"
)
add_btn = gr.Button("➕ Добавить Сегмент", variant="primary")
segments_info = gr.Markdown("*Нет добавленных сегментов*")
add_btn.click(
fn=add_segment,
inputs=[lecture_input, correct_input, wrong_input, option1_input, option2_input, correct_radio, segments_state],
outputs=[segments_state, segments_info, lecture_input, correct_input, wrong_input]
)
gr.Markdown("---")
create_btn = gr.Button("🎬 Создать Урок", variant="primary")
create_status = gr.Markdown("")
create_btn.click(
fn=create_lesson,
inputs=[segments_state],
outputs=[create_status, meta_state]
)
with gr.Tab("▶️ Прохождение Урока"):
gr.Markdown("""
### Как проходить урок:
- 🎬 Видео проигрывается автоматически
- ⏱ **За 7 секунд** до конца появятся кнопки с вариантами ответа
- 🎯 Выберите правильный ответ до окончания времени
- ✅ После ответа покажется видео реакции лектора
- ➡️ Затем автоматически начнется следующий сегмент
- 🏆 Пройдите все сегменты до конца!
""")
start_btn = gr.Button("🚀 Запустить Плеер", variant="primary")
player_html = gr.HTML()
start_btn.click(
fn=generate_player_html,
inputs=[meta_state],
outputs=[player_html]
)
if __name__ == '__main__':
demo.launch() |