Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 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 |
|
|
@@ -16,7 +15,6 @@ PLACEHOLDER = """
|
|
| 16 |
</div>
|
| 17 |
"""
|
| 18 |
|
| 19 |
-
|
| 20 |
css = """
|
| 21 |
h1 {
|
| 22 |
text-align: center;
|
|
@@ -31,89 +29,76 @@ border-radius: 100vh;
|
|
| 31 |
"""
|
| 32 |
|
| 33 |
# Load the tokenizer and model
|
| 34 |
-
#tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
|
| 35 |
-
#model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto") # to("cuda:0")
|
| 36 |
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
|
| 37 |
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", device_map="auto") # to("cuda:0")
|
|
|
|
| 38 |
terminators = [
|
| 39 |
-
tokenizer.eos_token_id,
|
| 40 |
-
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 41 |
]
|
| 42 |
|
| 43 |
@spaces.GPU(duration=120)
|
| 44 |
-
def chat_llama3_8b(
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
) -> str:
|
| 49 |
-
"""
|
| 50 |
-
Generate a streaming response using the llama3-8b model.
|
| 51 |
-
Args:
|
| 52 |
-
message (str): The input message.
|
| 53 |
-
history (list): The conversation history used by ChatInterface.
|
| 54 |
-
temperature (float): The temperature for generating the response.
|
| 55 |
-
max_new_tokens (int): The maximum number of new tokens to generate.
|
| 56 |
-
Returns:
|
| 57 |
-
str: The generated response.
|
| 58 |
-
"""
|
| 59 |
-
conversation = []
|
| 60 |
-
for user, assistant in history:
|
| 61 |
-
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
| 62 |
-
conversation.append({"role": "user", "content": message})
|
| 63 |
-
|
| 64 |
-
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
| 65 |
-
|
| 66 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 67 |
-
|
| 68 |
-
generate_kwargs = dict(
|
| 69 |
-
input_ids=
|
| 70 |
-
streamer=streamer,
|
| 71 |
-
max_new_tokens=max_new_tokens,
|
| 72 |
-
do_sample=True,
|
| 73 |
-
temperature=temperature,
|
| 74 |
-
eos_token_id=terminators,
|
| 75 |
-
)
|
| 76 |
-
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
| 77 |
-
if temperature == 0:
|
| 78 |
-
generate_kwargs['do_sample'] = False
|
| 79 |
-
|
| 80 |
-
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 81 |
-
t.start()
|
| 82 |
-
|
| 83 |
-
outputs = []
|
| 84 |
-
for text in streamer:
|
| 85 |
-
outputs.append(text)
|
| 86 |
-
|
| 87 |
-
yield "".join(outputs)
|
| 88 |
-
|
| 89 |
|
| 90 |
# Gradio block
|
| 91 |
-
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
| 92 |
|
| 93 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
| 94 |
-
|
| 95 |
-
gr.
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
gr.Slider(minimum=
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
label="Temperature",
|
| 107 |
-
render=False),
|
| 108 |
-
gr.Slider(minimum=128,
|
| 109 |
-
maximum=4096,
|
| 110 |
-
step=1,
|
| 111 |
-
value=512,
|
| 112 |
-
label="Max new tokens",
|
| 113 |
-
render=False ),
|
| 114 |
-
],
|
| 115 |
-
cache_examples=False,
|
| 116 |
-
)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import spaces
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 5 |
from threading import Thread
|
| 6 |
|
|
|
|
| 15 |
</div>
|
| 16 |
"""
|
| 17 |
|
|
|
|
| 18 |
css = """
|
| 19 |
h1 {
|
| 20 |
text-align: center;
|
|
|
|
| 29 |
"""
|
| 30 |
|
| 31 |
# Load the tokenizer and model
|
|
|
|
|
|
|
| 32 |
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
|
| 33 |
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", device_map="auto") # to("cuda:0")
|
| 34 |
+
|
| 35 |
terminators = [
|
| 36 |
+
tokenizer.eos_token_id,
|
| 37 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 38 |
]
|
| 39 |
|
| 40 |
@spaces.GPU(duration=120)
|
| 41 |
+
def chat_llama3_8b(
|
| 42 |
+
message: str,
|
| 43 |
+
history: list,
|
| 44 |
+
temperature: float,
|
| 45 |
+
max_new_tokens: int
|
| 46 |
) -> str:
|
| 47 |
+
"""
|
| 48 |
+
Generate a streaming response using the llama3-8b model.
|
| 49 |
+
Args:
|
| 50 |
+
message (str): The input message.
|
| 51 |
+
history (list): The conversation history used by ChatInterface.
|
| 52 |
+
temperature (float): The temperature for generating the response.
|
| 53 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
| 54 |
+
Returns:
|
| 55 |
+
str: The generated response.
|
| 56 |
+
"""
|
| 57 |
+
conversation = []
|
| 58 |
+
for user, assistant in history:
|
| 59 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
| 60 |
+
conversation.append({"role": "user", "content": message})
|
| 61 |
+
|
| 62 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
| 63 |
+
|
| 64 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 65 |
+
|
| 66 |
+
generate_kwargs = dict(
|
| 67 |
+
input_ids=input_ids,
|
| 68 |
+
streamer=streamer,
|
| 69 |
+
max_new_tokens=max_new_tokens,
|
| 70 |
+
do_sample=True,
|
| 71 |
+
temperature=temperature,
|
| 72 |
+
eos_token_id=terminators,
|
| 73 |
+
)
|
| 74 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
| 75 |
+
if temperature == 0:
|
| 76 |
+
generate_kwargs['do_sample'] = False
|
| 77 |
+
|
| 78 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 79 |
+
t.start()
|
| 80 |
+
|
| 81 |
+
outputs = []
|
| 82 |
+
for text in streamer:
|
| 83 |
+
outputs.append(text)
|
| 84 |
+
yield "".join(outputs)
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Gradio block
|
| 87 |
+
chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
| 88 |
|
| 89 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
| 90 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
| 91 |
+
gr.ChatInterface(
|
| 92 |
+
fn=chat_llama3_8b,
|
| 93 |
+
chatbot=chatbot,
|
| 94 |
+
fill_height=True,
|
| 95 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
| 96 |
+
additional_inputs=[
|
| 97 |
+
gr.Slider(minimum=0, maximum=1, step=0.1, value=0.95, label="Temperature", render=False),
|
| 98 |
+
gr.Slider(minimum=128, maximum=4096, step=1, value=512, label="Max new tokens", render=False),
|
| 99 |
+
],
|
| 100 |
+
cache_examples=False,
|
| 101 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
+
demo.launch()
|