Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
import tempfile
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
import random
|
| 6 |
-
import warnings
|
| 7 |
-
warnings.filterwarnings("ignore")
|
| 8 |
-
|
| 9 |
-
OUTPUT_DIR = Path("./output")
|
| 10 |
-
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 11 |
-
|
| 12 |
-
VOICE_STYLES = {
|
| 13 |
-
"1": "Professional Narrator",
|
| 14 |
-
"2": "YouTuber Style",
|
| 15 |
-
"3": "Teacher Style",
|
| 16 |
-
"4": "Documentary Voice"
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def load_model():
|
| 21 |
-
global model, tokenizer
|
| 22 |
-
|
| 23 |
-
if "model" in globals():
|
| 24 |
-
return model, tokenizer
|
| 25 |
-
|
| 26 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 27 |
-
|
| 28 |
-
model_name = "openbmb/MiniCPM-o-2_6"
|
| 29 |
-
|
| 30 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 31 |
-
model_name,
|
| 32 |
-
trust_remote_code=True
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
-
model_name,
|
| 37 |
-
trust_remote_code=True,
|
| 38 |
-
torch_dtype=torch.float32,
|
| 39 |
-
device_map="cpu"
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
model.eval()
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
final_prompt,
|
| 61 |
-
return_tensors="pt",
|
| 62 |
-
truncation=True,
|
| 63 |
-
max_length=1024
|
| 64 |
-
)
|
| 65 |
|
| 66 |
-
with torch.no_grad():
|
| 67 |
-
output = model.generate(
|
| 68 |
-
**inputs,
|
| 69 |
-
max_new_tokens=200,
|
| 70 |
-
temperature=0.7,
|
| 71 |
-
top_p=0.9
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
text = tokenizer.decode(
|
| 75 |
-
output[0][inputs.input_ids.shape[1]:],
|
| 76 |
-
skip_special_tokens=True
|
| 77 |
-
)
|
| 78 |
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
gr.Markdown("# ποΈ
|
| 85 |
|
| 86 |
-
|
| 87 |
-
label="Enter
|
| 88 |
-
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
choices=list(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
)
|
| 96 |
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
output_text = gr.Textbox(label="Generated Script")
|
| 101 |
-
status = gr.Textbox(label="Status")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
inputs=[
|
| 106 |
-
outputs=
|
| 107 |
)
|
| 108 |
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import edge_tts
|
| 3 |
+
import asyncio
|
| 4 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Voice options
|
| 7 |
+
VOICES = {
|
| 8 |
+
"π© Professional Female": "en-US-JennyNeural",
|
| 9 |
+
"π¨ Energetic Male": "en-US-GuyNeural",
|
| 10 |
+
"π¬π§ British Female": "en-GB-SoniaNeural",
|
| 11 |
+
"π¬π§ British Male": "en-GB-RyanNeural",
|
| 12 |
+
"π¬ Deep Documentary": "en-US-ChristopherNeural",
|
| 13 |
+
"π Friendly Female": "en-US-AnaNeural",
|
| 14 |
+
"π€ Energetic Announcer": "en-US-DavisNeural",
|
| 15 |
+
"π° News Anchor": "en-US-SaraNeural",
|
| 16 |
+
}
|
| 17 |
|
| 18 |
|
| 19 |
+
async def generate_speech(text, voice_name):
|
| 20 |
+
"""Generate speech using Edge TTS"""
|
| 21 |
+
if not text or not text.strip():
|
| 22 |
+
return None
|
| 23 |
|
| 24 |
+
voice_id = VOICES[voice_name]
|
| 25 |
|
| 26 |
+
output_file = tempfile.NamedTemporaryFile(
|
| 27 |
+
delete=False,
|
| 28 |
+
suffix=".mp3"
|
| 29 |
+
).name
|
| 30 |
|
| 31 |
+
communicate = edge_tts.Communicate(text, voice_id)
|
| 32 |
+
await communicate.save(output_file)
|
| 33 |
|
| 34 |
+
return output_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
def tts_wrapper(text, voice):
|
| 38 |
+
return asyncio.run(generate_speech(text, voice))
|
| 39 |
|
| 40 |
|
| 41 |
+
# UI
|
| 42 |
+
with gr.Blocks(title="Simple TTS", theme=gr.themes.Soft()) as demo:
|
| 43 |
|
| 44 |
+
gr.Markdown("# ποΈ Simple Text-to-Speech Generator")
|
| 45 |
|
| 46 |
+
text_input = gr.Textbox(
|
| 47 |
+
label="Enter your text",
|
| 48 |
+
placeholder="Type something here...",
|
| 49 |
+
lines=3
|
| 50 |
)
|
| 51 |
|
| 52 |
+
voice_select = gr.Dropdown(
|
| 53 |
+
choices=list(VOICES.keys()),
|
| 54 |
+
label="Select Voice",
|
| 55 |
+
value="π© Professional Female"
|
| 56 |
)
|
| 57 |
|
| 58 |
+
generate_btn = gr.Button("π Generate Speech", variant="primary")
|
| 59 |
|
| 60 |
+
audio_output = gr.Audio(label="Audio Output")
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
generate_btn.click(
|
| 63 |
+
fn=tts_wrapper,
|
| 64 |
+
inputs=[text_input, voice_select],
|
| 65 |
+
outputs=audio_output
|
| 66 |
)
|
| 67 |
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch(
|
| 71 |
+
server_name="0.0.0.0",
|
| 72 |
+
server_port=7860
|
| 73 |
+
)
|