Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import *
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
fs = HfFileSystem()
|
6 |
+
api = HfApi()
|
7 |
+
|
8 |
+
|
9 |
+
def remove_from(text, from_model, to_model):
|
10 |
+
text = text.replace(from_model, to_model)
|
11 |
+
return text
|
12 |
+
|
13 |
+
|
14 |
+
def return_operation_requests(from_model, to_model):
|
15 |
+
ls = [i['name'] for i in fs.ls(path=f'datasets/Weyaxi/requests/{from_model.split("/")[0]}') if from_model in i['name']]
|
16 |
+
liste=[]
|
17 |
+
|
18 |
+
for i in range(len(ls)):
|
19 |
+
path_for = ls[i]
|
20 |
+
will_write = json.loads(fs.read_text(path_for))
|
21 |
+
will_write['model'] = to_model
|
22 |
+
will_write = json.dumps(will_write)
|
23 |
+
|
24 |
+
liste.extend([CommitOperationAdd(path_in_repo="/".join(remove_from(path_for, from_model, to_model).split("/")[3:]), path_or_fileobj=will_write.encode()),
|
25 |
+
CommitOperationDelete(path_in_repo="/".join(path_for.split("/")[3:]))])
|
26 |
+
|
27 |
+
return liste
|
28 |
+
|
29 |
+
|
30 |
+
def return_operation_results(from_model, to_model):
|
31 |
+
ls = [i['name'] for i in fs.ls(path=f'datasets/Weyaxi/results/{from_model}') if from_model in i['name']]
|
32 |
+
liste=[]
|
33 |
+
|
34 |
+
for i in range(len(ls)):
|
35 |
+
path_for = ls[i]
|
36 |
+
|
37 |
+
|
38 |
+
will_write = json.loads(fs.read_text(path_for))
|
39 |
+
will_write['config_general']['model_name'] = to_model
|
40 |
+
will_write = json.dumps(will_write, indent=2)
|
41 |
+
|
42 |
+
liste.extend([CommitOperationAdd(path_in_repo="/".join(remove_from(path_for, from_model, to_model).split("/")[3:]), path_or_fileobj=will_write.encode()),
|
43 |
+
CommitOperationDelete(path_in_repo="/".join(path_for.split("/")[3:]))])
|
44 |
+
|
45 |
+
return liste
|
46 |
+
|
47 |
+
|
48 |
+
def commit(liste_requests, liste_results):
|
49 |
+
request_commit = (create_commit(repo_id="Weyaxi/requests", operations=liste_requests, commit_message=f"Renaming Models", repo_type="dataset", create_pr=True).__dict__['pr_url'])
|
50 |
+
result_commit = (create_commit(repo_id="Weyaxi/results", operations=liste_results, commit_message=f"Renaming Models", repo_type="dataset", create_pr=True).__dict__['pr_url'])
|
51 |
+
return request_commit, result_commit
|
52 |
+
|
53 |
+
|
54 |
+
def commit_gradio(from_model, to_model, hf_token):
|
55 |
+
try:
|
56 |
+
login(hf_token)
|
57 |
+
return commit(return_operation_requests(from_model, to_model), return_operation_results(from_model, to_model))
|
58 |
+
except Exception as e:
|
59 |
+
return e
|
60 |
+
|
61 |
+
|
62 |
+
demo = gr.Interface(fn=commit_gradio, inputs=["text", "text", "text"], outputs="text")
|
63 |
+
demo.launch()
|