anzorq commited on
Commit
760bfde
1 Parent(s): 4ad7983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -43
app.py CHANGED
@@ -1,56 +1,97 @@
1
  import subprocess
2
- from huggingface_hub import HfApi, Repository, hf_hub_download
3
  import gradio as gr
4
 
5
- subprocess.run(["git", "clone", "https://github.com/huggingface/diffusers.git", "diff"])
6
 
7
- def convert(token: str, model_id: str) -> str:
 
 
 
 
 
 
 
8
 
9
- # if token == "" or model_id == "":
10
- # return """
11
- # ### Invalid input 🐞
12
- # Please fill a token and model name.
13
- # """
14
  try:
15
- api = HfApi(token=token)
16
- # info = api.model_info(repo_id=model_id)
17
- #
18
- # # loop info siblings (type is RepoFile) and find the ones which rfilename ends with .ckpt, then return them
19
- # ckpt_files = [sibling for sibling in info.siblings if sibling.rfilename.endswith(".ckpt")]
20
- # if len(ckpt_files) == 0:
21
- # return f"#### Error: No checkpoint file found in the model repo."
22
- #
23
- # # return file names:
24
- # return "\n".join([f"- {ckpt_file.rfilename}" for ckpt_file in ckpt_files])
25
  api = HfApi()
26
- cpkt_path = hf_hub_download(repo_id="nitrosocke/Arcane-Diffusion", filename="arcane-diffusion-5k.ckpt")
27
- return cpkt_path
28
-
 
 
 
 
29
  except Exception as e:
30
- return f"#### Error: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
 
32
  with gr.Blocks() as demo:
33
 
34
  with gr.Row():
35
- with gr.Column(scale=50):
36
- gr.Markdown("DESCRIPTION")
37
-
38
- with gr.Column(scale=50):
39
- input_token = gr.Textbox(
40
- max_lines=1,
41
- label="Hugging Face token",
42
- )
43
- input_model = gr.Textbox(
44
- max_lines=1,
45
- label="Model name",
46
- placeholder="textattack/distilbert-base-cased-CoLA",
47
- )
48
-
49
- btn = gr.Button("Convert to Diffusers🧨")
50
- output = gr.Markdown(label="Output")
51
-
52
- btn.click(
53
- fn=convert, inputs=[input_token, input_model], outputs=output
 
 
 
 
 
 
 
 
 
 
54
  )
55
-
56
- demo.launch()
 
 
 
 
 
 
 
 
1
  import subprocess
2
+ from huggingface_hub import HfApi, hf_hub_download
3
  import gradio as gr
4
 
5
+ subprocess.run(["git", "clone", "https://github.com/huggingface/diffusers.git", "diffs"])
6
 
7
+ def error_str(error, title="Error"):
8
+ return f"""#### {title}
9
+ {error}"""
10
+
11
+ def url_to_model_id(model_id_str):
12
+ return model_id_str.split("/")[-2] + "/" + model_id_str.split("/")[-1] if model_id_str.startswith("https://huggingface.co/") else model_id_str
13
+
14
+ def get_ckpt_names(model_id = "nitrosocke/mo-di-diffusion"):
15
 
16
+ if model_id == "":
17
+ return error_str("Please enter a model name.", title="Invalid input"), None, None
18
+
 
 
19
  try:
 
 
 
 
 
 
 
 
 
 
20
  api = HfApi()
21
+ ckpt_files = [f for f in api.list_repo_files(url_to_model_id(model_id)) if f.endswith(".ckpt")]
22
+
23
+ if len(ckpt_files) == 0:
24
+ return error_str("No checkpoint files found in the model repo."), None, None
25
+
26
+ return None, gr.update(choices=ckpt_files, visible=True), gr.update(visible=True), "\n".join([f"- {ckpt_file.rfilename}" for ckpt_file in ckpt_files])
27
+
28
  except Exception as e:
29
+ return error_str(e), None, None
30
+
31
+ def convert(model_id, ckpt_name, token = "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"):
32
+
33
+ model_id = url_to_model_id(model_id)
34
+
35
+ # 1. Download the checkpoint file
36
+ ckpt_path = hf_hub_download(repo_id=model_id, filename=ckpt_name)
37
+
38
+ # 2. Run the conversion script
39
+ subprocess.run(
40
+ [
41
+ "python3",
42
+ "./diffs/scripts/convert_original_stable_diffusion_to_diffusers.py",
43
+ "--checkpoint_path",
44
+ ckpt_path,
45
+ "--dump_path" ,
46
+ model_id,
47
+ ]
48
+ )
49
+
50
+ # list files in current directory and return them as a list:
51
+ import os
52
+ return f"""files in current directory:
53
+ {[f for f in os.listdir(".") if os.path.isfile(f)]}"""
54
 
55
+
56
  with gr.Blocks() as demo:
57
 
58
  with gr.Row():
59
+
60
+ with gr.Column(scale=11):
61
+ with gr.Group():
62
+ gr.Markdown("## 1. Load model info")
63
+ input_token = gr.Textbox(
64
+ max_lines=1,
65
+ label="Hugging Face token",
66
+ placeholder="hf_...",
67
+ )
68
+ gr.Markdown("Get your token [here](https://huggingface.co/settings/tokens).")
69
+ input_model = gr.Textbox(
70
+ max_lines=1,
71
+ label="Model name or URL",
72
+ placeholder="username/model_name",
73
+ )
74
+
75
+ btn_get_ckpts = gr.Button("Load")
76
+
77
+ with gr.Column(scale=10, visible=False) as col_convert:
78
+ gr.Markdown("## 2. Convert to Diffusers🧨")
79
+ radio_ckpts = gr.Radio(label="Choose a checkpoint to convert", visible=False)
80
+ btn_convert = gr.Button("Convert")
81
+
82
+ error_output = gr.Markdown(label="Output")
83
+ btn_get_ckpts.click(
84
+ fn=get_ckpt_names,
85
+ inputs=[input_model],
86
+ outputs=[error_output, radio_ckpts, col_convert],
87
+ scroll_to_output=True
88
  )
89
+
90
+ btn_convert.click(
91
+ fn=convert,
92
+ inputs=[input_model, radio_ckpts, input_token],
93
+ outputs=error_output,
94
+ scroll_to_output=True
95
+ )
96
+
97
+ demo.launch(debug=True, share=True)