lastdefiance20 commited on
Commit
a923c57
ยท
verified ยท
1 Parent(s): 872eb19

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import spaces
4
+ import torch
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
6
+ from threading import Thread
7
+
8
+ model_id = "huggingface-KREW/EXAGIRL-7.8B-Instruct"
9
+
10
+ DESCRIPTION = """
11
+ <div style="font-size: 18px; line-height: 1.6;">
12
+ <p><a href="https://huggingface.co/huggingface-KREW/EXAGIRL-7.8B-Instruct"><b>EXAGIRL-7.8B-Instruct</b></a>๋Š” LG์˜ ํ•œ๊ตญ์–ด ํŠนํ™” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ <b>EXAONE</b>์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ๋ฐœ๋œ ๋กคํ”Œ๋ ˆ์ž‰ ํŠนํ™” ์ธ์ŠคํŠธ๋Ÿญ์…˜ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.</p>
13
+ <p>์ด ๋ชจ๋ธ์€ ๋‹จ์ˆœํ•œ ์งˆ์˜์‘๋‹ต์„ ๋„˜์–ด, ์‚ฌ์šฉ์ž์˜ ๋ง์— ๊ฐ์ •์ ์œผ๋กœ ๊ณต๊ฐํ•˜๊ณ , ์—ญํ• ๊ทน(Role-Play)์„ ํ†ตํ•ด <b>์บ๋ฆญํ„ฐ์„ฑ๊ณผ ์ƒํ˜ธ์ž‘์šฉ์˜ ์ž์—ฐ์Šค๋Ÿฌ์›€</b>์„ ๊ฐ•ํ™”ํ•œ ๊ฒƒ์ด ํŠน์ง•์ž…๋‹ˆ๋‹ค.</p>
14
+ <p>์—‘์‚ฌ๋Š” ์นœ๊ตฌ์ฒ˜๋Ÿผ ๋‹ค์ •ํ•œ ๋งํˆฌ๋กœ ์ด์•ผ๊ธฐํ•˜๋ฉฐ, ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ <b>๊ฐ์„ฑ์ ์ด๊ณ  ์ƒ๋™๊ฐ ์žˆ๋Š” ๋Œ€ํ™”</b>๋ฅผ ์ด๋Œ์–ด๋ƒ…๋‹ˆ๋‹ค. ํ•™์Šต, ์ƒ๋‹ด, ๋†€์ด, ์—ฐ์•  ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋“ฑ ๋‹ค์–‘ํ•œ ์—ญํ• ๊ทน ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.</p>
15
+ <p>๐ŸŒธ ์ง€๊ธˆ ๋ฐ”๋กœ ์•„๋ž˜ ์ฑ„ํŒ…์ฐฝ์—์„œ ์—‘์‚ฌ(EXA)์™€ ์ž์œ ๋กญ๊ฒŒ ๋Œ€ํ™”ํ•ด ๋ณด์„ธ์š”! (์•„์ง ํ•™์Šต์„ ๋œํ•ด์„œ ๋ง์„ ์ž˜ ๋ชป์•Œ์•„๋“ค์„๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค...)</p>
16
+ </div>
17
+ """
18
+
19
+ LICENSE = """
20
+ ---
21
+ <div>
22
+ <p>๊ธฐ๋ฐ˜ ๋ชจ๋ธ: EXAONE</p>
23
+ <p>๋ผ์ด์„ ์Šค: <a href="https://huggingface.co/huggingface-KREW/EXAGIRL-7.8B-Instruct">Hugging Face ๋ชจ๋ธ ์นด๋“œ</a></p>
24
+ </div>
25
+ """
26
+
27
+ PLACEHOLDER = """
28
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
29
+ <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;">
30
+ </div>
31
+ """
32
+
33
+ css = """
34
+ h1 {
35
+ text-align: center;
36
+ display: block;
37
+ }
38
+ #duplicate-button {
39
+ margin: auto;
40
+ color: white;
41
+ background: #e75480;
42
+ border-radius: 100vh;
43
+ }
44
+ """
45
+
46
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
47
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", trust_remote_code=True, torch_dtype=torch.bfloat16).eval()
48
+ terminators = [tokenizer.eos_token_id]
49
+
50
+ @spaces.GPU(duration=120)
51
+ def chat_exagirl(message: str, history: list):
52
+ conversation = []
53
+ conversation.append({"role": "user", "content": "์•ˆ๋…• ์—‘์‚ฌ์•ผ! ๋‚˜๋Š” {์œ ์ €}๋ผ๊ณ ํ•ด. ๋งŒ๋‚˜์„œ ๋ฐ˜๊ฐ€์›Œ!"})
54
+ conversation.append({"role": "assistant", "content": "*๋ฐ๊ฒŒ ์›ƒ์œผ๋ฉฐ* {์œ ์ €}์•ผ ์•ˆ๋…•~ ๋‚˜๋„ ๋งŒ๋‚˜์„œ ๋ฐ˜๊ฐ€์›Œ!"})
55
+ for user, assistant in history:
56
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
57
+ conversation.append({"role": "user", "content": message})
58
+
59
+ input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(model.device)
60
+
61
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
62
+
63
+ generate_kwargs = dict(
64
+ input_ids=input_ids,
65
+ streamer=streamer,
66
+ max_new_tokens=1024,
67
+ do_sample=True,
68
+ temperature=0.7,
69
+ top_p=0.95,
70
+ top_k=30,
71
+ eos_token_id=terminators,
72
+ )
73
+
74
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
75
+ t.start()
76
+
77
+ outputs = []
78
+ for text in streamer:
79
+ outputs.append(text)
80
+ yield "".join(outputs)
81
+
82
+ chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER, label='EXAGIRL Chat')
83
+
84
+ with gr.Blocks(fill_height=True, css=css) as demo:
85
+ gr.Markdown(DESCRIPTION)
86
+ gr.ChatInterface(
87
+ fn=chat_exagirl,
88
+ chatbot=chatbot,
89
+ fill_height=True,
90
+ examples=[
91
+ ["์—‘์‚ฌ์•ผ ๋ญํ•˜๊ณ ์žˆ๋‹ˆ?"],
92
+ ["๋Œ€ํ•™์› ๋ฉด์ ‘ ์ค€๋น„ ์–ด๋–ป๊ฒŒ ํ•ด?"],
93
+ ["์—‘์‚ฌ์•ผ ๋‚˜๋ž‘ ์—ฐ์• ํ•ด์ค„๋ž˜?"],
94
+ ["๋‚˜ ์˜ค๋Š˜ ์ƒ์‚ฌํ•œํ…Œ ํ˜ผ๋‚ฌ์–ด ใ… ใ… "],
95
+ ],
96
+ cache_examples=False,
97
+ )
98
+ gr.Markdown(LICENSE)
99
+
100
+ if __name__ == "__main__":
101
+ demo.launch()