Add FastAPI integration with proper CORS and queue handling
Browse files
app.py
CHANGED
@@ -1,5 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Load the base T5 model and tokenizer
|
5 |
model = T5ForConditionalGeneration.from_pretrained('t5-small')
|
@@ -48,11 +61,16 @@ demo = gr.Interface(
|
|
48 |
allow_flagging="never"
|
49 |
)
|
50 |
|
51 |
-
#
|
|
|
|
|
|
|
52 |
if __name__ == "__main__":
|
53 |
-
|
54 |
-
demo.
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
+
from fastapi import FastAPI
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Configure CORS
|
9 |
+
app.add_middleware(
|
10 |
+
CORSMiddleware,
|
11 |
+
allow_origins=["https://pdarleyjr.github.io"],
|
12 |
+
allow_credentials=True,
|
13 |
+
allow_methods=["*"],
|
14 |
+
allow_headers=["*"],
|
15 |
+
)
|
16 |
|
17 |
# Load the base T5 model and tokenizer
|
18 |
model = T5ForConditionalGeneration.from_pretrained('t5-small')
|
|
|
61 |
allow_flagging="never"
|
62 |
)
|
63 |
|
64 |
+
# Mount Gradio app to FastAPI
|
65 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
66 |
+
|
67 |
+
# Launch the app with FastAPI integration
|
68 |
if __name__ == "__main__":
|
69 |
+
import uvicorn
|
70 |
+
demo.queue(max_size=20)
|
71 |
+
uvicorn.run(
|
72 |
+
app,
|
73 |
+
host="0.0.0.0",
|
74 |
+
port=7860,
|
75 |
+
log_level="info"
|
76 |
)
|