Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,13 +4,13 @@ import torch
|
|
4 |
import tempfile
|
5 |
import asyncio
|
6 |
import edge_tts
|
7 |
-
import
|
8 |
from threading import Thread
|
9 |
from collections.abc import Iterator
|
10 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
11 |
|
12 |
DESCRIPTION = """
|
13 |
-
# QwQ Tiny with Edge TTS
|
14 |
"""
|
15 |
|
16 |
MAX_MAX_NEW_TOKENS = 2048
|
@@ -29,14 +29,23 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
29 |
model.eval()
|
30 |
|
31 |
async def text_to_speech(text: str) -> str:
|
32 |
-
"""Converts text to speech using Edge TTS and returns the
|
|
|
|
|
|
|
33 |
communicate = edge_tts.Communicate(text)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
@spaces.GPU
|
40 |
def generate(
|
41 |
message: str,
|
42 |
chat_history: list[dict],
|
@@ -47,12 +56,12 @@ def generate(
|
|
47 |
repetition_penalty: float = 1.2,
|
48 |
) -> Iterator[str] | str:
|
49 |
|
50 |
-
is_tts = message.strip().startswith("@tts")
|
51 |
is_text_only = message.strip().startswith("@text")
|
52 |
|
53 |
# Remove special tags
|
54 |
if is_tts:
|
55 |
-
message = message.replace("@tts", "").strip()
|
56 |
elif is_text_only:
|
57 |
message = message.replace("@text", "").strip()
|
58 |
|
@@ -91,7 +100,7 @@ def generate(
|
|
91 |
loop = asyncio.new_event_loop()
|
92 |
asyncio.set_event_loop(loop)
|
93 |
audio_path = loop.run_until_complete(text_to_speech(final_output))
|
94 |
-
return audio_path # Returning
|
95 |
|
96 |
return final_output # Returning text output
|
97 |
|
@@ -107,8 +116,8 @@ demo = gr.ChatInterface(
|
|
107 |
stop_btn=None,
|
108 |
examples=[
|
109 |
["A train travels 60 kilometers per hour. If it travels for 5 hours, how far will it travel in total?"],
|
110 |
-
["@text What
|
111 |
-
["@tts Explain Newton's third law of motion."],
|
112 |
["@text Rewrite the following sentence in passive voice: 'The dog chased the cat.'"],
|
113 |
],
|
114 |
cache_examples=False,
|
|
|
4 |
import tempfile
|
5 |
import asyncio
|
6 |
import edge_tts
|
7 |
+
from pydub import AudioSegment
|
8 |
from threading import Thread
|
9 |
from collections.abc import Iterator
|
10 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
11 |
|
12 |
DESCRIPTION = """
|
13 |
+
# QwQ Tiny with Edge TTS (MP3 Output)
|
14 |
"""
|
15 |
|
16 |
MAX_MAX_NEW_TOKENS = 2048
|
|
|
29 |
model.eval()
|
30 |
|
31 |
async def text_to_speech(text: str) -> str:
|
32 |
+
"""Converts text to speech using Edge TTS, converts WAV to MP3, and returns the MP3 file path."""
|
33 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_wav:
|
34 |
+
wav_path = tmp_wav.name
|
35 |
+
|
36 |
communicate = edge_tts.Communicate(text)
|
37 |
+
await communicate.save(wav_path)
|
38 |
+
|
39 |
+
# Convert WAV to MP3
|
40 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_mp3:
|
41 |
+
mp3_path = tmp_mp3.name
|
42 |
+
|
43 |
+
audio = AudioSegment.from_wav(wav_path)
|
44 |
+
audio.export(mp3_path, format="mp3")
|
45 |
+
|
46 |
+
os.remove(wav_path) # Delete the original WAV file
|
47 |
+
return mp3_path # Return the MP3 file path
|
48 |
|
|
|
49 |
def generate(
|
50 |
message: str,
|
51 |
chat_history: list[dict],
|
|
|
56 |
repetition_penalty: float = 1.2,
|
57 |
) -> Iterator[str] | str:
|
58 |
|
59 |
+
is_tts = message.strip().startswith("edgetts@tts")
|
60 |
is_text_only = message.strip().startswith("@text")
|
61 |
|
62 |
# Remove special tags
|
63 |
if is_tts:
|
64 |
+
message = message.replace("edgetts@tts", "").strip()
|
65 |
elif is_text_only:
|
66 |
message = message.replace("@text", "").strip()
|
67 |
|
|
|
100 |
loop = asyncio.new_event_loop()
|
101 |
asyncio.set_event_loop(loop)
|
102 |
audio_path = loop.run_until_complete(text_to_speech(final_output))
|
103 |
+
return audio_path # Returning MP3 file path
|
104 |
|
105 |
return final_output # Returning text output
|
106 |
|
|
|
116 |
stop_btn=None,
|
117 |
examples=[
|
118 |
["A train travels 60 kilometers per hour. If it travels for 5 hours, how far will it travel in total?"],
|
119 |
+
["@text What is AI?"],
|
120 |
+
["edgetts@tts Explain Newton's third law of motion."],
|
121 |
["@text Rewrite the following sentence in passive voice: 'The dog chased the cat.'"],
|
122 |
],
|
123 |
cache_examples=False,
|