gradio-subapp / app.py
freddyaboulton's picture
Optional
db7af4f
raw
history blame
1.33 kB
import gradio as gr
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
import uvicorn
app = FastAPI()
@app.on_event("startup")
def print_foo():
print("FOO!!!")
def dream(prompt):
return "hello world!"
block = gr.Blocks().queue()
with block:
prompt = gr.Text()
gallery = gr.Text()
btn = gr.Button("Generate")
btn.click(dream, inputs=prompt, outputs=[gallery])
def mount_gradio_app(
app: FastAPI, blocks: gr.Blocks, path: str, gradio_api: str
) -> FastAPI:
"""Mount a gradio application (created with gr.routes.App.create_app(block)) to an existing FastAPI application.
Parameters:
app: The parent FastAPI application.
blocks: The blocks application we want to moung
path: The path at which the gradio application will be mounted.
"""
gradio_app = gr.routes.App.create_app(blocks)
@app.on_event("startup")
async def start_queue():
if gradio_app.blocks.enable_queue:
gradio_app.blocks._queue.set_url(gradio_api)
gradio_app.blocks.startup_events()
app.mount(path, gradio_app)
return app
app = mount_gradio_app(app, block, "/", gradio_api="http://localhost:7860/")
app.mount("/img", StaticFiles(directory="images", html=True), name="images")
uvicorn.run(app, host="0.0.0.0", port=7860)