当前位置:   article > 正文

230902-部署Gradio到已有FastAPI及服务器中_gradio fastapi

gradio fastapi

在这里插入图片描述

1. 官方例子

  • run.py
from fastapi import FastAPI
import gradio as gr

CUSTOM_PATH = "/gradio"

app = FastAPI()


@app.get("/")
def read_main():
    return {"message": "This is your main app"}


io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
app = gr.mount_gradio_app(app, io, path=CUSTOM_PATH)


# Run this from the terminal as you would normally start a FastAPI app: `uvicorn run:app`
# and navigate to http://localhost:8000/gradio in your browser.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

运行方式:uvicorn run:app

2. 油管例子

  • gradio_ui.py
import gradio as gr


def greet(text: str) -> str:
    return text


demo = gr.Interface(
    fn=greet,
    inputs=gr.components.Textbox(label='Input'),
    outputs=gr.components.Textbox(label='Output'),
    allow_flagging='never'
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • run.py
from fastapi import FastAPI
import gradio as gr

from gradio_ui import demo

app = FastAPI()

@app.get('/')
async def root():
    return 'Gradio app is running at /gradio', 200

app = gr.mount_gradio_app(app, demo, path='/gradio')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 运行方式
uvicorn run:app --host 0.0.0.0 --port 5000
  • 1
  • 注意事项
1. 在命令行中的格式是<文件对象:挂载对象>
2. 文件对象,不要带py
3. 需要在同一个根目录下
  • 1
  • 2
  • 3

3. 视频演示

230920-部署Gradio到已有FastAPI及服务器

4. 参考文献

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/586306
推荐阅读
相关标签
  

闽ICP备14008679号