import gradio as gr import edge_tts import asyncio import tempfile import re import emoji from flask import Flask, request, jsonify, render_template_string app = Flask(__name__) # Функция для очистки текста от нежелательных символов и эмодзи def clean_text(text): # Удаление указанных символов text = re.sub(r'[*_~><]', '', text) # Удаление эмодзи text = emoji.replace_emoji(text, replace='') return text # Get all available voices async def get_voices(): voices = await edge_tts.list_voices() return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices} # Text-to-speech function async def text_to_speech(text, voice, rate, pitch): if not text.strip(): return None, "Пожалуйста, введите текст для озвучки." if not voice: return None, "Пожалуйста, выберите голос." # Очистка текста text = clean_text(text) voice_short_name = voice.split(" - ")[0] rate_str = f"{rate:+d}%" pitch_str = f"{pitch:+d}Hz" communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str) with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: tmp_path = tmp_file.name try: await communicate.save(tmp_path) except Exception as e: return None, f"Произошла ошибка при конвертации текста в речь: {str(e)}" return tmp_path, None # HTML шаблон HTML_TEMPLATE = """