Add the ability to set the tag --trust_remote_code to true

#19
Files changed (1) hide show
  1. app.py +40 -17
app.py CHANGED
@@ -92,23 +92,43 @@ class ModelConverter:
92
  extracted_folder = next(Path(tmp_dir).iterdir())
93
  extracted_folder.rename(self.config.repo_path)
94
 
95
- def convert_model(self, input_model_id: str) -> Tuple[bool, Optional[str]]:
96
  """Convert the model to ONNX format."""
97
  try:
98
- result = subprocess.run(
99
- [
100
- sys.executable,
101
- "-m",
102
- "scripts.convert",
103
- "--quantize",
104
- "--model_id",
105
- input_model_id,
106
- ],
107
- cwd=self.config.repo_path,
108
- capture_output=True,
109
- text=True,
110
- env={},
111
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  if result.returncode != 0:
114
  return False, result.stderr
@@ -177,6 +197,9 @@ def main():
177
  type="password",
178
  key="user_hf_token",
179
  )
 
 
 
180
 
181
  if config.hf_username == input_model_id.split("/")[0]:
182
  same_repo = st.checkbox(
@@ -206,7 +229,7 @@ def main():
206
  return
207
 
208
  with st.spinner("Converting model..."):
209
- success, stderr = converter.convert_model(input_model_id)
210
  if not success:
211
  st.error(f"Conversion failed: {stderr}")
212
  return
@@ -230,4 +253,4 @@ def main():
230
 
231
 
232
  if __name__ == "__main__":
233
- main()
 
92
  extracted_folder = next(Path(tmp_dir).iterdir())
93
  extracted_folder.rename(self.config.repo_path)
94
 
95
+ def convert_model(self, input_model_id: str, trust_remote_code=False) -> Tuple[bool, Optional[str]]:
96
  """Convert the model to ONNX format."""
97
  try:
98
+ if trust_remote_code:
99
+ if st.session_state.get("user_hf_token") != "":
100
+ result = subprocess.run(
101
+ [
102
+ sys.executable,
103
+ "-m",
104
+ "scripts.convert",
105
+ "--quantize",
106
+ "--trust_remote_code",
107
+ "--model_id",
108
+ input_model_id,
109
+ ],
110
+ cwd=self.config.repo_path,
111
+ capture_output=True,
112
+ text=True,
113
+ env={},
114
+ )
115
+ else:
116
+ raise Exception("Trust Remote Code requires your own HuggingFace token.")
117
+ else:
118
+ result = subprocess.run(
119
+ [
120
+ sys.executable,
121
+ "-m",
122
+ "scripts.convert",
123
+ "--quantize",
124
+ "--model_id",
125
+ input_model_id,
126
+ ],
127
+ cwd=self.config.repo_path,
128
+ capture_output=True,
129
+ text=True,
130
+ env={},
131
+ )
132
 
133
  if result.returncode != 0:
134
  return False, result.stderr
 
197
  type="password",
198
  key="user_hf_token",
199
  )
200
+ trust_remote_code = st.toggle("Trust Remote Code?")
201
+ if trust_remote_code:
202
+ st.warning("Remote code could be used for malicious purposes. Make sure you trust the code fully. You must use your own Hugging Face write token.")
203
 
204
  if config.hf_username == input_model_id.split("/")[0]:
205
  same_repo = st.checkbox(
 
229
  return
230
 
231
  with st.spinner("Converting model..."):
232
+ success, stderr = converter.convert_model(input_model_id, trust_remote_code=trust_remote_code)
233
  if not success:
234
  st.error(f"Conversion failed: {stderr}")
235
  return
 
253
 
254
 
255
  if __name__ == "__main__":
256
+ main()