Nasma commited on
Commit
84b088a
·
verified ·
1 Parent(s): ed9e3e7

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +56 -0
main.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ from TTS.api import TTS
4
+ import os
5
+ from io import BytesIO
6
+ from typing import Generator
7
+
8
+ app = FastAPI()
9
+
10
+ # Initialize the TTS model
11
+ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False) # Set gpu=True if you have GPU support
12
+
13
+ # Function to split text into chunks
14
+ def split_text(text: str, words_per_chunk: int = 20):
15
+ words = text.split()
16
+ return [' '.join(words[i:i + words_per_chunk]) for i in range(0, len(words), words_per_chunk)]
17
+
18
+ # Function to generate audio chunks
19
+ def generate_audio_chunks(
20
+ text: str, speaker_wav: str, language: str, chunk_size: int = 20
21
+ ) -> Generator[bytes, None, None]:
22
+ if tts.is_multi_lingual and not language:
23
+ raise ValueError("Language must be specified for multi-lingual models.")
24
+
25
+ text_chunks = split_text(text, chunk_size)
26
+
27
+ for idx, chunk in enumerate(text_chunks):
28
+ # Generate audio for each chunk and yield as bytes
29
+ audio_buffer = BytesIO()
30
+ tts.tts_to_file(
31
+ text=chunk,
32
+ file_path=audio_buffer,
33
+ speaker_wav=speaker_wav,
34
+ language=language
35
+ )
36
+ audio_buffer.seek(0)
37
+ yield audio_buffer.read()
38
+
39
+ @app.post("/generate-audio/")
40
+ async def generate_audio(
41
+ text: str = Query(..., description="The input text to convert to speech."),
42
+ language: str = Query("en", description="Language code for TTS (e.g., 'en' for English)."),
43
+ speaker_wav: str = Query(..., description="Path to the WAV file for voice cloning.")
44
+ ):
45
+ if not os.path.exists(speaker_wav):
46
+ raise HTTPException(status_code=400, detail="Speaker WAV file not found.")
47
+
48
+ # StreamingResponse to stream audio chunks
49
+ def audio_stream():
50
+ try:
51
+ for audio_chunk in generate_audio_chunks(text=text, speaker_wav=speaker_wav, language=language):
52
+ yield audio_chunk
53
+ except Exception as e:
54
+ raise HTTPException(status_code=500, detail=str(e))
55
+
56
+ return StreamingResponse(audio_stream(), media_type="audio/wav")