Spaces:
Running
Running
charbelmalo
commited on
Commit
·
01c3275
1
Parent(s):
fc799e9
Update space
Browse files- app.py +207 -201
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,204 +1,210 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
(
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
f"🔄 Running Evaluation Queue ({len(running_eval_queue_df)})",
|
123 |
-
open=False,
|
124 |
-
):
|
125 |
-
with gr.Row():
|
126 |
-
running_eval_table = gr.components.Dataframe(
|
127 |
-
value=running_eval_queue_df,
|
128 |
-
headers=EVAL_COLS,
|
129 |
-
datatype=EVAL_TYPES,
|
130 |
-
row_count=5,
|
131 |
-
)
|
132 |
-
|
133 |
-
with gr.Accordion(
|
134 |
-
f"⏳ Pending Evaluation Queue ({len(pending_eval_queue_df)})",
|
135 |
-
open=False,
|
136 |
-
):
|
137 |
-
with gr.Row():
|
138 |
-
pending_eval_table = gr.components.Dataframe(
|
139 |
-
value=pending_eval_queue_df,
|
140 |
-
headers=EVAL_COLS,
|
141 |
-
datatype=EVAL_TYPES,
|
142 |
-
row_count=5,
|
143 |
-
)
|
144 |
-
with gr.Row():
|
145 |
-
gr.Markdown("# ✉️✨ Submit your model here!", elem_classes="markdown-text")
|
146 |
-
|
147 |
-
with gr.Row():
|
148 |
-
with gr.Column():
|
149 |
-
model_name_textbox = gr.Textbox(label="Model name")
|
150 |
-
revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
|
151 |
-
model_type = gr.Dropdown(
|
152 |
-
choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
|
153 |
-
label="Model type",
|
154 |
-
multiselect=False,
|
155 |
-
value=None,
|
156 |
-
interactive=True,
|
157 |
-
)
|
158 |
-
|
159 |
-
with gr.Column():
|
160 |
-
precision = gr.Dropdown(
|
161 |
-
choices=[i.value.name for i in Precision if i != Precision.Unknown],
|
162 |
-
label="Precision",
|
163 |
-
multiselect=False,
|
164 |
-
value="float16",
|
165 |
-
interactive=True,
|
166 |
-
)
|
167 |
-
weight_type = gr.Dropdown(
|
168 |
-
choices=[i.value.name for i in WeightType],
|
169 |
-
label="Weights type",
|
170 |
-
multiselect=False,
|
171 |
-
value="Original",
|
172 |
-
interactive=True,
|
173 |
-
)
|
174 |
-
base_model_name_textbox = gr.Textbox(label="Base model (for delta or adapter weights)")
|
175 |
-
|
176 |
-
submit_button = gr.Button("Submit Eval")
|
177 |
-
submission_result = gr.Markdown()
|
178 |
-
submit_button.click(
|
179 |
-
add_new_eval,
|
180 |
-
[
|
181 |
-
model_name_textbox,
|
182 |
-
base_model_name_textbox,
|
183 |
-
revision_name_textbox,
|
184 |
-
precision,
|
185 |
-
weight_type,
|
186 |
-
model_type,
|
187 |
-
],
|
188 |
-
submission_result,
|
189 |
-
)
|
190 |
|
|
|
191 |
with gr.Row():
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
# File to store data
|
6 |
+
DATA_FILE = 'data.json'
|
7 |
+
|
8 |
+
# Initialize data if file doesn't exist
|
9 |
+
if not os.path.exists(DATA_FILE):
|
10 |
+
with open(DATA_FILE, 'w') as f:
|
11 |
+
json.dump({'spaces': [], 'tags': []}, f)
|
12 |
+
|
13 |
+
# Helper functions to read and write data
|
14 |
+
def load_data():
|
15 |
+
with open(DATA_FILE, 'r') as f:
|
16 |
+
return json.load(f)
|
17 |
+
|
18 |
+
def save_data(data):
|
19 |
+
with open(DATA_FILE, 'w') as f:
|
20 |
+
json.dump(data, f, indent=4)
|
21 |
+
|
22 |
+
# Function to display spaces
|
23 |
+
def display_spaces(filter_tag=None, filter_status=None):
|
24 |
+
data = load_data()
|
25 |
+
spaces = data['spaces']
|
26 |
+
tags = data['tags']
|
27 |
+
|
28 |
+
# Apply filters if any
|
29 |
+
if filter_tag:
|
30 |
+
spaces = [s for s in spaces if filter_tag in s['tags']]
|
31 |
+
if filter_status and filter_status != 'All':
|
32 |
+
spaces = [s for s in spaces if s['status'] == filter_status]
|
33 |
+
|
34 |
+
# Generate HTML for spaces
|
35 |
+
html = ''
|
36 |
+
for space in spaces:
|
37 |
+
html += f'''
|
38 |
+
<div style="border:1px solid #ccc; padding:10px; margin:10px; width:300px; display:inline-block; vertical-align:top;">
|
39 |
+
<h3>{space['name']}</h3>
|
40 |
+
<p><strong>Status:</strong> {space['status']}</p>
|
41 |
+
<p><strong>Tags:</strong> {', '.join(space['tags'])}</p>
|
42 |
+
<p>{space['description']}</p>
|
43 |
+
<a href="{space['link']}" target="_blank">Open Link</a><br><br>
|
44 |
+
<button onclick="edit_space('{space['id']}')">Edit</button>
|
45 |
+
<button onclick="delete_space('{space['id']}')">Delete</button>
|
46 |
+
</div>
|
47 |
+
'''
|
48 |
+
if not html:
|
49 |
+
html = '<p>No spaces available.</p>'
|
50 |
+
return html
|
51 |
+
|
52 |
+
# Function to add/edit a space
|
53 |
+
def save_space(id, name, description, link, status, tags, parent_id):
|
54 |
+
data = load_data()
|
55 |
+
space = {
|
56 |
+
'id': id or str(len(data['spaces']) + 1),
|
57 |
+
'name': name,
|
58 |
+
'description': description,
|
59 |
+
'link': link,
|
60 |
+
'status': status,
|
61 |
+
'tags': tags.split(',') if tags else [],
|
62 |
+
'parent_id': parent_id
|
63 |
+
}
|
64 |
+
if id:
|
65 |
+
# Edit existing space
|
66 |
+
data['spaces'] = [s for s in data['spaces'] if s['id'] != id]
|
67 |
+
data['spaces'].append(space)
|
68 |
+
save_data(data)
|
69 |
+
return 'Space saved successfully.'
|
70 |
+
|
71 |
+
# Function to delete a space
|
72 |
+
def delete_space(id):
|
73 |
+
data = load_data()
|
74 |
+
data['spaces'] = [s for s in data['spaces'] if s['id'] != id]
|
75 |
+
save_data(data)
|
76 |
+
return 'Space deleted successfully.'
|
77 |
+
|
78 |
+
# Function to get space details for editing
|
79 |
+
def get_space_details(id):
|
80 |
+
data = load_data()
|
81 |
+
space = next((s for s in data['spaces'] if s['id'] == id), None)
|
82 |
+
if space:
|
83 |
+
return space['name'], space['description'], space['link'], space['status'], ','.join(space['tags']), space['parent_id']
|
84 |
+
return '', '', '', '', '', ''
|
85 |
+
|
86 |
+
# Function to manage tags
|
87 |
+
def add_tag(name, parent_name):
|
88 |
+
data = load_data()
|
89 |
+
tag = {
|
90 |
+
'name': name,
|
91 |
+
'parent_name': parent_name or None
|
92 |
+
}
|
93 |
+
data['tags'].append(tag)
|
94 |
+
save_data(data)
|
95 |
+
return 'Tag added successfully.'
|
96 |
+
|
97 |
+
# Function to get list of tags
|
98 |
+
def get_tags():
|
99 |
+
data = load_data()
|
100 |
+
return [tag['name'] for tag in data['tags']]
|
101 |
+
|
102 |
+
# Main Gradio interface
|
103 |
+
with gr.Blocks() as demo:
|
104 |
+
gr.Markdown("# Spaces Management App")
|
105 |
+
|
106 |
+
# Filters
|
107 |
+
with gr.Row():
|
108 |
+
filter_tag = gr.Dropdown(label='Filter by Tag', choices=[''] + get_tags())
|
109 |
+
filter_status = gr.Dropdown(label='Filter by Status', choices=['All', 'Running', 'Error'])
|
110 |
+
|
111 |
+
# Display spaces
|
112 |
+
spaces_html = gr.HTML()
|
113 |
+
|
114 |
+
def update_spaces(filter_tag, filter_status):
|
115 |
+
return display_spaces(filter_tag, filter_status)
|
116 |
+
|
117 |
+
filter_tag.change(fn=update_spaces, inputs=[filter_tag, filter_status], outputs=spaces_html)
|
118 |
+
filter_status.change(fn=update_spaces, inputs=[filter_tag, filter_status], outputs=spaces_html)
|
119 |
+
|
120 |
+
# Initial display
|
121 |
+
spaces_html.value = display_spaces()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
# Add space button
|
124 |
with gr.Row():
|
125 |
+
add_space_btn = gr.Button("Add Space")
|
126 |
+
|
127 |
+
# Modal for adding/editing space
|
128 |
+
with gr.Modal(title="Add/Edit Space") as modal:
|
129 |
+
space_id = gr.Textbox(visible=False)
|
130 |
+
space_name = gr.Textbox(label='Space Name')
|
131 |
+
space_description = gr.Textbox(label='Description', lines=3)
|
132 |
+
space_link = gr.Textbox(label='Link')
|
133 |
+
space_status = gr.Dropdown(label='Status', choices=['Running', 'Error'])
|
134 |
+
space_tags = gr.Textbox(label='Tags (comma separated)')
|
135 |
+
space_parent_id = gr.Textbox(label='Parent Space ID (optional)')
|
136 |
+
save_space_btn = gr.Button("Save Space")
|
137 |
+
|
138 |
+
def open_add_space():
|
139 |
+
return gr.update(visible=True), '', '', '', '', '', ''
|
140 |
+
|
141 |
+
add_space_btn.click(fn=open_add_space, outputs=[modal, space_id, space_name, space_description, space_link, space_status, space_tags])
|
142 |
+
|
143 |
+
def submit_space(space_id, space_name, space_description, space_link, space_status, space_tags, space_parent_id):
|
144 |
+
message = save_space(space_id, space_name, space_description, space_link, space_status, space_tags, space_parent_id)
|
145 |
+
html = display_spaces()
|
146 |
+
return gr.update(visible=False), html, gr.update(value=''), gr.update(value=''), gr.update(value=''), gr.update(value=''), gr.update(value=''), gr.update(value='')
|
147 |
+
|
148 |
+
save_space_btn.click(fn=submit_space, inputs=[space_id, space_name, space_description, space_link, space_status, space_tags, space_parent_id],
|
149 |
+
outputs=[modal, spaces_html, space_id, space_name, space_description, space_link, space_status, space_tags])
|
150 |
+
|
151 |
+
# JavaScript functions for edit/delete
|
152 |
+
gr.HTML('''
|
153 |
+
<script>
|
154 |
+
function edit_space(id) {
|
155 |
+
fetch('/get_space_details', {
|
156 |
+
method: 'POST',
|
157 |
+
headers: {'Content-Type': 'application/json'},
|
158 |
+
body: JSON.stringify({'id': id})
|
159 |
+
}).then(response => response.json()).then(data => {
|
160 |
+
space_id.value = id;
|
161 |
+
space_name.value = data[0];
|
162 |
+
space_description.value = data[1];
|
163 |
+
space_link.value = data[2];
|
164 |
+
space_status.value = data[3];
|
165 |
+
space_tags.value = data[4];
|
166 |
+
space_parent_id.value = data[5];
|
167 |
+
modal.style.display = 'block';
|
168 |
+
});
|
169 |
+
}
|
170 |
+
|
171 |
+
function delete_space(id) {
|
172 |
+
if (confirm('Are you sure you want to delete this space?')) {
|
173 |
+
fetch('/delete_space', {
|
174 |
+
method: 'POST',
|
175 |
+
headers: {'Content-Type': 'application/json'},
|
176 |
+
body: JSON.stringify({'id': id})
|
177 |
+
}).then(response => response.json()).then(data => {
|
178 |
+
alert(data);
|
179 |
+
location.reload();
|
180 |
+
});
|
181 |
+
}
|
182 |
+
}
|
183 |
+
</script>
|
184 |
+
''')
|
185 |
+
|
186 |
+
# End of Gradio Blocks
|
187 |
+
|
188 |
+
# Gradio routes for edit/delete functions
|
189 |
+
from fastapi import FastAPI, Request
|
190 |
+
import uvicorn
|
191 |
+
|
192 |
+
app = gr.mount_gradio_app(FastAPI(), demo)
|
193 |
+
|
194 |
+
@app.post("/get_space_details")
|
195 |
+
async def get_space_details_route(request: Request):
|
196 |
+
req = await request.json()
|
197 |
+
id = req.get('id')
|
198 |
+
details = get_space_details(id)
|
199 |
+
return details
|
200 |
+
|
201 |
+
@app.post("/delete_space")
|
202 |
+
async def delete_space_route(request: Request):
|
203 |
+
req = await request.json()
|
204 |
+
id = req.get('id')
|
205 |
+
message = delete_space(id)
|
206 |
+
return message
|
207 |
+
|
208 |
+
# Run the app
|
209 |
+
if __name__ == "__main__":
|
210 |
+
uvicorn.run(app, host="0.0.0.0")
|
requirements.txt
CHANGED
@@ -13,4 +13,6 @@ python-dateutil
|
|
13 |
tqdm
|
14 |
transformers
|
15 |
tokenizers>=0.15.0
|
16 |
-
sentencepiece
|
|
|
|
|
|
13 |
tqdm
|
14 |
transformers
|
15 |
tokenizers>=0.15.0
|
16 |
+
sentencepiece
|
17 |
+
uvicorn
|
18 |
+
fastapi
|