vodkaslime commited on
Commit
530fde7
·
unverified ·
1 Parent(s): a9dfa06

working prototype

Browse files
Files changed (2) hide show
  1. app.py +137 -12
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,16 +1,141 @@
1
  import os
2
  import shutil
 
3
  import streamlit as st
 
 
 
4
 
5
- model = st.text_input("Model name")
6
-
7
- with st.container():
8
- interence_mode = st.radio("Inference mode", ("causallm", "seq2seq"))
9
- b = st.button("Convert model")
10
- if b:
11
- total, used, free = shutil.disk_usage("/")
12
- st.write("The model name is: ", model)
13
- st.write("Total: %d GiB" % (total // (2**30)))
14
- st.write("Used: %d GiB" % (used // (2**30)))
15
- st.write("Free: %d GiB" % (free // (2**30)))
16
- st.write(os.getcwd())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import shutil
3
+ import subprocess
4
  import streamlit as st
5
+ import uuid
6
+ from git import Repo
7
+ import huggingface_hub
8
 
9
+ BACKEND_REPO_URL = "https://github.com/vodkaslime/ctranslate2-converter-backend"
10
+
11
+ HOME_DIR = os.path.expanduser("~")
12
+ BACKEND_DIR = os.path.join(HOME_DIR, "backend")
13
+ BACKEND_SCRIPT = os.path.join(BACKEND_DIR, "main.py")
14
+
15
+ MODEL_ROOT_DIR = os.path.join(HOME_DIR, "models")
16
+
17
+ st.title(":wave: CTranslate2 Model Converter")
18
+
19
+
20
+ @st.cache_resource
21
+ def init():
22
+ if os.path.exists(BACKEND_DIR):
23
+ return
24
+
25
+ try:
26
+ Repo.clone_from(BACKEND_REPO_URL, BACKEND_DIR)
27
+ subprocess.check_call(
28
+ [
29
+ "pip",
30
+ "install",
31
+ "-r",
32
+ os.path.join(BACKEND_DIR, "requirements.txt"),
33
+ ]
34
+ )
35
+ except Exception as e:
36
+ shutil.rmtree(BACKEND_DIR)
37
+ st.error(f"error initializing backend: {e}")
38
+
39
+
40
+ def convert_and_upload_model(
41
+ model,
42
+ output_dir,
43
+ inference_mode,
44
+ prompt_template,
45
+ huggingface_token,
46
+ upload_mode,
47
+ new_model,
48
+ ):
49
+ # Verify parameters
50
+ if not model:
51
+ st.error("Must provide a model name")
52
+ return
53
+
54
+ if not new_model:
55
+ st.error("Must provide a new model name where the conversion will upload to")
56
+ return
57
+
58
+ if not huggingface_token:
59
+ st.error("Must provide a huggingface token")
60
+ return
61
+
62
+ command = ["python", BACKEND_SCRIPT]
63
+ command += ["--model", model]
64
+ command += ["--output_dir", output_dir]
65
+ command += ["--inference_mode", inference_mode]
66
+ if prompt_template:
67
+ command += ["--prompt_template", prompt_template]
68
+
69
+ # Handle model conversion
70
+ try:
71
+ with st.spinner("Converting model"):
72
+ subprocess.check_call(command)
73
+ except subprocess.CalledProcessError as e:
74
+ st.error(f"Error converting model to ctranslate2 format: {e}")
75
+ return
76
+
77
+ st.success("Model successfully converted")
78
+
79
+ # Handle model upload
80
+ try:
81
+ with st.spinner("Uploading converted model"):
82
+ huggingface_hub.login(huggingface_token)
83
+ api = huggingface_hub.HfApi()
84
+ if upload_mode == "new repo":
85
+ api.create_repo(new_model)
86
+ api.upload_folder(folder_path=output_dir, repo_id=new_model)
87
+
88
+ except Exception as e:
89
+ st.error(f"Error uploading model: {e}")
90
+ return
91
+
92
+ st.success("Model successfully uploaded.")
93
+
94
+
95
+ def clean_up(output_dir):
96
+ try:
97
+ with st.spinner("Cleaning up"):
98
+ shutil.rmtree(output_dir)
99
+ except Exception as e:
100
+ st.error(f"Error removing work dir: {e}")
101
+ st.success("Cleaning up finished")
102
+
103
+
104
+ init()
105
+
106
+ model = st.text_input("Model name", placeholder="Salesforce/codet5p-220m")
107
+ inference_mode = st.radio(
108
+ "Inference mode",
109
+ ("causallm", "seq2seq"),
110
+ )
111
+ prompt_template = st.text_input("Prompt template")
112
+ huggingface_token = st.text_input(
113
+ "Hugging face token (must be writable token)", type="password"
114
+ )
115
+ upload_mode = st.radio(
116
+ "Choose if you want to create a new model repo or push a commit to existing repo",
117
+ ("new repo", "existing repo"),
118
+ )
119
+ new_model = st.text_input(
120
+ "The new model name that the model is going to be converted to",
121
+ placeholder="TabbyML/T5P-220M",
122
+ )
123
+ convert_button = st.button("Convert model", use_container_width=True)
124
+
125
+ if convert_button:
126
+ id = uuid.uuid4()
127
+ output_dir = os.path.join(MODEL_ROOT_DIR, str(id))
128
+
129
+ # Try converting and uploading model
130
+ convert_and_upload_model(
131
+ model,
132
+ output_dir,
133
+ inference_mode,
134
+ prompt_template,
135
+ huggingface_token,
136
+ upload_mode,
137
+ new_model,
138
+ )
139
+
140
+ # Clean up the conversion
141
+ clean_up(output_dir)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GitPython
2
+ huggingface-hub