alfredplpl commited on
Commit
a4e1be1
โ€ข
1 Parent(s): 6ea7c34

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -0
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import spaces
4
+ from transformers import GemmaTokenizer, AutoModelForCausalLM
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
6
+ from threading import Thread
7
+
8
+
9
+ DESCRIPTION = '''
10
+ <div>
11
+ <h1 style="text-align: center;">Meta Llama3 8B</h1>
12
+ <p>This Space demonstrates the instruction-tuned model <a href="https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct"><b>Meta Llama3 8b Chat</b></a>. Meta Llama3 is the new open LLM and comes in two sizes: 8b and 70b. Feel free to play with it, or duplicate to run privately!</p>
13
+ <p>๐Ÿ”Ž For more details about the Llama3 release and how to use the model with <code>transformers</code>, take a look <a href="https://huggingface.co/blog/llama3">at our blog post</a>.</p>
14
+ <p>๐Ÿฆ• Looking for an even more powerful model? Check out the <a href="https://huggingface.co/chat/"><b>Hugging Chat</b></a> integration for Meta Llama 3 70b</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
+ <img src="https://ysharma-dummy-chat-app.hf.space/file=/tmp/gradio/8e75e61cc9bab22b7ce3dec85ab0e6db1da5d107/Meta_lockup_positive%20primary_RGB.jpg" style="width: 80%; max-width: 550px; height: auto; opacity: 0.55; ">
28
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
29
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
30
+ </div>
31
+ """
32
+
33
+
34
+ css = """
35
+ h1 {
36
+ text-align: center;
37
+ display: block;
38
+ }
39
+
40
+ #duplicate-button {
41
+ margin: auto;
42
+ color: white;
43
+ background: #1565c0;
44
+ border-radius: 100vh;
45
+ }
46
+ """
47
+
48
+ # Load the tokenizer and model
49
+ tokenizer = AutoTokenizer.from_pretrained("alfredplpl/Llama-3-8B-Instruct-Ja")
50
+ model = AutoModelForCausalLM.from_pretrained("alfredplpl/Llama-3-8B-Instruct-Ja", device_map="auto")
51
+ terminators = [
52
+ tokenizer.eos_token_id,
53
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
54
+ ]
55
+
56
+ @spaces.GPU(duration=120)
57
+ def chat_llama3_8b(message: str,
58
+ history: list,
59
+ temperature: float,
60
+ max_new_tokens: int
61
+ ) -> str:
62
+ """
63
+ Generate a streaming response using the llama3-8b model.
64
+ Args:
65
+ message (str): The input message.
66
+ history (list): The conversation history used by ChatInterface.
67
+ temperature (float): The temperature for generating the response.
68
+ max_new_tokens (int): The maximum number of new tokens to generate.
69
+ Returns:
70
+ str: The generated response.
71
+ """
72
+ conversation = []
73
+ for user, assistant in history:
74
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
75
+ conversation.append({"role": "user", "content": message})
76
+
77
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
78
+
79
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
80
+
81
+ generate_kwargs = dict(
82
+ input_ids= input_ids,
83
+ streamer=streamer,
84
+ max_new_tokens=max_new_tokens,
85
+ do_sample=True,
86
+ temperature=temperature,
87
+ eos_token_id=terminators,
88
+ )
89
+ # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
90
+ if temperature == 0:
91
+ generate_kwargs['do_sample'] = False
92
+
93
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
94
+ t.start()
95
+
96
+ outputs = []
97
+ for text in streamer:
98
+ outputs.append(text)
99
+ print(outputs)
100
+ yield "".join(outputs)
101
+
102
+
103
+ # Gradio block
104
+ chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
105
+
106
+ with gr.Blocks(fill_height=True, css=css) as demo:
107
+
108
+ gr.Markdown(DESCRIPTION)
109
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
110
+ gr.ChatInterface(
111
+ fn=chat_llama3_8b,
112
+ chatbot=chatbot,
113
+ fill_height=True,
114
+ additional_inputs_accordion=gr.Accordion(label="โš™๏ธ Parameters", open=False, render=False),
115
+ additional_inputs=[
116
+ gr.Slider(minimum=0,
117
+ maximum=1,
118
+ step=0.1,
119
+ value=0.2,
120
+ label="Temperature",
121
+ render=False),
122
+ gr.Slider(minimum=128,
123
+ maximum=4096,
124
+ step=1,
125
+ value=256,
126
+ label="Max new tokens",
127
+ render=False ),
128
+ ],
129
+ examples=[
130
+ ['็ซๆ˜ŸใซๅŸบๅœฐใ‚’็ซ‹ใฆใ‚‹ๆ–นๆณ•ใ‚’ๆ•™ใˆใฆใใ ใ•ใ„ใ€‚'],
131
+ ['ๅฐๅญฆ็”Ÿใซใ‚‚ใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซ็›ธๅฏพๆ€ง็†่ซ–ใ‚’ๆ•™ใˆใฆใใ ใ•ใ„ใ€‚'],
132
+ ['๏ผ‘ๅ€‹300ๅ††ใ‚Šใ‚“ใ”ใ‚’5ใค่ฒทใ†ใจๅˆ่จˆใฏไฝ•ๅ††ใซใชใ‚Šใพใ™ใ‹๏ผŸ'],
133
+ ['ๅ‹้”ใฎ้™ฝ่‘ตใซ่ช•็”Ÿๆ—ฅใƒ—ใƒฌใ‚ผใƒณใƒˆ๏ฟฝ๏ฟฝ่€ƒใˆใฆใใ ใ•ใ„ใ€‚'],
134
+ ['ใƒšใƒณใ‚ฎใƒณใŒใ‚ธใƒฃใƒณใ‚ฐใƒซใฎ็Ž‹ๆง˜ใงใ‚ใ‚‹ใ“ใจใ‚’ๆญฃๅฝ“ๅŒ–ใ™ใ‚‹ใ‚ˆใ†ใช่ชฌๆ˜Žใ‚’ใ—ใฆใใ ใ•ใ„ใ€‚']
135
+ ],
136
+ cache_examples=False,
137
+ )
138
+
139
+ gr.Markdown(LICENSE)
140
+
141
+ if __name__ == "__main__":
142
+ demo.launch()
143
+