Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import uuid
|
|
| 3 |
import os
|
| 4 |
from fastapi import FastAPI, Depends, HTTPException, status
|
| 5 |
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
| 6 |
-
from fastapi.
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from typing import Annotated
|
| 9 |
|
|
@@ -33,10 +33,13 @@ async def verify_token(token: str = Depends(oauth2_scheme)):
|
|
| 33 |
)
|
| 34 |
return token
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
AUDIO_DIR = "/
|
| 38 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
VOICE_MAP = {
|
| 41 |
"en": {"female": "en-US-AriaNeural", "male": "en-US-GuyNeural"},
|
| 42 |
"ar": {"female": "ar-EG-SalmaNeural", "male": "ar-EG-ShakirNeural"}
|
|
@@ -46,14 +49,6 @@ async def generate_tts(text, filepath, voice):
|
|
| 46 |
communicate = edge_tts.Communicate(text, voice)
|
| 47 |
await communicate.save(filepath)
|
| 48 |
|
| 49 |
-
# مسار جديد خالص ومباشر لجلب ملف الـ mp3 من غير mount
|
| 50 |
-
@app.get("/get-audio/{filename}")
|
| 51 |
-
async def get_audio(filename: str):
|
| 52 |
-
filepath = os.path.join(AUDIO_DIR, filename)
|
| 53 |
-
if not os.path.exists(filepath):
|
| 54 |
-
raise HTTPException(status_code=404, detail="File not found")
|
| 55 |
-
return FileResponse(filepath, media_type="audio/mpeg")
|
| 56 |
-
|
| 57 |
@app.get("/speak")
|
| 58 |
async def speak(
|
| 59 |
text: str,
|
|
@@ -68,8 +63,8 @@ async def speak(
|
|
| 68 |
|
| 69 |
await generate_tts(text, filepath, voice_name)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
return {
|
| 73 |
-
"audio_url": f"https://mohamedislegend4-pipertts.hf.space/
|
| 74 |
"voice_used": voice_name
|
| 75 |
}
|
|
|
|
| 3 |
import os
|
| 4 |
from fastapi import FastAPI, Depends, HTTPException, status
|
| 5 |
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
| 6 |
+
from fastapi.staticfiles import StaticFiles
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from typing import Annotated
|
| 9 |
|
|
|
|
| 33 |
)
|
| 34 |
return token
|
| 35 |
|
| 36 |
+
# استخدام فولدر /tmp السحري عشان صلاحيات الـ Docker والـ Proxy
|
| 37 |
+
AUDIO_DIR = "/tmp/audio"
|
| 38 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
| 39 |
|
| 40 |
+
# تخديم الفولدر بمساره الجديد
|
| 41 |
+
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
|
| 42 |
+
|
| 43 |
VOICE_MAP = {
|
| 44 |
"en": {"female": "en-US-AriaNeural", "male": "en-US-GuyNeural"},
|
| 45 |
"ar": {"female": "ar-EG-SalmaNeural", "male": "ar-EG-ShakirNeural"}
|
|
|
|
| 49 |
communicate = edge_tts.Communicate(text, voice)
|
| 50 |
await communicate.save(filepath)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
@app.get("/speak")
|
| 53 |
async def speak(
|
| 54 |
text: str,
|
|
|
|
| 63 |
|
| 64 |
await generate_tts(text, filepath, voice_name)
|
| 65 |
|
| 66 |
+
# الـ JSON راجع بنفس الشكل القديم بالملي عشان الـ Apps متضربش
|
| 67 |
return {
|
| 68 |
+
"audio_url": f"https://mohamedislegend4-pipertts.hf.space/audio/{filename}",
|
| 69 |
"voice_used": voice_name
|
| 70 |
}
|