caphuutri commited on
Commit
35638bb
·
verified ·
1 Parent(s): 0143bfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -9
app.py CHANGED
@@ -1,11 +1,13 @@
1
  import edge_tts
 
 
 
2
  from fastapi import FastAPI, Request
3
  from fastapi.responses import StreamingResponse, JSONResponse
4
  from fastapi.middleware.cors import CORSMiddleware
5
 
6
  app = FastAPI()
7
 
8
- # Mở toang cửa CORS để Web gọi vào không bị chặn
9
  app.add_middleware(
10
  CORSMiddleware,
11
  allow_origins=["*"],
@@ -14,15 +16,22 @@ app.add_middleware(
14
  allow_headers=["*"],
15
  )
16
 
 
 
 
17
  @app.get("/")
18
  def read_root():
 
19
  return {"status": "VCLOUD Voice API is running optimally on Hugging Face!"}
20
 
21
  @app.post("/tts")
22
  @app.get("/tts")
23
  async def generate_tts(request: Request):
24
  text = ""
25
- voice = "vi-VN-HoaiMyNeural" # Giọng đọc mặc định
 
 
 
26
 
27
  if request.method == "POST":
28
  try:
@@ -34,22 +43,38 @@ async def generate_tts(request: Request):
34
  elif request.method == "GET":
35
  text = request.query_params.get("text", "").strip()
36
  voice = request.query_params.get("voice", voice)
 
 
 
37
 
 
38
  if not text:
39
- return JSONResponse(
40
- {"status": "success", "message": "Ping OK. Trạm Voice Hugging Face đang hoạt động hoàn hảo!"},
41
- status_code=200
42
- )
43
 
 
44
  try:
45
- communicate = edge_tts.Communicate(text, voice)
 
 
 
46
 
47
- # 🟢 NÂNG CẤP STREAMING: Bơm thẳng byte âm thanh từ RAM về Web, không chạm vào ổ cứng
 
 
 
 
 
 
 
 
 
 
 
 
48
  async def audio_stream():
49
  async for chunk in communicate.stream():
50
  if chunk["type"] == "audio":
51
  yield chunk["data"]
52
-
53
  return StreamingResponse(audio_stream(), media_type="audio/mpeg")
54
  except Exception as e:
55
  return JSONResponse({"error": str(e)}, status_code=500)
 
1
  import edge_tts
2
+ import hmac
3
+ import hashlib
4
+ import time
5
  from fastapi import FastAPI, Request
6
  from fastapi.responses import StreamingResponse, JSONResponse
7
  from fastapi.middleware.cors import CORSMiddleware
8
 
9
  app = FastAPI()
10
 
 
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
 
16
  allow_headers=["*"],
17
  )
18
 
19
+ # 🔐 MẬT MÃ BÍ MẬT (CHỈ HF VÀ VPS CỦA SẾP ĐƯỢC BIẾT)
20
+ SECRET_KEY = "VCLOUD_!@#_SECRET_2026"
21
+
22
  @app.get("/")
23
  def read_root():
24
+ # Điểm danh cho Cronjob/Worker (Không cần Token)
25
  return {"status": "VCLOUD Voice API is running optimally on Hugging Face!"}
26
 
27
  @app.post("/tts")
28
  @app.get("/tts")
29
  async def generate_tts(request: Request):
30
  text = ""
31
+ voice = "vi-VN-HoaiMyNeural"
32
+ bot_uid = ""
33
+ req_time = "0"
34
+ token = ""
35
 
36
  if request.method == "POST":
37
  try:
 
43
  elif request.method == "GET":
44
  text = request.query_params.get("text", "").strip()
45
  voice = request.query_params.get("voice", voice)
46
+ bot_uid = request.query_params.get("bot_uid", "").strip()
47
+ req_time = request.query_params.get("time", "0").strip()
48
+ token = request.query_params.get("token", "").strip()
49
 
50
+ # Cho phép Admin Ping nút xanh trong Cài đặt (Không cần text)
51
  if not text:
52
+ return JSONResponse({"status": "success", "message": "Ping OK."}, status_code=200)
 
 
 
53
 
54
+ # 🛡️ HÀNG RÀO BẢO MẬT CHỮ KÝ HMAC
55
  try:
56
+ # 1. Chống dùng lại Token chôm chỉa (Hết hạn sau 12 tiếng = 43200s)
57
+ current_time = int(time.time())
58
+ if current_time - int(req_time) > 43200:
59
+ return JSONResponse({"error": "Token đã hết hạn!"}, status_code=401)
60
 
61
+ # 2. Đối chiếu Chữ ký (Mã hóa SHA-256)
62
+ message = f"{bot_uid}{req_time}".encode('utf-8')
63
+ expected_token = hmac.new(SECRET_KEY.encode('utf-8'), message, hashlib.sha256).hexdigest()
64
+
65
+ if not hmac.compare_digest(expected_token, token):
66
+ return JSONResponse({"error": "Truy cập bị từ chối! Sai Token bảo mật."}, status_code=403)
67
+
68
+ except Exception as e:
69
+ return JSONResponse({"error": "Lỗi xác thực bảo mật"}, status_code=401)
70
+
71
+ # 🚀 VƯỢT RÀO THÀNH CÔNG -> STREAM TỪ RAM
72
+ try:
73
+ communicate = edge_tts.Communicate(text, voice)
74
  async def audio_stream():
75
  async for chunk in communicate.stream():
76
  if chunk["type"] == "audio":
77
  yield chunk["data"]
 
78
  return StreamingResponse(audio_stream(), media_type="audio/mpeg")
79
  except Exception as e:
80
  return JSONResponse({"error": str(e)}, status_code=500)