Spaces:
Running
on
Zero
Running
on
Zero
added static dir to allowed paths
Browse files
app.py
CHANGED
@@ -1,63 +1,62 @@
|
|
1 |
from pathlib import Path
|
2 |
-
import tempfile
|
3 |
import gradio as gr
|
4 |
from datetime import datetime
|
5 |
-
import os
|
6 |
import sys
|
|
|
7 |
|
8 |
-
import spaces
|
9 |
from spaces.zero.client import _get_token
|
10 |
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
|
|
|
17 |
|
18 |
@spaces.GPU(duration=10)
|
19 |
-
def predict(request: gr.Request,
|
20 |
token = _get_token(request)
|
21 |
file_name = f"{datetime.utcnow().strftime('%s')}.html"
|
22 |
-
file_path =
|
23 |
-
print(
|
24 |
with open(file_path, "w") as f:
|
25 |
-
f.write(f"""
|
26 |
-
|
27 |
-
<
|
28 |
-
<
|
29 |
-
|
30 |
-
|
31 |
-
<
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
</
|
36 |
-
</
|
|
|
37 |
""")
|
|
|
38 |
os.chmod(file_path, 0o644)
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# Here we assume it serves files from /file=<basename>.
|
43 |
-
iframe = f'<iframe src="/file={file_name}" width="100%" height="500px"></iframe>'
|
44 |
-
link = f'<a href="/file={file_name}" target="_blank">{file_name}</a>'
|
45 |
-
print("Serving file at URL:", f"/file={file_name}")
|
46 |
return link, iframe
|
47 |
|
48 |
with gr.Blocks() as block:
|
49 |
gr.Markdown("""
|
50 |
-
## Gradio +
|
51 |
-
This demo
|
|
|
52 |
""")
|
53 |
with gr.Row():
|
54 |
with gr.Column():
|
55 |
text_input = gr.Textbox(label="Name")
|
56 |
-
markdown = gr.Markdown(label="Output
|
57 |
new_btn = gr.Button("New")
|
58 |
with gr.Column():
|
59 |
-
html = gr.HTML(label="HTML
|
60 |
|
61 |
new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
|
62 |
|
63 |
-
block.launch(debug=True, share=False, ssr_mode=False)
|
|
|
1 |
from pathlib import Path
|
|
|
2 |
import gradio as gr
|
3 |
from datetime import datetime
|
|
|
4 |
import sys
|
5 |
+
import os
|
6 |
|
7 |
+
import spaces # necessary to run on Zero.
|
8 |
from spaces.zero.client import _get_token
|
9 |
|
10 |
+
|
11 |
+
# create a static directory to store the static files
|
12 |
+
static_dir = Path('./static')
|
13 |
+
static_dir.mkdir(parents=True, exist_ok=True)
|
14 |
+
#os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
|
15 |
+
export GRADIO_ALLOWED_PATHS="/static"
|
16 |
+
print(os.environ["GRADIO_ALLOWED_PATHS"] )
|
17 |
|
18 |
@spaces.GPU(duration=10)
|
19 |
+
def predict(request: gr.Request,text_input):
|
20 |
token = _get_token(request)
|
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"""
|
26 |
+
<!DOCTYPE html>
|
27 |
+
<html>
|
28 |
+
<head>
|
29 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
30 |
+
</head>
|
31 |
+
<body class="bg-gray-200 dark:text-white dark:bg-gray-900">
|
32 |
+
<h1 class="text-3xl font-bold">
|
33 |
+
Hello <i>{text_input}</i> From Gradio Iframe
|
34 |
+
</h1>
|
35 |
+
<h3>Filename: {file_name}</h3>
|
36 |
+
</body>
|
37 |
+
</html>
|
38 |
""")
|
39 |
+
file_path = static_dir / file_name
|
40 |
os.chmod(file_path, 0o644)
|
41 |
+
iframe = f'<iframe src="/file=static/{file_name}" width="100%" height="500px"></iframe>'
|
42 |
+
link = f'<a href="/file=static/{file_name}" target="_blank">{file_name}</a>'
|
43 |
+
print("Serving file at:", f"/file={file_path}")
|
|
|
|
|
|
|
|
|
44 |
return link, iframe
|
45 |
|
46 |
with gr.Blocks() as block:
|
47 |
gr.Markdown("""
|
48 |
+
## Gradio + FastAPI + Static Server
|
49 |
+
This is a demo of how to use Gradio with FastAPI and a static server.
|
50 |
+
The Gradio app generates dynamic HTML files and stores them in a static directory. FastAPI serves the static files.
|
51 |
""")
|
52 |
with gr.Row():
|
53 |
with gr.Column():
|
54 |
text_input = gr.Textbox(label="Name")
|
55 |
+
markdown = gr.Markdown(label="Output Box")
|
56 |
new_btn = gr.Button("New")
|
57 |
with gr.Column():
|
58 |
+
html = gr.HTML(label="HTML preview", show_label=True)
|
59 |
|
60 |
new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
|
61 |
|
62 |
+
block.launch(debug=True, share=False, ssr_mode=False)#,ssr_mode=False
|