gradio-pr-bot commited on
Commit
87fb88a
1 Parent(s): 2d0f950

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. app.py +25 -8
  2. requirements.txt +2 -4
  3. templates/index.html +118 -0
app.py CHANGED
@@ -4,12 +4,30 @@ import os
4
  import sys
5
  import copy
6
  import pathlib
 
 
 
 
7
 
8
  os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
9
 
10
  demo_dir = pathlib.Path(__file__).parent / "demos"
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  all_demos = []
14
  demo_module = None
15
  for p in sorted(os.listdir("./demos")):
@@ -20,16 +38,15 @@ for p in sorted(os.listdir("./demos")):
20
  demo_module = importlib.import_module(f"run")
21
  else:
22
  demo_module = importlib.reload(demo_module)
23
- all_demos.append((p, demo_module.demo))
24
  except Exception as e:
25
- p = p + " ❌"
26
  with gr.Blocks() as demo:
27
  gr.Markdown(f"Error loading demo: {e}")
28
- all_demos.append((p, demo))
 
 
 
29
 
30
- with gr.Blocks() as mega_demo:
31
- for demo_name, demo in all_demos:
32
- with gr.Tab(demo_name):
33
- demo.render()
34
 
35
- mega_demo.queue().launch()
 
 
4
  import sys
5
  import copy
6
  import pathlib
7
+ from fastapi import FastAPI, Request
8
+ from fastapi.templating import Jinja2Templates
9
+ import uvicorn
10
+ from gradio.utils import get_space
11
 
12
  os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
13
 
14
  demo_dir = pathlib.Path(__file__).parent / "demos"
15
 
16
 
17
+ app = FastAPI()
18
+
19
+ templates = Jinja2Templates(directory="templates")
20
+
21
+ names = sorted(os.listdir("./demos"))
22
+
23
+
24
+ @app.get("/")
25
+ def index(request: Request):
26
+ names = [[p[0], p[2]] for p in all_demos]
27
+ return templates.TemplateResponse("index.html", {"request": request, "names": names,
28
+ "initial_demo": names[0][0], "is_space": get_space()})
29
+
30
+
31
  all_demos = []
32
  demo_module = None
33
  for p in sorted(os.listdir("./demos")):
 
38
  demo_module = importlib.import_module(f"run")
39
  else:
40
  demo_module = importlib.reload(demo_module)
41
+ all_demos.append((p, demo_module.demo.queue(), False))
42
  except Exception as e:
 
43
  with gr.Blocks() as demo:
44
  gr.Markdown(f"Error loading demo: {e}")
45
+ all_demos.append((p, demo, True))
46
+
47
+ for demo_name, demo, _ in all_demos:
48
+ app = gr.mount_gradio_app(app, demo, f"/demo/{demo_name}")
49
 
 
 
 
 
50
 
51
+ if __name__ == "__main__":
52
+ uvicorn.run(app, port=7860, host="0.0.0.0")
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
 
2
- gradio-client @ git+https://github.com/gradio-app/gradio@42cbe015a3da385b2d8a83838d52b4cfe9fcdb54#subdirectory=client/python
3
- https://gradio-builds.s3.amazonaws.com/42cbe015a3da385b2d8a83838d52b4cfe9fcdb54/gradio-4.8.0-py3-none-any.whl
4
  pypistats==1.1.0
5
  plotly==5.10.0
6
  opencv-python==4.6.0.66
@@ -8,5 +8,3 @@ transformers==4.21.1
8
  torch==1.12.1
9
  altair
10
  vega_datasets
11
- pydantic==2.1.1
12
- pydantic_core==2.4.0
 
1
 
2
+ gradio-client @ git+https://github.com/gradio-app/gradio@40c01c121762aed57d4dd8220efb5ebf7b12be4b#subdirectory=client/python
3
+ https://gradio-builds.s3.amazonaws.com/40c01c121762aed57d4dd8220efb5ebf7b12be4b/gradio-4.8.0-py3-none-any.whl
4
  pypistats==1.1.0
