Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,142 +1,202 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os
|
3 |
import spaces
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
DESCRIPTION = '''
|
13 |
-
<div>
|
14 |
-
<h1 style="text-align: center;">Meta Llama3 8B</h1>
|
15 |
-
<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>
|
16 |
-
<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>
|
17 |
-
<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>
|
18 |
-
</div>
|
19 |
-
'''
|
20 |
-
|
21 |
-
LICENSE = """
|
22 |
-
<p/>
|
23 |
-
---
|
24 |
-
Built with Meta Llama 3
|
25 |
-
"""
|
26 |
-
|
27 |
-
PLACEHOLDER = """
|
28 |
-
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
29 |
-
<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; ">
|
30 |
-
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
|
31 |
-
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
|
32 |
-
</div>
|
33 |
-
"""
|
34 |
-
|
35 |
-
|
36 |
-
css = """
|
37 |
-
h1 {
|
38 |
-
text-align: center;
|
39 |
-
display: block;
|
40 |
-
}
|
41 |
-
#duplicate-button {
|
42 |
-
margin: auto;
|
43 |
-
color: white;
|
44 |
-
background: #1565c0;
|
45 |
-
border-radius: 100vh;
|
46 |
-
}
|
47 |
-
"""
|
48 |
-
|
49 |
-
# Load the tokenizer and model
|
50 |
-
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
|
51 |
-
model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto") # to("cuda:0")
|
52 |
-
terminators = [
|
53 |
-
tokenizer.eos_token_id,
|
54 |
-
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
55 |
-
]
|
56 |
-
|
57 |
-
@spaces.GPU(duration=120)
|
58 |
-
def chat_llama3_8b(message: str,
|
59 |
-
history: list,
|
60 |
-
temperature: float,
|
61 |
-
max_new_tokens: int
|
62 |
-
) -> str:
|
63 |
-
"""
|
64 |
-
Generate a streaming response using the llama3-8b model.
|
65 |
-
Args:
|
66 |
-
message (str): The input message.
|
67 |
-
history (list): The conversation history used by ChatInterface.
|
68 |
-
temperature (float): The temperature for generating the response.
|
69 |
-
max_new_tokens (int): The maximum number of new tokens to generate.
|
70 |
-
Returns:
|
71 |
-
str: The generated response.
|
72 |
-
"""
|
73 |
-
conversation = []
|
74 |
-
for user, assistant in history:
|
75 |
-
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
76 |
-
conversation.append({"role": "user", "content": message})
|
77 |
-
|
78 |
-
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
79 |
-
|
80 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
max_new_tokens=max_new_tokens,
|
86 |
-
do_sample=True,
|
87 |
-
temperature=temperature,
|
88 |
eos_token_id=terminators,
|
|
|
|
|
|
|
89 |
)
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
|
103 |
|
104 |
-
# Gradio block
|
105 |
-
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
106 |
|
107 |
-
with gr.Blocks(fill_height=True, css=css) as demo:
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
|
139 |
-
|
140 |
|
141 |
-
if __name__ == "__main__":
|
142 |
-
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import spaces
|
3 |
+
import torch
|
4 |
+
|
5 |
+
import transformers
|
6 |
+
import torch
|
7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
+
|
9 |
+
model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
pipeline = transformers.pipeline(
|
12 |
+
"text-generation",
|
13 |
+
model=model_name,
|
14 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
15 |
+
device="cpu",
|
16 |
+
)
|
17 |
+
|
18 |
+
def chat_function(message, history, system_prompt,max_new_tokens,temperature):
|
19 |
+
messages = [
|
20 |
+
{"role": "system", "content": system_prompt},
|
21 |
+
{"role": "user", "content": message},
|
22 |
+
]
|
23 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
24 |
+
messages,
|
25 |
+
tokenize=False,
|
26 |
+
add_generation_prompt=True
|
27 |
+
)
|
28 |
+
terminators = [
|
29 |
+
pipeline.tokenizer.eos_token_id,
|
30 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
31 |
+
]
|
32 |
+
temp = temperature + 0.1
|
33 |
+
outputs = pipeline(
|
34 |
+
prompt,
|
35 |
max_new_tokens=max_new_tokens,
|
|
|
|
|
36 |
eos_token_id=terminators,
|
37 |
+
do_sample=True,
|
38 |
+
temperature=temp,
|
39 |
+
top_p=0.9,
|
40 |
)
|
41 |
+
return outputs[0]["generated_text"][len(prompt):]
|
42 |
+
|
43 |
+
gr.ChatInterface(
|
44 |
+
chat_function,
|
45 |
+
chatbot=gr.Chatbot(height=400),
|
46 |
+
textbox=gr.Textbox(placeholder="Enter message here", container=False, scale=7),
|
47 |
+
title="Meta-Llama-3-8B-Instruct",
|
48 |
+
description="""
|
49 |
+
To Learn about Fine-tuning Llama-3-8B, Ckeck https://exnrt.com/blog/ai/finetune-llama3-8b/.
|
50 |
+
""",
|
51 |
+
additional_inputs=[
|
52 |
+
gr.Textbox("You are helpful AI.", label="System Prompt"),
|
53 |
+
gr.Slider(512, 4096, label="Max New Tokens"),
|
54 |
+
gr.Slider(0, 1, label="Temperature")
|
55 |
+
]
|
56 |
+
).launch()
|
57 |
+
|
58 |
+
|
59 |
+
#The Code
|
60 |
+
|
61 |
+
# import gradio as gr
|
62 |
+
# import os
|
63 |
+
# import spaces
|
64 |
+
# from transformers import GemmaTokenizer, AutoModelForCausalLM
|
65 |
+
# from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
66 |
+
# from threading import Thread
|
67 |
+
|
68 |
+
# # Set an environment variable
|
69 |
+
# HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
70 |
+
|
71 |
+
|
72 |
+
# DESCRIPTION = '''
|
73 |
+
# <div>
|
74 |
+
# <h1 style="text-align: center;">Meta Llama3 8B</h1>
|
75 |
+
# <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>
|
76 |
+
# <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>
|
77 |
+
# <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>
|
78 |
+
# </div>
|
79 |
+
# '''
|
80 |
+
|
81 |
+
# LICENSE = """
|
82 |
+
# <p/>
|
83 |
+
# ---
|
84 |
+
# Built with Meta Llama 3
|
85 |
+
# """
|
86 |
+
|
87 |
+
# PLACEHOLDER = """
|
88 |
+
# <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
89 |
+
# <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; ">
|
90 |
+
# <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
|
91 |
+
# <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
|
92 |
+
# </div>
|
93 |
+
# """
|
94 |
+
|
95 |
+
|
96 |
+
# css = """
|
97 |
+
# h1 {
|
98 |
+
# text-align: center;
|
99 |
+
# display: block;
|
100 |
+
# }
|
101 |
+
# #duplicate-button {
|
102 |
+
# margin: auto;
|
103 |
+
# color: white;
|
104 |
+
# background: #1565c0;
|
105 |
+
# border-radius: 100vh;
|
106 |
+
# }
|
107 |
+
# """
|
108 |
+
|
109 |
+
# # Load the tokenizer and model
|
110 |
+
# tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
|
111 |
+
# model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto") # to("cuda:0")
|
112 |
+
# terminators = [
|
113 |
+
# tokenizer.eos_token_id,
|
114 |
+
# tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
115 |
+
# ]
|
116 |
+
|
117 |
+
# @spaces.GPU(duration=120)
|
118 |
+
# def chat_llama3_8b(message: str,
|
119 |
+
# history: list,
|
120 |
+
# temperature: float,
|
121 |
+
# max_new_tokens: int
|
122 |
+
# ) -> str:
|
123 |
+
# """
|
124 |
+
# Generate a streaming response using the llama3-8b model.
|
125 |
+
# Args:
|
126 |
+
# message (str): The input message.
|
127 |
+
# history (list): The conversation history used by ChatInterface.
|
128 |
+
# temperature (float): The temperature for generating the response.
|
129 |
+
# max_new_tokens (int): The maximum number of new tokens to generate.
|
130 |
+
# Returns:
|
131 |
+
# str: The generated response.
|
132 |
+
# """
|
133 |
+
# conversation = []
|
134 |
+
# for user, assistant in history:
|
135 |
+
# conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
136 |
+
# conversation.append({"role": "user", "content": message})
|
137 |
+
|
138 |
+
# input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
139 |
+
|
140 |
+
# streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
141 |
+
|
142 |
+
# generate_kwargs = dict(
|
143 |
+
# input_ids= input_ids,
|
144 |
+
# streamer=streamer,
|
145 |
+
# max_new_tokens=max_new_tokens,
|
146 |
+
# do_sample=True,
|
147 |
+
# temperature=temperature,
|
148 |
+
# eos_token_id=terminators,
|
149 |
+
# )
|
150 |
+
# # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
151 |
+
# if temperature == 0:
|
152 |
+
# generate_kwargs['do_sample'] = False
|
153 |
|
154 |
+
# t = Thread(target=model.generate, kwargs=generate_kwargs)
|
155 |
+
# t.start()
|
156 |
+
|
157 |
+
# outputs = []
|
158 |
+
# for text in streamer:
|
159 |
+
# outputs.append(text)
|
160 |
+
# print(outputs)
|
161 |
+
# yield "".join(outputs)
|
162 |
|
163 |
|
164 |
+
# # Gradio block
|
165 |
+
# chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
166 |
|
167 |
+
# with gr.Blocks(fill_height=True, css=css) as demo:
|
168 |
|
169 |
+
# gr.Markdown(DESCRIPTION)
|
170 |
+
# gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
171 |
+
# gr.ChatInterface(
|
172 |
+
# fn=chat_llama3_8b,
|
173 |
+
# chatbot=chatbot,
|
174 |
+
# fill_height=True,
|
175 |
+
# additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
176 |
+
# additional_inputs=[
|
177 |
+
# gr.Slider(minimum=0,
|
178 |
+
# maximum=1,
|
179 |
+
# step=0.1,
|
180 |
+
# value=0.95,
|
181 |
+
# label="Temperature",
|
182 |
+
# render=False),
|
183 |
+
# gr.Slider(minimum=128,
|
184 |
+
# maximum=4096,
|
185 |
+
# step=1,
|
186 |
+
# value=512,
|
187 |
+
# label="Max new tokens",
|
188 |
+
# render=False ),
|
189 |
+
# ],
|
190 |
+
# examples=[
|
191 |
+
# ['How to setup a human base on Mars? Give short answer.'],
|
192 |
+
# ['Explain theory of relativity to me like I’m 8 years old.'],
|
193 |
+
# ['What is 9,000 * 9,000?'],
|
194 |
+
# ['Write a pun-filled happy birthday message to my friend Alex.']
|
195 |
+
# ],
|
196 |
+
# cache_examples=False,
|
197 |
+
# )
|
198 |
|
199 |
+
# gr.Markdown(LICENSE)
|
200 |
|
201 |
+
# if __name__ == "__main__":
|
202 |
+
# demo.launch()
|