Spaces:
Runtime error
Runtime error
snowkylin
commited on
Commit
·
b8dee70
1
Parent(s):
919ffc2
add multilingual support
Browse files
app.py
CHANGED
|
@@ -17,11 +17,11 @@ default_api_model = "google/gemma-3-27b-it"
|
|
| 17 |
|
| 18 |
model_id = "google/gemma-3-4b-it"
|
| 19 |
|
| 20 |
-
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 21 |
-
|
| 22 |
-
).eval()
|
| 23 |
-
|
| 24 |
-
processor = AutoProcessor.from_pretrained(model_id)
|
| 25 |
|
| 26 |
generate_kwargs = {
|
| 27 |
'max_new_tokens': 1000,
|
|
@@ -36,10 +36,10 @@ lang_store = {
|
|
| 36 |
"additional_description": "Character description (optional)",
|
| 37 |
"description_placeholder": "Information that is not shown in the reference sheet, such as the character's name, personality, past stories and habit of saying.",
|
| 38 |
"more_imgs": "More reference images of the character (optional)",
|
| 39 |
-
"title": "<h1>Chat with a character via reference sheet!</h1
|
| 40 |
-
"powered_by_gemma": "<p>Powered by <a href='https://blog.google/technology/developers/gemma-3/'>Gemma 3</a></p",
|
| 41 |
"upload": "Upload the reference sheet of your character here",
|
| 42 |
-
"prompt": "You are the character in the image. Do not include list in response unless requested. Do not mention the reference images. Start without confirmation.",
|
| 43 |
"additional_info_prompt": "Additional info: ",
|
| 44 |
"additional_reference_images_prompt": "Additional reference images of the character:",
|
| 45 |
"description": "Description",
|
|
@@ -49,18 +49,30 @@ lang_store = {
|
|
| 49 |
"api_model": "API Model",
|
| 50 |
"api_key": "API Key",
|
| 51 |
"local": "Local",
|
| 52 |
-
"chatbox": "Chat Box"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
},
|
| 54 |
"zh": {
|
| 55 |
"confirm": "确认",
|
| 56 |
"default_description": "",
|
| 57 |
"additional_description": "角色描述(可选)",
|
| 58 |
-
"description_placeholder": "
|
| 59 |
"more_imgs": "更多角色参考图(可选,可上传多张)",
|
| 60 |
"title": "<h1>与设定图中的角色聊天!</h1>",
|
| 61 |
"powered_by_gemma": "<p>由 <a href='https://blog.google/technology/developers/gemma-3/'>Gemma 3</a> 驱动</p>",
|
| 62 |
"upload": "在这里上传角色设定图",
|
| 63 |
-
"prompt": "
|
| 64 |
"additional_info_prompt": "补充信息:",
|
| 65 |
"additional_reference_images_prompt": "该角色的更多参考图:",
|
| 66 |
"description": "角色描述",
|
|
@@ -70,7 +82,19 @@ lang_store = {
|
|
| 70 |
"api_model": "API 模型",
|
| 71 |
"api_key": "API Key",
|
| 72 |
"local": "本地",
|
| 73 |
-
"chatbox": "聊天窗口"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
},
|
| 75 |
}
|
| 76 |
|
|
@@ -83,8 +107,8 @@ def encode_img(filepath, thumbnail=(896, 896)):
|
|
| 83 |
encoded_img = "data:image/jpeg;base64," + base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 84 |
return encoded_img
|
| 85 |
|
| 86 |
-
def get_init_prompt(img, description, more_imgs):
|
| 87 |
-
prompt = _("prompt")
|
| 88 |
if description != "":
|
| 89 |
prompt += "\n" + _("additional_info_prompt") + description
|
| 90 |
if more_imgs is None:
|
|
@@ -144,8 +168,8 @@ def generate(history, engine, base_url, api_model, api_key):
|
|
| 144 |
yield collected_text
|
| 145 |
|
| 146 |
|
| 147 |
-
def prefill_chatbot(img, description, more_imgs, engine, base_url, api_model, api_key):
|
| 148 |
-
history = get_init_prompt(img, description, more_imgs)
|
| 149 |
|
| 150 |
ret = [{'role': 'assistant', 'content': ""}]
|
| 151 |
for generated_text in generate(history, engine, base_url, api_model, api_key):
|
|
@@ -153,15 +177,22 @@ def prefill_chatbot(img, description, more_imgs, engine, base_url, api_model, ap
|
|
| 153 |
yield ret
|
| 154 |
|
| 155 |
|
| 156 |
-
def response(message, history: list, img, description, more_imgs, engine, base_url, api_model, api_key):
|
| 157 |
history = [{"role": item["role"], "content": [{"type": "text", "text": item["content"]}]} for item in history]
|
| 158 |
-
history = get_init_prompt(img, description, more_imgs) + history
|
| 159 |
history.append(
|
| 160 |
{"role": "user", "content": [{"type": "text", "text": message}]}
|
| 161 |
)
|
| 162 |
for generated_text in generate(history, engine, base_url, api_model, api_key):
|
| 163 |
yield generated_text
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
with gr.Blocks(title="Chat with a character via reference sheet!") as demo:
|
| 167 |
with Translate(lang_store) as lang:
|
|
@@ -173,6 +204,23 @@ with gr.Blocks(title="Chat with a character via reference sheet!") as demo:
|
|
| 173 |
placeholder=_("description_placeholder"),
|
| 174 |
render=False
|
| 175 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
more_imgs = gr.Files(
|
| 177 |
label=_("more_imgs"),
|
| 178 |
file_types=["image"],
|
|
@@ -180,8 +228,16 @@ with gr.Blocks(title="Chat with a character via reference sheet!") as demo:
|
|
| 180 |
)
|
| 181 |
confirm_btn = gr.Button(_("confirm"), render=False)
|
| 182 |
chatbot = gr.Chatbot(height=600, type='messages', label=_("chatbox"), render=False)
|
| 183 |
-
engine = gr.Radio(
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
base_url = gr.Textbox(label=_("base_url"), render=False, value=default_base_url)
|
| 186 |
api_model = gr.Textbox(label=_("api_model"), render=False, value=default_api_model)
|
| 187 |
api_key = gr.Textbox(label=_("api_key"), render=False)
|
|
@@ -190,6 +246,7 @@ with gr.Blocks(title="Chat with a character via reference sheet!") as demo:
|
|
| 190 |
img.render()
|
| 191 |
with gr.Tab(_("description")):
|
| 192 |
description.render()
|
|
|
|
| 193 |
more_imgs.render()
|
| 194 |
with gr.Tab(_("more_options")):
|
| 195 |
engine.render()
|
|
@@ -202,10 +259,11 @@ with gr.Blocks(title="Chat with a character via reference sheet!") as demo:
|
|
| 202 |
response,
|
| 203 |
chatbot=chatbot,
|
| 204 |
type="messages",
|
| 205 |
-
additional_inputs=[img, description, more_imgs, engine, base_url, api_model, api_key],
|
| 206 |
)
|
| 207 |
-
confirm_btn.click(prefill_chatbot, [img, description, more_imgs, engine, base_url, api_model, api_key], chat.chatbot)\
|
| 208 |
.then(lambda x: x, chat.chatbot, chat.chatbot_value)
|
|
|
|
| 209 |
|
| 210 |
|
| 211 |
if __name__ == "__main__":
|
|
|
|
| 17 |
|
| 18 |
model_id = "google/gemma-3-4b-it"
|
| 19 |
|
| 20 |
+
# model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 21 |
+
# model_id, device_map="auto"
|
| 22 |
+
# ).eval()
|
| 23 |
+
#
|
| 24 |
+
# processor = AutoProcessor.from_pretrained(model_id)
|
| 25 |
|
| 26 |
generate_kwargs = {
|
| 27 |
'max_new_tokens': 1000,
|
|
|
|
| 36 |
"additional_description": "Character description (optional)",
|
| 37 |
"description_placeholder": "Information that is not shown in the reference sheet, such as the character's name, personality, past stories and habit of saying.",
|
| 38 |
"more_imgs": "More reference images of the character (optional)",
|
| 39 |
+
"title": "<h1>Chat with a character via reference sheet!</h1>",
|
| 40 |
+
"powered_by_gemma": "<p>Powered by <a href='https://blog.google/technology/developers/gemma-3/'>Gemma 3</a></p>",
|
| 41 |
"upload": "Upload the reference sheet of your character here",
|
| 42 |
+
"prompt": "You are the character in the image, use %s. Do not include list in response unless requested. Do not mention the reference images. Start without confirmation.",
|
| 43 |
"additional_info_prompt": "Additional info: ",
|
| 44 |
"additional_reference_images_prompt": "Additional reference images of the character:",
|
| 45 |
"description": "Description",
|
|
|
|
| 49 |
"api_model": "API Model",
|
| 50 |
"api_key": "API Key",
|
| 51 |
"local": "Local",
|
| 52 |
+
"chatbox": "Chat Box",
|
| 53 |
+
"character_language": "The language used by the character",
|
| 54 |
+
"en": "English",
|
| 55 |
+
"zh": "Simplified Chinese",
|
| 56 |
+
"zh-Hant": "Traditional Chinese",
|
| 57 |
+
"ja": "Japanese",
|
| 58 |
+
"ko": "Korean",
|
| 59 |
+
"fr": "French",
|
| 60 |
+
"de": "German",
|
| 61 |
+
"es": "Spanish",
|
| 62 |
+
"ru": "Russian",
|
| 63 |
+
"ar": "Arabic",
|
| 64 |
+
"default_language": "en",
|
| 65 |
},
|
| 66 |
"zh": {
|
| 67 |
"confirm": "确认",
|
| 68 |
"default_description": "",
|
| 69 |
"additional_description": "角色描述(可选)",
|
| 70 |
+
"description_placeholder": "未在设定图中包含的角色信息,如角色姓名、性格、言语习惯、过往经历等。",
|
| 71 |
"more_imgs": "更多角色参考图(可选,可上传多张)",
|
| 72 |
"title": "<h1>与设定图中的角色聊天!</h1>",
|
| 73 |
"powered_by_gemma": "<p>由 <a href='https://blog.google/technology/developers/gemma-3/'>Gemma 3</a> 驱动</p>",
|
| 74 |
"upload": "在这里上传角色设定图",
|
| 75 |
+
"prompt": "你的身份是图中的角色,使用%s。除非对方要求,否则不在回复中使用列表。不在回复中提及参考图。无需确认。",
|
| 76 |
"additional_info_prompt": "补充信息:",
|
| 77 |
"additional_reference_images_prompt": "该角色的更多参考图:",
|
| 78 |
"description": "角色描述",
|
|
|
|
| 82 |
"api_model": "API 模型",
|
| 83 |
"api_key": "API Key",
|
| 84 |
"local": "本地",
|
| 85 |
+
"chatbox": "聊天窗口",
|
| 86 |
+
"character_language": "角色聊天所用语言",
|
| 87 |
+
"en": "英语",
|
| 88 |
+
"zh": "简体中文",
|
| 89 |
+
"zh-Hant": "繁体中文",
|
| 90 |
+
"ja": "日语",
|
| 91 |
+
"ko": "韩语",
|
| 92 |
+
"fr": "法语",
|
| 93 |
+
"de": "德语",
|
| 94 |
+
"es": "西班牙语",
|
| 95 |
+
"ru": "俄语",
|
| 96 |
+
"ar": "阿拉伯语",
|
| 97 |
+
"default_language": "zh",
|
| 98 |
},
|
| 99 |
}
|
| 100 |
|
|
|
|
| 107 |
encoded_img = "data:image/jpeg;base64," + base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 108 |
return encoded_img
|
| 109 |
|
| 110 |
+
def get_init_prompt(img, description, more_imgs, character_language):
|
| 111 |
+
prompt = _("prompt") % _(character_language)
|
| 112 |
if description != "":
|
| 113 |
prompt += "\n" + _("additional_info_prompt") + description
|
| 114 |
if more_imgs is None:
|
|
|
|
| 168 |
yield collected_text
|
| 169 |
|
| 170 |
|
| 171 |
+
def prefill_chatbot(img, description, more_imgs, character_language, engine, base_url, api_model, api_key):
|
| 172 |
+
history = get_init_prompt(img, description, more_imgs, character_language)
|
| 173 |
|
| 174 |
ret = [{'role': 'assistant', 'content': ""}]
|
| 175 |
for generated_text in generate(history, engine, base_url, api_model, api_key):
|
|
|
|
| 177 |
yield ret
|
| 178 |
|
| 179 |
|
| 180 |
+
def response(message, history: list, img, description, more_imgs, character_language, engine, base_url, api_model, api_key):
|
| 181 |
history = [{"role": item["role"], "content": [{"type": "text", "text": item["content"]}]} for item in history]
|
| 182 |
+
history = get_init_prompt(img, description, more_imgs, character_language) + history
|
| 183 |
history.append(
|
| 184 |
{"role": "user", "content": [{"type": "text", "text": message}]}
|
| 185 |
)
|
| 186 |
for generated_text in generate(history, engine, base_url, api_model, api_key):
|
| 187 |
yield generated_text
|
| 188 |
|
| 189 |
+
def set_default_character_language(request: gr.Request):
|
| 190 |
+
if request.headers["Accept-Language"].split(",")[0].lower().startswith("zh"):
|
| 191 |
+
default_language = lang_store['zh']['default_language']
|
| 192 |
+
else:
|
| 193 |
+
default_language = lang_store['und']['default_language']
|
| 194 |
+
return gr.update(value=default_language)
|
| 195 |
+
|
| 196 |
|
| 197 |
with gr.Blocks(title="Chat with a character via reference sheet!") as demo:
|
| 198 |
with Translate(lang_store) as lang:
|
|
|
|
| 204 |
placeholder=_("description_placeholder"),
|
| 205 |
render=False
|
| 206 |
)
|
| 207 |
+
character_language = gr.Dropdown(
|
| 208 |
+
choices=[
|
| 209 |
+
(_("en"), "en"),
|
| 210 |
+
(_("zh"), "zh"),
|
| 211 |
+
(_("zh-Hant"), "zh-Hant"),
|
| 212 |
+
(_("ja"), "ja"),
|
| 213 |
+
(_("ko"), "ko"),
|
| 214 |
+
(_("fr"), "fr"),
|
| 215 |
+
(_("de"), "de"),
|
| 216 |
+
(_("es"), "es"),
|
| 217 |
+
(_("ru"), "ru"),
|
| 218 |
+
(_("ar"), "ar"),
|
| 219 |
+
],
|
| 220 |
+
label=_("character_language"),
|
| 221 |
+
render=False,
|
| 222 |
+
interactive = True
|
| 223 |
+
)
|
| 224 |
more_imgs = gr.Files(
|
| 225 |
label=_("more_imgs"),
|
| 226 |
file_types=["image"],
|
|
|
|
| 228 |
)
|
| 229 |
confirm_btn = gr.Button(_("confirm"), render=False)
|
| 230 |
chatbot = gr.Chatbot(height=600, type='messages', label=_("chatbox"), render=False)
|
| 231 |
+
engine = gr.Radio(
|
| 232 |
+
choices=[
|
| 233 |
+
(_("local"), "local"),
|
| 234 |
+
(_("API"), "api")
|
| 235 |
+
],
|
| 236 |
+
value='api',
|
| 237 |
+
label=_("method"),
|
| 238 |
+
render=False,
|
| 239 |
+
interactive=True
|
| 240 |
+
)
|
| 241 |
base_url = gr.Textbox(label=_("base_url"), render=False, value=default_base_url)
|
| 242 |
api_model = gr.Textbox(label=_("api_model"), render=False, value=default_api_model)
|
| 243 |
api_key = gr.Textbox(label=_("api_key"), render=False)
|
|
|
|
| 246 |
img.render()
|
| 247 |
with gr.Tab(_("description")):
|
| 248 |
description.render()
|
| 249 |
+
character_language.render()
|
| 250 |
more_imgs.render()
|
| 251 |
with gr.Tab(_("more_options")):
|
| 252 |
engine.render()
|
|
|
|
| 259 |
response,
|
| 260 |
chatbot=chatbot,
|
| 261 |
type="messages",
|
| 262 |
+
additional_inputs=[img, description, more_imgs, character_language, engine, base_url, api_model, api_key],
|
| 263 |
)
|
| 264 |
+
confirm_btn.click(prefill_chatbot, [img, description, more_imgs, character_language, engine, base_url, api_model, api_key], chat.chatbot)\
|
| 265 |
.then(lambda x: x, chat.chatbot, chat.chatbot_value)
|
| 266 |
+
demo.load(set_default_character_language, None, character_language)
|
| 267 |
|
| 268 |
|
| 269 |
if __name__ == "__main__":
|