hivecorp commited on
Commit
996d0ad
·
verified ·
1 Parent(s): dd6cda6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import uvicorn
4
+ from fastapi import FastAPI
5
+ from pydub import AudioSegment
6
+ from uuid import uuid4
7
+
8
+ app = FastAPI()
9
+
10
+ # Your first Hugging Face TTS API URL
11
+ TTS_API_URL = "https://hivecorp-s8test.hf.space/tts"
12
+ MAX_LENGTH = 250 # Max characters per chunk
13
+
14
+
15
+ def split_text(text, max_length=MAX_LENGTH):
16
+ """Splits text into smaller chunks at sentence boundaries."""
17
+ chunks = []
18
+ current_chunk = ""
19
+ sentences = text.split(". ")
20
+
21
+ for sentence in sentences:
22
+ if len(current_chunk + sentence) > max_length:
23
+ chunks.append(current_chunk.strip())
24
+ current_chunk = ""
25
+ current_chunk += sentence + ". "
26
+
27
+ if current_chunk:
28
+ chunks.append(current_chunk.strip())
29
+
30
+ return chunks
31
+
32
+
33
+ def call_tts_api(text, voice):
34
+ """Calls the TTS API with the text & voice and returns the generated MP3 file."""
35
+ response = requests.post(TTS_API_URL, json={"text": text, "voice": voice})
36
+ if response.status_code == 200:
37
+ filename = f"audio_{uuid4().hex}.mp3"
38
+ with open(filename, "wb") as f:
39
+ f.write(response.content)
40
+ return filename
41
+ return None
42
+
43
+
44
+ def merge_audio(files):
45
+ """Merges multiple MP3 files into one using pydub."""
46
+ final_audio = AudioSegment.empty()
47
+
48
+ for file in files:
49
+ audio = AudioSegment.from_file(file, format="mp3")
50
+ final_audio += audio
51
+
52
+ final_filename = "final_audio.mp3"
53
+ final_audio.export(final_filename, format="mp3")
54
+
55
+ # Cleanup temp files
56
+ for file in files:
57
+ os.remove(file)
58
+
59
+ return final_filename
60
+
61
+
62
+ @app.post("/generate-audio")
63
+ async def generate_audio(text: str, voice: str):
64
+ """Processes text, splits it, calls TTS API, and merges MP3s."""
65
+ text_chunks = split_text(text)
66
+ audio_files = [call_tts_api(chunk, voice) for chunk in text_chunks]
67
+
68
+ if None in audio_files:
69
+ return {"error": "TTS API failed for some parts"}
70
+
71
+ merged_file = merge_audio(audio_files)
72
+ return {"audio_url": f"/download/{merged_file}"}
73
+
74
+
75
+ @app.get("/download/{filename}")
76
+ async def download_file(filename: str):
77
+ """Endpoint to download the merged MP3 file."""
78
+ return {"file": filename}
79
+
80
+
81
+ # Ensure the app starts when running in Hugging Face Spaces
82
+ if __name__ == "__main__":
83
+ uvicorn.run(app, host="0.0.0.0", port=7860)