Jianpeng Li commited on
Commit
9f35f8e
·
0 Parent(s):

first commit

Browse files
Files changed (2) hide show
  1. README.md +6 -0
  2. app.py +85 -0
README.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ ---
2
+ title: server
3
+ app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 4.1.2
6
+ ---
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from fastapi import FastAPI
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi import File, UploadFile
5
+
6
+ import uvicorn
7
+ import gradio as gr
8
+ from datetime import datetime
9
+ import sys
10
+
11
+
12
+ # create a FastAPI app
13
+ app = FastAPI()
14
+
15
+ # create a static directory to store the static files
16
+ static_dir = Path('./static')
17
+ static_dir.mkdir(parents=True, exist_ok=True)
18
+
19
+ # mount FastAPI StaticFiles server
20
+ app.mount("/static", StaticFiles(directory=static_dir), name="static")
21
+ app.mount("/public", StaticFiles(directory=static_dir), name="static")
22
+
23
+ @app.post("/upload")
24
+ def upload(file: UploadFile = File(...)):
25
+ try:
26
+ file_name = f"{datetime.utcnow().strftime('%s')}.html"
27
+ file_path = static_dir / file_name
28
+ print(file_path)
29
+ with open(file_path, 'wb') as f:
30
+ while contents := file.file.read(1024 * 1024):
31
+ f.write(contents)
32
+ except Exception:
33
+ return {"message": "There was an error uploading the file"}
34
+ finally:
35
+ file.file.close()
36
+
37
+ return {"message": f"Successfully uploaded {file_path}", "url": f"https://punglee-server.hf.space/{file_path}"}
38
+
39
+
40
+ def predict(text_input):
41
+ file_name = f"{datetime.utcnow().strftime('%s')}.html"
42
+ file_path = static_dir / file_name
43
+ print(file_path)
44
+ with open(file_path, "w") as f:
45
+ f.write(f"""
46
+ <script src="https://cdn.tailwindcss.com"></script>
47
+ <body class="bg-gray-200 dark:text-white dark:bg-gray-900">
48
+ <h1 class="text-3xl font-bold">
49
+ Hello <i>{text_input}</i> From Gradio Iframe
50
+ </h1>
51
+ <h3>Filename: {file_name}</h3>
52
+ """)
53
+ iframe = f"""<iframe src="/static/{file_name}" width="100%" height="500px"></iframe>"""
54
+ link = f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
55
+ return link, iframe
56
+
57
+
58
+ with gr.Blocks() as block:
59
+ gr.Markdown("""
60
+ ## Gradio + FastAPI + Static Server
61
+ This is a demo of how to use Gradio with FastAPI and a static server.
62
+ The Gradio app generates dynamic HTML files and stores them in a static directory. FastAPI serves the static files.
63
+ """)
64
+ with gr.Row():
65
+ with gr.Column():
66
+ text_input = gr.Textbox(label="Name")
67
+ markdown = gr.Markdown(label="Output Box")
68
+ new_btn = gr.Button("New")
69
+ with gr.Column():
70
+ html = gr.HTML(label="HTML preview", show_label=True)
71
+
72
+ new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
73
+
74
+
75
+ # mount Gradio app to FastAPI app
76
+ app = gr.mount_gradio_app(app, block, path="/")
77
+
78
+ # serve the app
79
+ if __name__ == "__main__":
80
+ uvicorn.run(app, host="0.0.0.0", port=7860)
81
+
82
+ # run the app with
83
+ # python app.py
84
+ # or
85
+ # uvicorn "app:app" --host "0.0.0.0" --port 7860 --reload