Spaces:
Running
Running
Commit
·
9071ed9
1
Parent(s):
54bc5b6
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
|
|
4 |
from huggingface_hub import HfApi, update_repo_visibility
|
5 |
from slugify import slugify
|
6 |
import gradio as gr
|
|
|
7 |
import uuid
|
8 |
from typing import Optional
|
9 |
import json
|
@@ -42,7 +43,8 @@ def extract_info(json_data):
|
|
42 |
urls_to_download.append({
|
43 |
"url": image["url"],
|
44 |
"filename": os.path.basename(image["url"]),
|
45 |
-
"type": "imageName"
|
|
|
46 |
})
|
47 |
|
48 |
info = {
|
@@ -52,7 +54,8 @@ def extract_info(json_data):
|
|
52 |
"name": json_data["name"],
|
53 |
"description": json_data["description"],
|
54 |
"trainedWords": model_version["trainedWords"],
|
55 |
-
"creator": json_data["creator"]["username"]
|
|
|
56 |
}
|
57 |
return info
|
58 |
return None
|
@@ -60,11 +63,15 @@ def extract_info(json_data):
|
|
60 |
def download_files(info, folder="."):
|
61 |
downloaded_files = {
|
62 |
"imageName": [],
|
|
|
63 |
"weightName": []
|
64 |
}
|
65 |
for item in info["urls_to_download"]:
|
66 |
download_file(item["url"], item["filename"], folder)
|
67 |
downloaded_files[item["type"]].append(item["filename"])
|
|
|
|
|
|
|
68 |
return downloaded_files
|
69 |
|
70 |
def download_file(url, filename, folder="."):
|
@@ -98,17 +105,17 @@ def create_readme(info, downloaded_files, is_author=True, folder="."):
|
|
98 |
readme_content = ""
|
99 |
original_url = f"https://civitai.com/models/{info['id']}"
|
100 |
non_author_disclaimer = f'This model was originally uploaded on [CivitAI]({original_url}), by [{info["creator"]}](https://civitai.com/user/{info["creator"]}/models). The information below was provided by the author on CivitAI:'
|
|
|
|
|
|
|
|
|
|
|
101 |
content = f"""---
|
102 |
license: other
|
103 |
-
tags:
|
104 |
-
- text-to-image
|
105 |
-
- stable-diffusion
|
106 |
-
- lora
|
107 |
-
- diffusers
|
108 |
base_model: stabilityai/stable-diffusion-xl-base-1.0
|
109 |
instance_prompt: {info['trainedWords'][0] if 'trainedWords' in info and len(info['trainedWords']) > 0 else ''}
|
110 |
-
widget:
|
111 |
-
- text: {info['trainedWords'][0] if 'trainedWords' in info and len(info['trainedWords']) > 0 else ''}
|
112 |
---
|
113 |
|
114 |
# {info["name"]}
|
@@ -120,11 +127,11 @@ widget:
|
|
120 |
{info["description"]}
|
121 |
|
122 |
"""
|
123 |
-
for index, image in enumerate(downloaded_files["imageName"]):
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
readme_content += content + "\n"
|
129 |
|
130 |
with open(f"{folder}/README.md", "w") as file:
|
|
|
4 |
from huggingface_hub import HfApi, update_repo_visibility
|
5 |
from slugify import slugify
|
6 |
import gradio as gr
|
7 |
+
import re
|
8 |
import uuid
|
9 |
from typing import Optional
|
10 |
import json
|
|
|
43 |
urls_to_download.append({
|
44 |
"url": image["url"],
|
45 |
"filename": os.path.basename(image["url"]),
|
46 |
+
"type": "imageName",
|
47 |
+
"prompt": image["meta"]["prompt"]
|
48 |
})
|
49 |
|
50 |
info = {
|
|
|
54 |
"name": json_data["name"],
|
55 |
"description": json_data["description"],
|
56 |
"trainedWords": model_version["trainedWords"],
|
57 |
+
"creator": json_data["creator"]["username"],
|
58 |
+
"tags": json_data["tags"]
|
59 |
}
|
60 |
return info
|
61 |
return None
|
|
|
63 |
def download_files(info, folder="."):
|
64 |
downloaded_files = {
|
65 |
"imageName": [],
|
66 |
+
"imagePrompt": [],
|
67 |
"weightName": []
|
68 |
}
|
69 |
for item in info["urls_to_download"]:
|
70 |
download_file(item["url"], item["filename"], folder)
|
71 |
downloaded_files[item["type"]].append(item["filename"])
|
72 |
+
if(item["type"] == "imageName"):
|
73 |
+
prompt_clean = re.sub(r'<.*?>', '', item["prompt"])
|
74 |
+
downloaded_files["imagePrompt"].append(prompt_clean)
|
75 |
return downloaded_files
|
76 |
|
77 |
def download_file(url, filename, folder="."):
|
|
|
105 |
readme_content = ""
|
106 |
original_url = f"https://civitai.com/models/{info['id']}"
|
107 |
non_author_disclaimer = f'This model was originally uploaded on [CivitAI]({original_url}), by [{info["creator"]}](https://civitai.com/user/{info["creator"]}/models). The information below was provided by the author on CivitAI:'
|
108 |
+
default_tags = ["text-to-image", "stable-diffusion", "lora", "diffusers"]
|
109 |
+
civit_tags = [t for t in info["tags"] if t not in default_tags]
|
110 |
+
widget_prompts = "\n- text:".join(downloaded_files["imagePrompt"])
|
111 |
+
tags = default_tags + civit_tags
|
112 |
+
unpacked_tags = "\n-".join(tags)
|
113 |
content = f"""---
|
114 |
license: other
|
115 |
+
tags:{unpacked_tags}
|
|
|
|
|
|
|
|
|
116 |
base_model: stabilityai/stable-diffusion-xl-base-1.0
|
117 |
instance_prompt: {info['trainedWords'][0] if 'trainedWords' in info and len(info['trainedWords']) > 0 else ''}
|
118 |
+
widget:{widget_prompts}
|
|
|
119 |
---
|
120 |
|
121 |
# {info["name"]}
|
|
|
127 |
{info["description"]}
|
128 |
|
129 |
"""
|
130 |
+
for index, (image, prompt) in enumerate(zip(downloaded_files["imageName"], downloaded_files["imagePrompt"])):
|
131 |
+
if index == 1:
|
132 |
+
content += f"## Image examples for the model:\n![Image {index}]({image})\n> Prompt: {prompt}"
|
133 |
+
elif index > 1:
|
134 |
+
content += f"\n![Image {index}]({image})\n> Prompt: {prompt}"
|
135 |
readme_content += content + "\n"
|
136 |
|
137 |
with open(f"{folder}/README.md", "w") as file:
|