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 = """ Озвучка с музыкальной атмосферой

Озвучка с музыкальной атмосферой







""" @app.route('/get_voices', methods=['GET']) async def get_voices_route(): voices = await get_voices() return jsonify(voices) @app.route('/tts', methods=['POST']) async def tts_route(): data = request.get_json() text = data.get('text', '') voice = data.get('voice', '') rate = data.get('rate', 0) pitch = data.get('pitch', 0) audio, warning = await text_to_speech(text, voice, rate, pitch) if warning: return jsonify({'warning': warning}) else: return jsonify({'audio': audio}) @app.route('/') def index(): return render_template_string(HTML_TEMPLATE) @app.route('/music/') def serve_music(path): return app.send_static_file(f'music/{path}') if __name__ == "__main__": app.run(debug=True)