Spaces:
Running
Running
Update app.py
Browse files
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
|
| 60 |
-
|
| 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=["*"],
|
| 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 |
-
|
| 76 |
-
#
|
| 77 |
-
|
|
|
|
| 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 lý port và host, không cần uvicorn.run
|
| 71 |
+
demo.launch()
|