Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import spaces | |
import torch | |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer | |
from threading import Thread | |
model_id = "huggingface-KREW/EXAGIRL-7.8B-Instruct" | |
DESCRIPTION = """ | |
<div style="font-size: 18px; line-height: 1.6;"> | |
<p><a href="https://huggingface.co/huggingface-KREW/EXAGIRL-7.8B-Instruct"><b>EXAGIRL-7.8B-Instruct</b></a>๋ LG์ ํ๊ตญ์ด ํนํ ๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ <b>EXAONE</b>์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ๋ฐ๋ ๋กคํ๋ ์ ํนํ ์ธ์คํธ๋ญ์ ๋ชจ๋ธ์ ๋๋ค.</p> | |
<p>์ด ๋ชจ๋ธ์ ๋จ์ํ ์ง์์๋ต์ ๋์ด, ์ฌ์ฉ์์ ๋ง์ ๊ฐ์ ์ ์ผ๋ก ๊ณต๊ฐํ๊ณ , ์ญํ ๊ทน(Role-Play)์ ํตํด <b>์บ๋ฆญํฐ์ฑ๊ณผ ์ํธ์์ฉ์ ์์ฐ์ค๋ฌ์</b>์ ๊ฐํํ ๊ฒ์ด ํน์ง์ ๋๋ค.</p> | |
<p>์์ฌ๋ ์น๊ตฌ์ฒ๋ผ ๋ค์ ํ ๋งํฌ๋ก ์ด์ผ๊ธฐํ๋ฉฐ, ๋ค์ํ ์ํฉ์์ <b>๊ฐ์ฑ์ ์ด๊ณ ์๋๊ฐ ์๋ ๋ํ</b>๋ฅผ ์ด๋์ด๋ ๋๋ค. ํ์ต, ์๋ด, ๋์ด, ์ฐ์ ์๋ฎฌ๋ ์ด์ ๋ฑ ๋ค์ํ ์ญํ ๊ทน ์๋๋ฆฌ์ค๋ฅผ ์ง์ํฉ๋๋ค.</p> | |
<p>๐ธ ์ง๊ธ ๋ฐ๋ก ์๋ ์ฑํ ์ฐฝ์์ ์์ฌ(EXA)์ ์์ ๋กญ๊ฒ ๋ํํด ๋ณด์ธ์! (์์ง ํ์ต์ ๋ํด์ ๋ง์ ์ ๋ชป์์๋ค์๋๊ฐ ๋ง์ต๋๋ค...)</p> | |
</div> | |
""" | |
LICENSE = """ | |
--- | |
<div> | |
<p>๊ธฐ๋ฐ ๋ชจ๋ธ: EXAONE</p> | |
<p>๋ผ์ด์ ์ค: <a href="https://huggingface.co/huggingface-KREW/EXAGIRL-7.8B-Instruct">Hugging Face ๋ชจ๋ธ ์นด๋</a></p> | |
</div> | |
""" | |
PLACEHOLDER = """ | |
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;"> | |
<img src="https://huggingface.co/huggingface-KREW/EXAGIRL-7.8B-Instruct/resolve/main/exagirl-logo.png" style="width: 60%; max-width: 480px; height: auto; opacity: 0.5;"> | |
</div> | |
""" | |
css = """ | |
h1 { | |
text-align: center; | |
display: block; | |
} | |
#duplicate-button { | |
margin: auto; | |
color: white; | |
background: #e75480; | |
border-radius: 100vh; | |
} | |
""" | |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) | |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", trust_remote_code=True, torch_dtype=torch.bfloat16).eval() | |
terminators = [tokenizer.eos_token_id] | |
def chat_exagirl(message: str, history: list): | |
conversation = [] | |
conversation.append({"role": "user", "content": "์๋ ์์ฌ์ผ! ๋๋ {์ ์ }๋ผ๊ณ ํด. ๋ง๋์ ๋ฐ๊ฐ์!"}) | |
conversation.append({"role": "assistant", "content": "*๋ฐ๊ฒ ์์ผ๋ฉฐ* {์ ์ }์ผ ์๋ ~ ๋๋ ๋ง๋์ ๋ฐ๊ฐ์!"}) | |
for user, assistant in history: | |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}]) | |
conversation.append({"role": "user", "content": message}) | |
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(model.device) | |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True) | |
generate_kwargs = dict( | |
input_ids=input_ids, | |
streamer=streamer, | |
max_new_tokens=1024, | |
do_sample=True, | |
temperature=0.7, | |
top_p=0.95, | |
top_k=30, | |
eos_token_id=terminators, | |
) | |
t = Thread(target=model.generate, kwargs=generate_kwargs) | |
t.start() | |
outputs = [] | |
for text in streamer: | |
outputs.append(text) | |
yield "".join(outputs) | |
chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER, label='EXAGIRL Chat') | |
with gr.Blocks(fill_height=True, css=css) as demo: | |
gr.Markdown(DESCRIPTION) | |
gr.ChatInterface( | |
fn=chat_exagirl, | |
chatbot=chatbot, | |
fill_height=True, | |
examples=[ | |
["์์ฌ์ผ ๋ญํ๊ณ ์๋?"], | |
["๋ํ์ ๋ฉด์ ์ค๋น ์ด๋ป๊ฒ ํด?"], | |
["์์ฌ์ผ ๋๋ ์ฐ์ ํด์ค๋?"], | |
["๋ ์ค๋ ์์ฌํํ ํผ๋ฌ์ด ใ ใ "], | |
], | |
cache_examples=False, | |
) | |
gr.Markdown(LICENSE) | |
if __name__ == "__main__": | |
demo.launch() |