Spaces:
Running
on
Zero
Running
on
Zero
reintroduced uvicorn
Browse files
app.py
CHANGED
|
@@ -4,6 +4,9 @@ from datetime import datetime
|
|
| 4 |
import os
|
| 5 |
import spaces # necessary to run on Zero.
|
| 6 |
from spaces.zero.client import _get_token
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Create a static directory to store the dynamic HTML files
|
| 9 |
static_dir = Path("./static")
|
|
@@ -13,6 +16,12 @@ static_dir.mkdir(parents=True, exist_ok=True)
|
|
| 13 |
os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
|
| 14 |
print("os.environ['GRADIO_ALLOWED_PATHS'] =", os.environ["GRADIO_ALLOWED_PATHS"])
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@spaces.GPU(duration=10)
|
| 17 |
def predict(request: gr.Request, text_input):
|
| 18 |
token = _get_token(request)
|
|
@@ -43,7 +52,7 @@ def predict(request: gr.Request, text_input):
|
|
| 43 |
with gr.Blocks() as block:
|
| 44 |
gr.Markdown("""
|
| 45 |
## Gradio + Static Files Demo
|
| 46 |
-
This demo generates dynamic HTML files and stores them in a "static" directory. They are then served via Gradio
|
| 47 |
""")
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column():
|
|
@@ -55,4 +64,9 @@ This demo generates dynamic HTML files and stores them in a "static" directory.
|
|
| 55 |
|
| 56 |
new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import spaces # necessary to run on Zero.
|
| 6 |
from spaces.zero.client import _get_token
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
+
import uvicorn
|
| 10 |
|
| 11 |
# Create a static directory to store the dynamic HTML files
|
| 12 |
static_dir = Path("./static")
|
|
|
|
| 16 |
os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
|
| 17 |
print("os.environ['GRADIO_ALLOWED_PATHS'] =", os.environ["GRADIO_ALLOWED_PATHS"])
|
| 18 |
|
| 19 |
+
# Create FastAPI app
|
| 20 |
+
app = FastAPI()
|
| 21 |
+
|
| 22 |
+
# Mount the static directory
|
| 23 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 24 |
+
|
| 25 |
@spaces.GPU(duration=10)
|
| 26 |
def predict(request: gr.Request, text_input):
|
| 27 |
token = _get_token(request)
|
|
|
|
| 52 |
with gr.Blocks() as block:
|
| 53 |
gr.Markdown("""
|
| 54 |
## Gradio + Static Files Demo
|
| 55 |
+
This demo generates dynamic HTML files and stores them in a "static" directory. They are then served via Gradio's `/file=` route.
|
| 56 |
""")
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
|
|
|
| 64 |
|
| 65 |
new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
|
| 66 |
|
| 67 |
+
# Mount Gradio app to FastAPI
|
| 68 |
+
app = gr.mount_gradio_app(app, block, path="/")
|
| 69 |
+
|
| 70 |
+
# Run both servers
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|