MaenGit commited on
Commit
922672f
·
1 Parent(s): 7ac3704

initialisation commit

Browse files
Files changed (4) hide show
  1. .vscode/settings.json +5 -0
  2. Dockerfile +18 -0
  3. main.py +54 -0
  4. requirements.txt +4 -0
.vscode/settings.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "python-envs.defaultEnvManager": "ms-python.python:conda",
3
+ "python-envs.defaultPackageManager": "ms-python.python:conda",
4
+ "python-envs.pythonProjects": []
5
+ }
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY main.py .
9
+
10
+ RUN useradd -m -u 1000 user
11
+ USER user
12
+ ENV HOME=/home/user \
13
+ PATH=/home/user/.local/bin:$PATH
14
+
15
+ EXPOSE 7860
16
+
17
+ # إضافة --proxy-headers إذا كنت تستخدمه خلف Nginx أو Hugging Face
18
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--timeout-keep-alive", "65"]
main.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Query
2
+ from fastapi.responses import StreamingResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import edge_tts
5
+ import logging
6
+
7
+ # إضافة سجلات لمراقبة الأخطاء
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(name)
10
+
11
+ app = FastAPI()
12
+
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ @app.get("/")
22
+ async def home():
23
+ return {"status": "running", "message": "Ready to stream audio"}
24
+
25
+ async def generate_audio_stream(text, voice, rate, pitch):
26
+ try:
27
+ communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch)
28
+ async for chunk in communicate.stream():
29
+ if chunk["type"] == "audio":
30
+ yield chunk["data"]
31
+ except Exception as e:
32
+ logger.error(f"Error during streaming: {e}")
33
+
34
+ @app.get("/tts")
35
+ async def tts_endpoint(
36
+ text: str = Query(..., min_length=1), # التأكد من عدم إرسال نص فارغ
37
+ voice: str = "en-US-AriaNeural",
38
+ rate: str = "+0%",
39
+ pitch: str = "+0Hz"
40
+ ):
41
+ # التحقق من طول النص (edge-tts قد يواجه مشاكل مع النصوص الطويلة جداً دفعة واحدة)
42
+ if len(text) > 1000:
43
+ logger.warning("Text too long, might cause delays.")
44
+
45
+ try:
46
+ # إضافة Header لتحديد طول المحتوى غير معروف (للمشغلات المتصفح)
47
+ return StreamingResponse(
48
+ generate_audio_stream(text, voice, rate, pitch),
49
+ media_type="audio/mpeg",
50
+ headers={"Cache-Control": "no-cache"} # لمنع تخزين أخطاء الصوت
51
+ )
52
+ except Exception as e:
53
+ logger.error(f"TTS Endpoint Error: {e}")
54
+ raise HTTPException(status_code=500, detail="Internal Server Error")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ edge-tts
4
+ httpx