alfredplpl commited on
Commit
c94dddb
ยท
verified ยท
1 Parent(s): 53df471

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ref: https://huggingface.co/spaces/ysharma/Chat_with_Meta_llama3_8b
2
+
3
+ import gradio as gr
4
+ import os
5
+ import spaces
6
+ from transformers import GemmaTokenizer, AutoModelForCausalLM
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
8
+ from threading import Thread
9
+
10
+
11
+ DESCRIPTION = '''
12
+ <div>
13
+ <h1 style="text-align: center;">Sarashina-7B Instruct Test</h1>
14
+ <p>Sarashina-7B Instruct Testใ ใ‚ˆใ€‚ <a href="https://huggingface.co/alfredplpl/sarashina2-7b-it-test"><b>alfredplpl/sarashina2-7b-it-test</b></a>.</p>
15
+ </div>
16
+ '''
17
+
18
+ LICENSE = """
19
+ <p/>
20
+
21
+ ---
22
+ Built with Meta Llama 3
23
+ """
24
+
25
+ PLACEHOLDER = """
26
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
27
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Sarashina-7B Instruct Test</h1>
28
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">ใชใ‚“ใงใ‚‚ใใ„ใฆใญ</p>
29
+ </div>
30
+ """
31
+
32
+
33
+ css = """
34
+ h1 {
35
+ text-align: center;
36
+ display: block;
37
+ }
38
+
39
+ #duplicate-button {
40
+ margin: auto;
41
+ color: white;
42
+ background: #1565c0;
43
+ border-radius: 100vh;
44
+ }
45
+ """
46
+
47
+ # Load the tokenizer and model
48
+ tokenizer = AutoTokenizer.from_pretrained("alfredplpl/sarashina2-7b-it-test")
49
+ model = AutoModelForCausalLM.from_pretrained("alfredplpl/sarashina2-7b-it-test", device_map="auto")
50
+
51
+ @spaces.GPU()
52
+ def chat_llama3_8b(message: str,
53
+ history: list,
54
+ temperature: float,
55
+ max_new_tokens: int
56
+ ) -> str:
57
+ """
58
+ Generate a streaming response using the llama3-8b model.
59
+ Args:
60
+ message (str): The input message.
61
+ history (list): The conversation history used by ChatInterface.
62
+ temperature (float): The temperature for generating the response.
63
+ max_new_tokens (int): The maximum number of new tokens to generate.
64
+ Returns:
65
+ str: The generated response.
66
+ """
67
+ conversation = []
68
+ conversation.append({"role": "system", "content": "ใ‚ใชใŸใฏๆ—ฅๆœฌ่ชžใงๅ›ž็ญ”ใ™ใ‚‹AIใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใงใ™ใ€‚"})
69
+ for user, assistant in history:
70
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
71
+ conversation.append({"role": "user", "content": message})
72
+
73
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
74
+
75
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
76
+
77
+ generate_kwargs = dict(
78
+ input_ids= input_ids,
79
+ streamer=streamer,
80
+ max_new_tokens=max_new_tokens,
81
+ do_sample=True,
82
+ temperature=temperature,
83
+ top_p=0.95,
84
+ repetition_penalty=1.1,
85
+ eos_token_id=terminators,
86
+ )
87
+ # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
88
+ if temperature == 0:
89
+ generate_kwargs['do_sample'] = False
90
+
91
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
92
+ t.start()
93
+
94
+ outputs = []
95
+ for text in streamer:
96
+ outputs.append(text)
97
+ print(outputs)
98
+ yield "".join(outputs)
99
+
100
+
101
+ # Gradio block
102
+ chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
103
+
104
+ with gr.Blocks(fill_height=True, css=css) as demo:
105
+
106
+ gr.Markdown(DESCRIPTION)
107
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
108
+ gr.ChatInterface(
109
+ fn=chat_llama3_8b,
110
+ chatbot=chatbot,
111
+ fill_height=True,
112
+ additional_inputs_accordion=gr.Accordion(label="โš™๏ธ Parameters", open=False, render=False),
113
+ additional_inputs=[
114
+ gr.Slider(minimum=0,
115
+ maximum=1,
116
+ step=0.1,
117
+ value=0.2,
118
+ label="Temperature",
119
+ render=False),
120
+ gr.Slider(minimum=128,
121
+ maximum=4096,
122
+ step=1,
123
+ value=512,
124
+ label="Max new tokens",
125
+ render=False ),
126
+ ],
127
+ examples=[
128
+ ['ๅฐๅญฆ็”Ÿใซใ‚‚ใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซ็›ธๅฏพๆ€ง็†่ซ–ใ‚’ๆ•™ใˆใฆใใ ใ•ใ„ใ€‚'],
129
+ ['ๅฎ‡ๅฎ™ใฎ่ตทๆบใ‚’็Ÿฅใ‚‹ใŸใ‚ใฎๆ–นๆณ•ใ‚’ใ‚นใƒ†ใƒƒใƒ—ใƒปใƒใ‚คใƒปใ‚นใƒ†ใƒƒใƒ—ใงๆ•™ใˆใฆใใ ใ•ใ„ใ€‚'],
130
+ ['1ใ‹ใ‚‰100ใพใงใฎ็ด ๆ•ฐใ‚’ๆฑ‚ใ‚ใ‚‹ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’Pythonใงๆ›ธใ„ใฆใใ ใ•ใ„ใ€‚'],
131
+ ['ๅ‹้”ใฎ้™ฝ่‘ตใซใ‚ใ’ใ‚‹่ช•็”Ÿๆ—ฅใƒ—ใƒฌใ‚ผใƒณใƒˆใ‚’่€ƒใˆใฆใใ ใ•ใ„ใ€‚ใŸใ ใ—ใ€้™ฝ่‘ตใฏไธญๅญฆ็”Ÿใงใ€็งใฏๅŒใ˜ใ‚ฏใƒฉใ‚นใฎ็”ทๆ€งใงใ‚ใ‚‹ใ“ใจใ‚’่€ƒๆ…ฎใ—ใฆใใ ใ•ใ„ใ€‚'],
132
+ ['ใƒšใƒณใ‚ฎใƒณใŒใ‚ธใƒฃใƒณใ‚ฐใƒซใฎ็Ž‹ๆง˜ใงใ‚ใ‚‹ใ“ใจใ‚’ๆญฃๅฝ“ๅŒ–ใ™ใ‚‹ใ‚ˆใ†ใซ่ชฌๆ˜Žใ—ใฆใใ ใ•ใ„ใ€‚']
133
+ ],
134
+ cache_examples=False,
135
+ )
136
+
137
+ gr.Markdown(LICENSE)
138
+
139
+ if __name__ == "__main__":
140
+ demo.launch()
141
+