Spaces:
Build error
Build error
File size: 6,763 Bytes
db9d335 8e44e35 db9d335 bbf823e db9d335 bbf823e f78381d b9b0a3a f7d34b8 bbf823e f7d34b8 bbf823e b9b0a3a f7d34b8 bbf823e f7d34b8 bbf823e b9b0a3a 8e44e35 b9b0a3a bbf823e 8e44e35 bbf823e 8e44e35 bbf823e db9d335 f7d34b8 8e44e35 bbf823e 8e44e35 bbf823e 8e44e35 bbf823e f7d34b8 bbf823e f7d34b8 bbf823e db9d335 8e44e35 bbf823e f7d34b8 8e44e35 bbf823e f7d34b8 bbf823e f7d34b8 bbf823e f7d34b8 bbf823e f7d34b8 b9b0a3a bbf823e db9d335 b9b0a3a db9d335 bbf823e f7d34b8 8e44e35 f7d34b8 bbf823e f7d34b8 bbf823e db9d335 b9b0a3a f7d34b8 db9d335 bbf823e 8e44e35 f7d34b8 bbf823e f7d34b8 bbf823e 8e44e35 b9b0a3a bbf823e f7d34b8 bbf823e f7d34b8 bbf823e b9b0a3a f7d34b8 b9b0a3a bbf823e f7d34b8 bbf823e b9b0a3a bbf823e b9b0a3a bbf823e f7d34b8 b9b0a3a bbf823e f7d34b8 b9b0a3a bbf823e b9b0a3a 8e44e35 bbf823e | 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 | import gradio as gr
import os
from PIL import Image
import tempfile
from gradio_client import Client, handle_file
import torch
from transformers import VitsModel, AutoTokenizer, pipeline
import scipy.io.wavfile as wavfile
import traceback
# =========================
# Загрузка моделей
# =========================
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
try:
# TTS модель (казахский)
tts_model = VitsModel.from_pretrained("facebook/mms-tts-kaz").to(device)
tts_tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-kaz")
# Перевод ru -> kk
translator = pipeline(
"translation",
model="facebook/nllb-200-distilled-600M",
device=0 if device == "cuda" else -1
)
print("✅ Все модели успешно загружены!")
except Exception as e:
raise RuntimeError(f"❌ Ошибка загрузки моделей: {str(e)}")
# =========================
# Talking Head API
# =========================
TALKING_HEAD_SPACE = "Skywork/skyreels-a1-talking-head"
# =========================
# Основная функция
# =========================
def inference(image: Image.Image, text: str):
error_msg = ""
video_path = None
audio_path = None
img_path = None
try:
# =========================
# Проверка входных данных
# =========================
if image is None:
raise ValueError("Загрузите изображение лектора!")
if not text or not text.strip():
raise ValueError("Введите текст лекции!")
if len(text) > 500:
raise ValueError("Текст превышает 500 символов!")
print("📥 Ввод (RU):", text)
# =========================
# Шаг 1 — Перевод
# =========================
translation = translator(
text,
src_lang="rus_Cyrl",
tgt_lang="kaz_Cyrl"
)
translated_text = translation[0]["translation_text"]
print("🌍 Перевод (KK):", translated_text)
if not translated_text.strip():
raise ValueError("Перевод не удался!")
# =========================
# Шаг 2 — Озвучка
# =========================
inputs = tts_tokenizer(translated_text, return_tensors="pt").to(device)
with torch.no_grad():
output = tts_model(**inputs)
waveform = output.waveform.squeeze().cpu().numpy()
if waveform.size == 0:
raise ValueError("TTS вернул пустое аудио!")
audio = (waveform * 32767).astype("int16")
sampling_rate = tts_model.config.sampling_rate
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
wavfile.write(f.name, sampling_rate, audio)
audio_path = f.name
print("🔊 Аудио создано:", audio_path)
# =========================
# Шаг 3 — Сохранение фото
# =========================
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
if image.mode != "RGB":
image = image.convert("RGB")
image.save(f.name)
img_path = f.name
print("🖼 Фото сохранено:", img_path)
# =========================
# Шаг 4 — Генерация видео
# =========================
print("🎥 Подключение к SkyReels...")
client = Client(TALKING_HEAD_SPACE)
result = client.predict(
image_path=handle_file(img_path),
audio_path=handle_file(audio_path),
guidance_scale=3.0,
steps=10,
api_name="/process_image_audio"
)
print("✅ RAW RESULT:", result)
# =========================
# Универсальный разбор результата
# =========================
if isinstance(result, tuple) and len(result) > 0:
video_data = result[0]
elif isinstance(result, dict):
video_data = result
else:
raise ValueError(f"Неизвестный формат ответа API: {type(result)}")
if isinstance(video_data, dict):
video_path = (
video_data.get("video")
or video_data.get("path")
or video_data.get("file")
)
elif isinstance(video_data, str):
video_path = video_data
else:
raise ValueError(f"Не удалось извлечь видео: {type(video_data)}")
if not video_path:
raise ValueError("API не вернул путь к видео!")
print("✅ Видео создано:", video_path)
error_msg = "✅ Видео успешно создано!"
except Exception as e:
error_msg = f"❌ Ошибка: {str(e)}"
print(error_msg)
traceback.print_exc()
finally:
# =========================
# Очистка временных файлов
# =========================
for p in [audio_path, img_path]:
if p and os.path.exists(p):
try:
os.remove(p)
print("🗑 Удалён файл:", p)
except:
pass
return video_path, error_msg
# =========================
# Интерфейс Gradio
# =========================
title = "🎓 Бейне Оқытушы"
description = """
Суретіңізді жүктеп, дәріс мәтінін **орыс тілінде** енгізіңіз.
Жүйе автоматты түрде қазақ тіліне аударады, озвучка жасайды және бейне шығарады!
**Талаптар:**
- Фото: бет анық көрінетін
- Мәтін: 500 таңбаға дейін
"""
iface = gr.Interface(
fn=inference,
inputs=[
gr.Image(type="pil", label="📸 Фото дәріскер"),
gr.Textbox(
lines=5,
label="📝 Дәріс мәтіні (орыс тілінде)",
placeholder="Мәтінді енгізіңіз..."
)
],
outputs=[
gr.Video(label="🎬 Дайын бейне"),
gr.Textbox(label="ℹ️ Мәртебе", interactive=False)
],
title=title,
description=description,
cache_examples=False,
flagging_mode="never"
)
if __name__ == "__main__":
iface.launch()
|