m7n commited on
Commit
2b75953
·
1 Parent(s): 5da1fd3

added static dir to allowed paths

Browse files
Files changed (1) hide show
  1. app.py +35 -36
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 # 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)
 
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