Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,23 +2,32 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import soundfile as sf
|
| 4 |
import tempfile
|
|
|
|
| 5 |
|
| 6 |
-
from transformers import
|
| 7 |
from kokoro import KPipeline
|
| 8 |
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
tts = KPipeline(lang_code="a")
|
| 19 |
|
| 20 |
|
| 21 |
def generate_answer_and_voice(user_question):
|
|
|
|
|
|
|
|
|
|
| 22 |
messages = [
|
| 23 |
{
|
| 24 |
"role": "system",
|
|
@@ -30,15 +39,35 @@ def generate_answer_and_voice(user_question):
|
|
| 30 |
}
|
| 31 |
]
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
messages,
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
temperature=0.7,
|
| 38 |
-
top_p=0.9
|
| 39 |
)
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
audio_parts = []
|
| 44 |
|
|
@@ -48,9 +77,11 @@ def generate_answer_and_voice(user_question):
|
|
| 48 |
):
|
| 49 |
audio_parts.append(audio)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
final_audio = np.concatenate(audio_parts)
|
| 52 |
|
| 53 |
-
# Create temporary WAV file for Gradio output
|
| 54 |
output_file = tempfile.NamedTemporaryFile(
|
| 55 |
delete=False,
|
| 56 |
suffix=".wav"
|
|
@@ -72,7 +103,7 @@ demo = gr.Interface(
|
|
| 72 |
gr.Audio(label="Generated Voice", type="filepath")
|
| 73 |
],
|
| 74 |
title="AI Text Generation + Text-to-Speech",
|
| 75 |
-
description="Ask
|
| 76 |
)
|
| 77 |
|
| 78 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import soundfile as sf
|
| 4 |
import tempfile
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 8 |
from kokoro import KPipeline
|
| 9 |
|
| 10 |
|
| 11 |
+
MODEL_NAME = "HuggingFaceTB/SmolLM2-360M-Instruct"
|
| 12 |
+
|
| 13 |
+
# Download and load model inside the Space
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
+
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
MODEL_NAME,
|
| 18 |
+
torch_dtype=torch.float32
|
| 19 |
)
|
| 20 |
|
| 21 |
+
model.eval()
|
| 22 |
+
|
| 23 |
+
# Load text-to-speech model
|
| 24 |
tts = KPipeline(lang_code="a")
|
| 25 |
|
| 26 |
|
| 27 |
def generate_answer_and_voice(user_question):
|
| 28 |
+
if not user_question or not user_question.strip():
|
| 29 |
+
return "Please enter a question.", None
|
| 30 |
+
|
| 31 |
messages = [
|
| 32 |
{
|
| 33 |
"role": "system",
|
|
|
|
| 39 |
}
|
| 40 |
]
|
| 41 |
|
| 42 |
+
# Convert chat messages into model prompt
|
| 43 |
+
prompt = tokenizer.apply_chat_template(
|
| 44 |
messages,
|
| 45 |
+
tokenize=False,
|
| 46 |
+
add_generation_prompt=True
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
inputs = tokenizer(
|
| 50 |
+
prompt,
|
| 51 |
+
return_tensors="pt"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
output_ids = model.generate(
|
| 56 |
+
**inputs,
|
| 57 |
+
max_new_tokens=120,
|
| 58 |
+
do_sample=True,
|
| 59 |
+
temperature=0.7,
|
| 60 |
+
top_p=0.9,
|
| 61 |
+
pad_token_id=tokenizer.eos_token_id
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Keep only newly generated tokens
|
| 65 |
+
new_tokens = output_ids[0][inputs["input_ids"].shape[1]:]
|
| 66 |
+
|
| 67 |
+
generated_response = tokenizer.decode(
|
| 68 |
+
new_tokens,
|
| 69 |
+
skip_special_tokens=True
|
| 70 |
+
).strip()
|
| 71 |
|
| 72 |
audio_parts = []
|
| 73 |
|
|
|
|
| 77 |
):
|
| 78 |
audio_parts.append(audio)
|
| 79 |
|
| 80 |
+
if not audio_parts:
|
| 81 |
+
return generated_response, None
|
| 82 |
+
|
| 83 |
final_audio = np.concatenate(audio_parts)
|
| 84 |
|
|
|
|
| 85 |
output_file = tempfile.NamedTemporaryFile(
|
| 86 |
delete=False,
|
| 87 |
suffix=".wav"
|
|
|
|
| 103 |
gr.Audio(label="Generated Voice", type="filepath")
|
| 104 |
],
|
| 105 |
title="AI Text Generation + Text-to-Speech",
|
| 106 |
+
description="Ask a question. SmolLM generates an answer and Kokoro converts it into speech."
|
| 107 |
)
|
| 108 |
|
| 109 |
demo.launch()
|