5
  plotly==5.10.0
6
  opencv-python==4.6.0.66
 
8
  torch==1.12.1
9
  altair
10
  vega_datasets
 
 
templates/index.html ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ {%if is_space %}
5
+ <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
6
+ {% endif %}
7
+ <style>
8
+ /* Reset some default styles */
9
+ body, h1, h2, h3, p, ul {
10
+ margin: 0;
11
+ padding: 0;
12
+ }
13
+
14
+ body {
15
+ font-family: 'Arial', sans-serif;
16
+ overflow-y: hidden;
17
+ }
18
+
19
+ .sidebar {
20
+ position: absolute;
21
+ top: 0;
22
+ left: 0;
23
+ height: 100vh;
24
+ max-height: calc(100vh - 50px);
25
+ width: 300px;
26
+ overflow-y: scroll;
27
+ overflow-x: hidden;
28
+ font-size: 0.875rem; /* 14px */
29
+ line-height: 1.25rem; /* 20px */
30
+ transition: width 0.3s ease;
31
+ }
32
+
33
+ .sidebar.collapsed {
34
+ width: 0;
35
+ }
36
+
37
+ .sidebar a {
38
+ display: block;
39
+ padding: 8px 16px;
40
+ text-decoration: none;
41
+ color: rgb(107 114 128);
42
+ }
43
+
44
+ .sidebar a:hover {
45
+ color: black;
46
+ transform: translateX(1px);
47
+ }
48
+
49
+ /* Apply a different style to the selected item in the sidebar */
50
+ .sidebar a.active {
51
+ background-color: rgb(251 146 60);
52
+ color: white;
53
+ border-radius: 0.75rem;
54
+ font-weight: bold;
55
+ }
56
+
57
+ /* Styling for the close button */
58
+ .close-btn {
59
+ cursor: pointer;
60
+ border: none;
61
+ background-color: white;
62
+ font-size: xx-large;
63
+ position: relative;
64
+ }
65
+
66
+ /* Styling for the content */
67
+ .content {
68
+ margin-left: 300px;
69
+ padding: 20px;
70
+ display: block;
71
+ height: 100vh;
72
+ transition: margin-left 0.3s ease;
73
+ }
74
+
75
+ .content.collapsed {
76
+ margin-left: 0;
77
+ }
78
+
79
+ /* Make the iframe responsive */
80
+ .content iframe {
81
+ width: 100%;
82
+ border: 0;
83
+ height: 100%;
84
+ }
85
+
86
+ @media only screen and (max-width: 600px) {
87
+ /* Adjust styles for smaller screens */
88
+ .sidebar {
89
+ width: 100%;
90
+ position: relative;
91
+ height: auto;
92
+ }
93
+
94
+ .content {
95
+ margin-left: 0;
96
+ }
97
+ }
98
+ </style>
99
+ <script src="//unpkg.com/alpinejs" defer></script>
100
+ </head>
101
+ <body x-data="{ current_demo: '{{ initial_demo }}', is_collapsed: false }">
102
+ <div style="display: flex; flex-direction: column;">
103
+ <div>
104
+ <button @click="is_collapsed = !is_collapsed" class="close-btn">
105
+ <a x-text="is_collapsed ? '➡️' : '⬅️'"></a>
106
+ </button>
107
+ </div>
108
+ <div :class="{ 'sidebar': true, 'collapsed': is_collapsed }" style="margin-top: 50px;">
109
+ {% for name in names %}
110
+ <a @click="current_demo = '{{ name[0] }}'" :class="current_demo == '{{ name[0] }}' ? 'active' : ''">{{ name[0] }} {% if name[1] %}❌{% endif %}</a>
111
+ {% endfor %}
112
+ </div>
113
+ </div>
114
+ <div :class="{ 'content': true, 'collapsed': is_collapsed }">
115
+ <iframe :src="`/demo/${current_demo}${document.location.search}`"></iframe>
116
+ </div>
117
+ </body>
118
+ </html>