Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Set an environment variable
|
9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
10 |
+
|
11 |
+
TITLE = '''
|
12 |
+
<h1 style="text-align: center;">Meta Llama3.1 8B <a href="https://huggingface.co/spaces/ysharma/Chat_with_Meta_llama3_1_8b?duplicate=true" id="duplicate-button"><button style="color:white">Duplicate this Space</button></a></h1>
|
13 |
+
'''
|
14 |
+
|
15 |
+
DESCRIPTION = '''
|
16 |
+
<div>
|
17 |
+
<p>This Space demonstrates the instruction-tuned model <a href="https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct"><b>Meta Llama3.1 8b Chat</b></a>. Feel free to play with this demo, or duplicate to run privately!</p>
|
18 |
+
<p>🔨 Interested in trying out more powerful Instruct versions of Llama3.1? Check out the <a href="https://huggingface.co/chat/"><b>Hugging Chat</b></a> integration for 🐘 Meta Llama 3.1 70b, and 🦕 Meta Llama 3.1 405b</p>
|
19 |
+
<p>🔎 For more details about the Llama3.1 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>
|
20 |
+
</div>
|
21 |
+
'''
|
22 |
+
|
23 |
+
PLACEHOLDER = """
|
24 |
+
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
25 |
+
<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; ">
|
26 |
+
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3.1</h1>
|
27 |
+
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
|
28 |
+
</div>
|
29 |
+
"""
|
30 |
+
|
31 |
+
css = """
|
32 |
+
h1 {
|
33 |
+
text-align: center;
|
34 |
+
display: block;
|
35 |
+
display: flex;
|
36 |
+
align-items: center;
|
37 |
+
justify-content: center;
|
38 |
+
}
|
39 |
+
|
40 |
+
#duplicate-button {
|
41 |
+
margin-left: 10px;
|
42 |
+
color: white;
|
43 |
+
background: #1565c0;
|
44 |
+
border-radius: 100vh;
|
45 |
+
font-size: 1rem;
|
46 |
+
padding: 3px 5px;
|
47 |
+
}
|
48 |
+
"""
|
49 |
+
|
50 |
+
model = "llhf/Meta-Llama-3.1-8B-Instruct"
|
51 |
+
|
52 |
+
# Load the tokenizer and model
|
53 |
+
tokenizer = AutoTokenizer.from_pretrained(f"{model}")
|
54 |
+
model = AutoModelForCausalLM.from_pretrained(f"{model}", device_map="auto")
|
55 |
+
terminators = [
|
56 |
+
tokenizer.eos_token_id,
|
57 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
58 |
+
]
|
59 |
+
|
60 |
+
# Gradio inference function
|
61 |
+
@spaces.GPU(duration=120)
|
62 |
+
def chat_llama3_1_8b(message: str,
|
63 |
+
history: list,
|
64 |
+
temperature: float,
|
65 |
+
max_new_tokens: int
|
66 |
+
) -> str:
|
67 |
+
"""
|
68 |
+
Generate a streaming response using the llama3-8b model.
|
69 |
+
Args:
|
70 |
+
message (str): The input message.
|
71 |
+
history (list): The conversation history used by ChatInterface.
|
72 |
+
temperature (float): The temperature for generating the response.
|
73 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
74 |
+
Returns:
|
75 |
+
str: The generated response.
|
76 |
+
"""
|
77 |
+
conversation = []
|
78 |
+
|
79 |
+
# Gradio now supports the Messages API out of the box!
|
80 |
+
conversation.append({"role": "user", "content": message})
|
81 |
+
|
82 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
83 |
+
|
84 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
85 |
+
|
86 |
+
generate_kwargs = dict(
|
87 |
+
input_ids= input_ids,
|
88 |
+
streamer=streamer,
|
89 |
+
max_new_tokens=max_new_tokens,
|
90 |
+
do_sample=True,
|
91 |
+
temperature=temperature,
|
92 |
+
eos_token_id=terminators,
|
93 |
+
)
|
94 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
95 |
+
if temperature == 0:
|
96 |
+
generate_kwargs['do_sample'] = False
|
97 |
+
|
98 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
99 |
+
t.start()
|
100 |
+
|
101 |
+
outputs = []
|
102 |
+
for text in streamer:
|
103 |
+
outputs.append(text)
|
104 |
+
yield "".join(outputs)
|
105 |
+
|
106 |
+
|
107 |
+
# Gradio block
|
108 |
+
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
109 |
+
|
110 |
+
with gr.Blocks(fill_height=True, css=css) as demo:
|
111 |
+
|
112 |
+
gr.Markdown(TITLE)
|
113 |
+
gr.Markdown(DESCRIPTION)
|
114 |
+
#gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
115 |
+
gr.ChatInterface(
|
116 |
+
fn=chat_llama3_1_8b,
|
117 |
+
chatbot=chatbot,
|
118 |
+
type="messages",
|
119 |
+
fill_height=True,
|
120 |
+
examples_per_page=3,
|
121 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
122 |
+
additional_inputs=[
|
123 |
+
gr.Slider(minimum=0,
|
124 |
+
maximum=1,
|
125 |
+
step=0.1,
|
126 |
+
value=0.95,
|
127 |
+
label="Temperature",
|
128 |
+
render=False),
|
129 |
+
gr.Slider(minimum=128,
|
130 |
+
maximum=4096,
|
131 |
+
step=1,
|
132 |
+
value=512,
|
133 |
+
label="Max new tokens",
|
134 |
+
render=False ),
|
135 |
+
],
|
136 |
+
examples=[
|
137 |
+
["There's a llama in my garden 😱 What should I do?"],
|
138 |
+
["What is the best way to open a can of worms?"],
|
139 |
+
["The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. "],
|
140 |
+
['How to setup a human base on Mars? Give short answer.'],
|
141 |
+
['Explain theory of relativity to me like I’m 8 years old.'],
|
142 |
+
['What is 9,000 * 9,000?'],
|
143 |
+
['Write a pun-filled happy birthday message to my friend Alex.'],
|
144 |
+
['Justify why a penguin might make a good king of the jungle.']
|
145 |
+
],
|
146 |
+
cache_examples=False,
|
147 |
+
)
|
148 |
+
|
149 |
+
|
150 |
+
if __name__ == "__main__":
|
151 |
+
demo.launch()
|
152 |
+
|