File size: 1,363 Bytes
9e6aa30
 
 
 
 
 
 
 
 
 
 
53b5c95
9e6aa30
 
 
 
 
 
a09b191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32a6c80
9e6aa30
a09b191
 
9e6aa30
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import logging

import gradio as gr
import uvicorn
from fastapi import FastAPI

import routes
from helpers import formatters, session_logger


session_logger.change_logging()
app = FastAPI(title="fastapi_with_gradio...", version="1.0")
logging.info("FastAPI app created, including routes...")
app.include_router(routes.router)
logging.info("routes included, creating gradio app")


CUSTOM_GRADIO_PATH = "/"
def get_gradio_app():
    with gr.Blocks() as gradio_app:
        gr.Markdown(
        """
        # Hello World!
        
        Start typing below to _see_ the *output*.

        Here a [link](https://huggingface.co/spaces/aletrn/gradio_with_fastapi).
        """)
        btn = gr.Button(value="Divide et Impera")
        btn.click(
            formatters.request_formatter,
            inputs=[
                gr.Textbox(lines=1, placeholder="10", label="write an integer to divide 100; 0 will raise ZeroDivisionError")
            ],
            outputs=[
                gr.Textbox(lines=1, placeholder=None, label="Text Output")
            ]
        )
    return gradio_app


logging.info("mounting gradio app within FastAPI...")
gradio_app_md = get_gradio_app()
app = gr.mount_gradio_app(app, gradio_app_md, path=CUSTOM_GRADIO_PATH)
logging.info("gradio app mounted")


if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=7860)