radames commited on
Commit
7bf4167
·
1 Parent(s): 8f90fc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -9
app.py CHANGED
@@ -4,6 +4,7 @@ from fastapi.staticfiles import StaticFiles
4
  import uvicorn
5
  import gradio as gr
6
  from datetime import datetime
 
7
 
8
 
9
  # create a FastAPI app
@@ -16,24 +17,42 @@ static_dir.mkdir(parents=True, exist_ok=True)
16
  # mount FastAPI StaticFiles server
17
  app.mount("/static", StaticFiles(directory=static_dir), name="static")
18
 
19
- # Gradio code
 
 
20
  def predict(text_input):
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"""<h2>Hello {text_input} </h2>
26
- <h3>{file_name}</h3>
 
 
 
 
 
27
  """)
28
-
29
- return f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
 
30
 
31
 
32
  with gr.Blocks() as block:
33
- text_input = gr.Textbox(label="Name")
34
- markdown = gr.Markdown(label="Output Box")
35
- new_btn = gr.Button("New")
36
- new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown])
 
 
 
 
 
 
 
 
 
 
37
 
38
  # mount Gradio app to FastAPI app
39
  app = gr.mount_gradio_app(app, block, path="/")
@@ -41,3 +60,8 @@ app = gr.mount_gradio_app(app, block, path="/")
41
  # serve the app
42
  if __name__ == "__main__":
43
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
4
  import uvicorn
5
  import gradio as gr
6
  from datetime import datetime
7
+ import sys
8
 
9
 
10
  # create a FastAPI app
 
17
  # mount FastAPI StaticFiles server
18
  app.mount("/static", StaticFiles(directory=static_dir), name="static")
19
 
20
+ # Gradio stuff
21
+
22
+
23
  def predict(text_input):
24
  file_name = f"{datetime.utcnow().strftime('%s')}.html"
25
  file_path = static_dir / file_name
26
  print(file_path)
27
  with open(file_path, "w") as f:
28
+ f.write(f"""
29
+ <script src="https://cdn.tailwindcss.com"></script>
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
  """)
36
+ iframe = f"""<iframe src="/static/{file_name}" width="100%" height="500px"></iframe>"""
37
+ link = f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
38
+ return link, iframe
39
 
40
 
41
  with gr.Blocks() as block:
42
+ gr.Markdown("""
43
+ ## Gradio + FastAPI + Static Server
44
+ This is a demo of how to use Gradio with FastAPI and a static server.
45
+ The Gradio app generates dynamic HTML files and stores them in a static directory. FastAPI serves the static files.
46
+ """)
47
+ with gr.Row():
48
+ with gr.Column():
49
+ text_input = gr.Textbox(label="Name")
50
+ markdown = gr.Markdown(label="Output Box")
51
+ new_btn = gr.Button("New")
52
+ with gr.Column():
53
+ html = gr.HTML(label="HTML preview", show_label=True)
54
+
55
+ new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
56
 
57
  # mount Gradio app to FastAPI app
58
  app = gr.mount_gradio_app(app, block, path="/")
 
60
  # serve the app
61
  if __name__ == "__main__":
62
  uvicorn.run(app, host="0.0.0.0", port=7860)
63
+
64
+ # run the app with
65
+ # python app.py
66
+ # or
67
+ # uvicorn "app:app" --host "0.0.0.0" --port 7860 --reload