helliun commited on
Commit
3099bf0
·
verified ·
1 Parent(s): 6d1648e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from fastapi import FastAPI
3
+ from fastapi.staticfiles import StaticFiles
4
+ import uvicorn
5
+ import gradio as gr
6
+ from datetime import datetime
7
+
8
+
9
+ # create a FastAPI app
10
+ app = FastAPI()
11
+
12
+ # create a static directory to store the static files
13
+ static_dir = Path('./static')
14
+ static_dir.mkdir(parents=True, exist_ok=True)
15
+
16
+ # mount FastAPI StaticFiles server
17
+ app.mount("/static", StaticFiles(directory=static_dir), name="static")
18
+
19
+ # Gradio code
20
+ def predict(text_input):
21
+ file_name = f"{datetime.utcnow().strftime('%s')}.html"
22
+ file_path = static_dir / file_name
23
+ print(file_path)
24
+ with open(file_path, "w") as f:
25
+ f.write(f"""<h2>Hello {text_input} </h2>
26
+ <h3>{file_name}</h3>
27
+ """)
28
+
29
+ return f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
30
+
31
+
32
+ with gr.Blocks() as block:
33
+ text_input = gr.Textbox(label="Name")
34
+ markdown = gr.Markdown(label="Output Box")
35
+ new_btn = gr.Button("New")
36
+ new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown])
37
+
38
+ # mount Gradio app to FastAPI app
39
+ app = gr.mount_gradio_app(app, block, path="/")
40
+
41
+ # serve the app
42
+ if __name__ == "__main__":
43
+ uvicorn.run(app, host="0.0.0.0", port=7860)
44
+
45
+ # run the app with
46
+ # python app.py
47
+ # or
48
+ # uvicorn "app:app" --host "0.0.0.0" --port 7860 --reload