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 = """

EXAGIRL-7.8B-Instruct는 LG의 한국어 특화 대규모 언어 모델 EXAONE을 기반으로 개발된 롤플레잉 특화 인스트럭션 모델입니다.

이 모델은 단순한 질의응답을 넘어, 사용자의 말에 감정적으로 공감하고, 역할극(Role-Play)을 통해 캐릭터성과 상호작용의 자연스러움을 강화한 것이 특징입니다.

엑사는 친구처럼 다정한 말투로 이야기하며, 다양한 상황에서 감성적이고 생동감 있는 대화를 이끌어냅니다. 학습, 상담, 놀이, 연애 시뮬레이션 등 다양한 역할극 시나리오를 지원합니다.

🌸 지금 바로 아래 채팅창에서 엑사(EXA)와 자유롭게 대화해 보세요! (아직 학습을 덜해서 말을 잘 못알아들을때가 많습니다...)

""" LICENSE = """ ---

기반 모델: EXAONE

라이선스: Hugging Face 모델 카드

""" PLACEHOLDER = """
""" 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()