Spaces:
Runtime error
Runtime error
Commit
·
cedc9e6
1
Parent(s):
56a5699
Upload 4 files
Browse files- app.py +67 -0
- requirements.txt +3 -0
- static/1678999939.html +8 -0
- static/output.html +44 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
import uvicorn
|
5 |
+
import gradio as gr
|
6 |
+
from datetime import datetime
|
7 |
+
import sys
|
8 |
+
|
9 |
+
|
10 |
+
# create a FastAPI app
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
# create a static directory to store the static files
|
14 |
+
static_dir = Path('./static')
|
15 |
+
static_dir.mkdir(parents=True, exist_ok=True)
|
16 |
+
|
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="/")
|
59 |
+
|
60 |
+
# serve the app
|
61 |
+
if __name__ == "__main__":
|
62 |
+
uvicorn.run(app, host="127.0.0.1", port=7862)
|
63 |
+
|
64 |
+
# run the app with
|
65 |
+
# python app.py
|
66 |
+
# or
|
67 |
+
# uvicorn "app:app" --host "0.0.0.0" --port 7860 --reload
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
uvicorn
|
2 |
+
fastapi
|
3 |
+
gradio
|
static/1678999939.html
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
3 |
+
<body class="bg-gray-200 dark:text-white dark:bg-gray-900">
|
4 |
+
<h1 class="text-3xl font-bold">
|
5 |
+
Hello <i>test</i> From Gradio Iframe
|
6 |
+
</h1>
|
7 |
+
<h3>Filename: 1678999939.html</h3>
|
8 |
+
|
static/output.html
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
<h1 class="p-name">Moroccan-Style Tomato Salad</h1>
|
4 |
+
<p class="p-summary">A salad that's the perfect accompaniment to summer grills.</p>
|
5 |
+
<h2>Ingredients</h2>
|
6 |
+
<ul class="ingredient-list">
|
7 |
+
<li><span class="value">200g</span> tinned chickpeas</li>
|
8 |
+
<li><span class="value">1/2 tsp</span> ground cumin</li>
|
9 |
+
<li><span class="value">1/2 tsp</span> cayenne</li>
|
10 |
+
<li><span class="value">1/2 tsp</span> ground coriander</li>
|
11 |
+
<li>Extra-virgin olive oil</li>
|
12 |
+
<li><span class="value">1 kg</span> mixed ripe tomatoes</li>
|
13 |
+
<li><span class="value">1/2</span> cucumber</li>
|
14 |
+
<li><span class="value">4</span> spring onions</li>
|
15 |
+
<li><span class="value">1</span> green chilli</li>
|
16 |
+
<li><span class="value">1</span> small lemon</li>
|
17 |
+
<li><span class="value">1/2</span> small bunch of dill</li>
|
18 |
+
<li><span class="value">1/2</span> small bunch of coriander</li>
|
19 |
+
</ul>
|
20 |
+
<h2>Dressing</h2>
|
21 |
+
<ul class="ingredient-list">
|
22 |
+
<li><span class="value">4 tbsp</span> extra-virgin olive oil</li>
|
23 |
+
<li><span class="value">1 tbsp</span> lemon juice (plus juice reserved from zested lemon)</li>
|
24 |
+
<li><span class="value">2-3 tsp</span> harissa</li>
|
25 |
+
<li><span class="value">2-3 tsp</span> balsamic vinegar</li>
|
26 |
+
<li><span class="value">1/2 tsp</span> sumac</li>
|
27 |
+
</ul>
|
28 |
+
<h2>Instructions</h2>
|
29 |
+
<ol class="instruction-list">
|
30 |
+
<li>Preheat the oven to 190°C. Tip the chickpeas into a small roasting tin, add the cumin, cayenne, coriander and 1 tablespoon of olive oil, and season. Mix to coat the chickpeas in the spices, then roast in the oven for about 20 minutes, until golden and crisp.</li>
|
31 |
+
<li>Meanwhile, prepare the tomatoes, slicing the larger ones and cutting any cherry tomatoes into halves or quarters. Arrange on a large platter. Peel and thinly slice the cucumber, then tuck among the tomatoes. Trim and thinly slice the spring onions and chilli. Peel and finely slice half the lemon zest, then scrunch with a pinch of salt and a squeeze of lemon juice, then scatter over salad with spring onion and chilli. Roughly chop herbs and set aside.</li>
|
32 |
+
<li>To make dressing, combine extra-virgin olive oil with one tablespoon of lemon juice (from the zested lemon), harissa and vinegar and season well. Taste, adding more harissa or juice as needed. Scatter the herbs and toasted chickpeas over the salad, spoon over the dressing, sprinkle with the sumac and leave the salad to sit for 10 minutes, allowing the flavours to mingle, before serving.</li>
|
33 |
+
</ol>
|
34 |
+
<h2>Nutrition</h2>
|
35 |
+
<ul class="nutrition-list">
|
36 |
+
<li><strong>Calories:</strong> 181</li>
|
37 |
+
<li><strong>Fat:</strong> 14.3g (2g sat fats)</li>
|
38 |
+
<li><strong>Protein:</strong> 2.5g</li>
|
39 |
+
<li><strong>Carbohydrates:</strong> 10.9g</li>
|
40 |
+
<li><strong>Sugar:</strong> 10.6g</li>
|
41 |
+
<li><strong>Sodium:</strong> 0.26g</li>
|
42 |
+
<li><strong>Fiber:</strong> 3.4g</li>
|
43 |
+
</ul>
|
44 |
+
<div class="p-note">Recipe source: adapted from BBC Good Food</div>
|