pdarleyjr commited on
Commit
0f9b256
·
1 Parent(s): 4c4c9e8

Add FastAPI with CORS middleware for GitHub Pages integration

Browse files
Files changed (1) hide show
  1. app.py +18 -2
app.py CHANGED
@@ -1,5 +1,19 @@
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,14 +62,16 @@ demo = gr.Interface(
48
  allow_flagging="never"
49
  )
50
 
51
- # Launch the app with configuration for Hugging Face Spaces
52
  if __name__ == "__main__":
53
  demo.queue(concurrency_count=3) # Allow multiple concurrent requests
 
54
  demo.launch(
55
  server_name="0.0.0.0",
56
  server_port=7860,
57
  enable_queue=True,
58
  show_api=True, # Enable API documentation
59
  share=False, # Not needed in Spaces
60
- debug=True
 
61
  )
 
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
+ # Create FastAPI app
7
+ app = FastAPI()
8
+
9
+ # Add CORS middleware
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["https://pdarleyjr.github.io"], # Allow GitHub Pages domain
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
 
18
  # Load the base T5 model and tokenizer
19
  model = T5ForConditionalGeneration.from_pretrained('t5-small')
 
62
  allow_flagging="never"
63
  )
64
 
65
+ # Mount the Gradio app and launch
66
  if __name__ == "__main__":
67
  demo.queue(concurrency_count=3) # Allow multiple concurrent requests
68
+ app = gr.mount_gradio_app(app, demo, path="/")
69
  demo.launch(
70
  server_name="0.0.0.0",
71
  server_port=7860,
72
  enable_queue=True,
73
  show_api=True, # Enable API documentation
74
  share=False, # Not needed in Spaces
75
+ debug=True,
76
+ root_path=""
77
  )