Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,14 @@
|
|
| 1 |
-
import os
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
-
import spaces
|
| 6 |
import edge_tts
|
| 7 |
from fastapi import FastAPI, Request
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
-
from fastapi.responses import
|
| 10 |
-
|
| 11 |
-
# ===== 1. ZEROGPU CPU/RAM MAXIMIZER =====
|
| 12 |
-
@spaces.GPU(duration=5)
|
| 13 |
-
def _hold_zerogpu():
|
| 14 |
-
print("[zerogpu] claimed host; GPU slice released, CPU+RAM stay ours.")
|
| 15 |
-
return True
|
| 16 |
-
|
| 17 |
-
_hold_zerogpu()
|
| 18 |
|
|
|
|
| 19 |
def effective_cpus() -> int:
|
| 20 |
try:
|
| 21 |
quota, period = Path("/sys/fs/cgroup/cpu.max").read_text().split()[:2]
|
|
@@ -38,16 +31,17 @@ def memory_limit_gb():
|
|
| 38 |
return None
|
| 39 |
|
| 40 |
CORES = effective_cpus()
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
# ===== 2. FASTAPI SERVER & CORS SETUP =====
|
| 44 |
app = FastAPI(title="Edge-TTS-Server")
|
| 45 |
app.add_middleware(
|
| 46 |
-
CORSMiddleware,
|
| 47 |
-
allow_origins=["*"],
|
| 48 |
allow_credentials=True,
|
| 49 |
-
allow_methods=["*"],
|
| 50 |
-
allow_headers=["*"]
|
| 51 |
)
|
| 52 |
|
| 53 |
# ===== 3. PARAMETER NORMALIZERS =====
|
|
@@ -178,7 +172,7 @@ DEFAULT_VOICES = [
|
|
| 178 |
with gr.Blocks(title="High-Speed Edge-TTS API") as demo:
|
| 179 |
gr.Markdown(
|
| 180 |
f"# ⚡ High-Speed Edge-TTS Server\n"
|
| 181 |
-
f"Running
|
| 182 |
f"**External API Endpoints:**\n"
|
| 183 |
f"- `GET / POST /tts?text=...&voice=...&speed=1.0&pitch=+0Hz`\n"
|
| 184 |
f"- `POST /v1/audio/speech` (OpenAI Compatible)\n"
|
|
@@ -196,9 +190,10 @@ with gr.Blocks(title="High-Speed Edge-TTS API") as demo:
|
|
| 196 |
|
| 197 |
btn.click(fn=gradio_tts, inputs=[text_input, voice_dropdown, speed_slider, pitch_input], outputs=audio_output)
|
| 198 |
|
| 199 |
-
|
| 200 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
import uvicorn
|
| 204 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
import edge_tts
|
| 7 |
from fastapi import FastAPI, Request
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# ===== 1. CPU / SYSTEM RESOURCE DETECTOR =====
|
| 12 |
def effective_cpus() -> int:
|
| 13 |
try:
|
| 14 |
quota, period = Path("/sys/fs/cgroup/cpu.max").read_text().split()[:2]
|
|
|
|
| 31 |
return None
|
| 32 |
|
| 33 |
CORES = effective_cpus()
|
| 34 |
+
RAM = memory_limit_gb() or "?"
|
| 35 |
+
print(f"[resources] Effective cores: {CORES} | RAM limit: {RAM} GB")
|
| 36 |
|
| 37 |
# ===== 2. FASTAPI SERVER & CORS SETUP =====
|
| 38 |
app = FastAPI(title="Edge-TTS-Server")
|
| 39 |
app.add_middleware(
|
| 40 |
+
CORSMiddleware,
|
| 41 |
+
allow_origins=["*"],
|
| 42 |
allow_credentials=True,
|
| 43 |
+
allow_methods=["*"],
|
| 44 |
+
allow_headers=["*"],
|
| 45 |
)
|
| 46 |
|
| 47 |
# ===== 3. PARAMETER NORMALIZERS =====
|
|
|
|
| 172 |
with gr.Blocks(title="High-Speed Edge-TTS API") as demo:
|
| 173 |
gr.Markdown(
|
| 174 |
f"# ⚡ High-Speed Edge-TTS Server\n"
|
| 175 |
+
f"Running with {CORES} CPU Cores Allocated\n\n"
|
| 176 |
f"**External API Endpoints:**\n"
|
| 177 |
f"- `GET / POST /tts?text=...&voice=...&speed=1.0&pitch=+0Hz`\n"
|
| 178 |
f"- `POST /v1/audio/speech` (OpenAI Compatible)\n"
|
|
|
|
| 190 |
|
| 191 |
btn.click(fn=gradio_tts, inputs=[text_input, voice_dropdown, speed_slider, pitch_input], outputs=audio_output)
|
| 192 |
|
| 193 |
+
# ===== 6. MOUNT GRADIO & RUN SERVER =====
|
| 194 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 195 |
|
| 196 |
if __name__ == "__main__":
|
| 197 |
import uvicorn
|
| 198 |
+
port = int(os.environ.get("PORT", 7860))
|
| 199 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|