Spaces:
Sleeping
Sleeping
try temp
Browse files
app.py
CHANGED
@@ -1,61 +1,63 @@
|
|
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
|
8 |
from spaces.zero.client import _get_token
|
9 |
|
10 |
-
|
11 |
-
# create a
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
@spaces.GPU(duration=10)
|
18 |
-
def predict(request: gr.Request,text_input):
|
19 |
token = _get_token(request)
|
20 |
file_name = f"{datetime.utcnow().strftime('%s')}.html"
|
21 |
-
file_path =
|
22 |
-
print(file_path)
|
23 |
with open(file_path, "w") as f:
|
24 |
-
f.write(f"""
|
25 |
-
|
26 |
-
|
27 |
-
<
|
28 |
-
|
29 |
-
|
30 |
-
<
|
31 |
-
<
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
</html>
|
37 |
""")
|
38 |
-
file_path = static_dir / file_name
|
39 |
os.chmod(file_path, 0o644)
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
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 tempfile
|
3 |
import gradio as gr
|
4 |
from datetime import datetime
|
|
|
5 |
import os
|
6 |
+
import sys
|
7 |
|
8 |
+
import spaces # necessary to run on Zero.
|
9 |
from spaces.zero.client import _get_token
|
10 |
|
11 |
+
# Instead of a static folder, use the system temporary directory.
|
12 |
+
# You can also create a subfolder within tempfile.gettempdir() if needed.
|
13 |
+
rtemp_dir = Path(tempfile.gettempdir()) / "gradio_generated_files"
|
14 |
+
rtemp_dir.mkdir(parents=True, exist_ok=True)
|
15 |
+
# Optionally, set GRADIO_ALLOWED_PATHS to this directory if required.
|
16 |
+
os.environ["GRADIO_ALLOWED_PATHS"] = str(rtemp_dir.resolve())
|
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 = rtemp_dir / file_name
|
23 |
+
print("Writing file to:", file_path)
|
24 |
with open(file_path, "w") as f:
|
25 |
+
f.write(f"""<!DOCTYPE html>
|
26 |
+
<html>
|
27 |
+
<head>
|
28 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
29 |
+
</head>
|
30 |
+
<body class="bg-gray-200 dark:text-white dark:bg-gray-900">
|
31 |
+
<h1 class="text-3xl font-bold">
|
32 |
+
Hello <i>{text_input}</i> From Gradio Iframe
|
33 |
+
</h1>
|
34 |
+
<h3>Filename: {file_name}</h3>
|
35 |
+
</body>
|
36 |
+
</html>
|
|
|
37 |
""")
|
|
|
38 |
os.chmod(file_path, 0o644)
|
39 |
+
# Construct the URL relative to the repo root.
|
40 |
+
# If Gradio automatically serves files from the temporary directory,
|
41 |
+
# you might need to check what URL path it uses.
|
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 + Temporary Files Demo
|
51 |
+
This demo generates dynamic HTML files and stores them in the temporary directory.
|
|
|
52 |
""")
|
53 |
with gr.Row():
|
54 |
with gr.Column():
|
55 |
text_input = gr.Textbox(label="Name")
|
56 |
+
markdown = gr.Markdown(label="Output Link")
|
57 |
new_btn = gr.Button("New")
|
58 |
with gr.Column():
|
59 |
+
html = gr.HTML(label="HTML Preview", show_label=True)
|
60 |
|
61 |
new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
|
62 |
|
63 |
+
block.launch(debug=True, share=False, ssr_mode=False)
|