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