pdarleyjr commited on
Commit
9e0003a
·
1 Parent(s): 4fbb619

Add FastAPI integration with proper CORS and queue handling

Browse files
Files changed (1) hide show
  1. app.py +24 -6
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
- # Launch the app with minimal configuration
 
 
 
52
  if __name__ == "__main__":
53
- demo.queue(max_size=20) # Configure queue with max size
54
- demo.launch(
55
- server_name="0.0.0.0",
56
- server_port=7860,
57
- share=False
 
 
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
  )