EXAGIRL-7.8B / app.py
lastdefiance20's picture
Create app.py
a923c57 verified
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]
@spaces.GPU(duration=120)
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()