tuhbooh commited on
Commit
f84cbd3
·
verified ·
1 Parent(s): 8bb8a78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -13
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import translators as ts
3
  import time
4
- from fastapi import FastAPI
5
  from fastapi.middleware.cors import CORSMiddleware
6
 
7
  # --- LOGIC DỊCH THUẬT ---
@@ -56,22 +55,17 @@ with gr.Blocks(title="Translation API") as demo:
56
  api_name="translate"
57
  )
58
 
59
- # --- XỬ LÝ CORS BẰNG FASTAPI ---
60
- app = FastAPI()
61
-
62
- # Cấu hình Middleware để cho phép gọi từ bất kỳ website nào (HTML của Minh Đức)
63
- app.add_middleware(
64
  CORSMiddleware,
65
- allow_origins=["*"], # Cho phép tất cả các trang web gọi vào
66
  allow_credentials=True,
67
  allow_methods=["*"],
68
  allow_headers=["*"],
69
  )
70
 
71
- # Gắn Gradio vào FastAPI
72
- app = gr.mount_gradio_app(app, demo, path="/")
73
-
74
  if __name__ == "__main__":
75
- import uvicorn
76
- # Chạy ứng dụng bằng uvicorn để FastAPI hiệu lực
77
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import gradio as gr
2
  import translators as ts
3
  import time
 
4
  from fastapi.middleware.cors import CORSMiddleware
5
 
6
  # --- LOGIC DỊCH THUẬT ---
 
55
  api_name="translate"
56
  )
57
 
58
+ # --- XỬ LÝ CORS TRỰC TIẾP TRÊN GRADIO APP ---
59
+ # Gradio.launch() sẽ khởi tạo app. Chúng ta thêm middleware vào app của demo.
60
+ demo.app.add_middleware(
 
 
61
  CORSMiddleware,
62
+ allow_origins=["*"],
63
  allow_credentials=True,
64
  allow_methods=["*"],
65
  allow_headers=["*"],
66
  )
67
 
 
 
 
68
  if __name__ == "__main__":
69
+ # Trên HF Spaces, chỉ cần gọi demo.launch()
70
+ # HF sẽ tự quản port host, không cần uvicorn.run
71
+ demo.launch()