Add application file
Browse files- app.py +166 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import spaces
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
5 |
+
import gradio as gr
|
6 |
+
from threading import Thread
|
7 |
+
|
8 |
+
model_id = os.environ.get("MODEL_ID")
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
model_id,
|
12 |
+
torch_dtype=torch.float16,
|
13 |
+
device_map="sequential",
|
14 |
+
offload_folder="offload", # Specify the offload folder
|
15 |
+
offload_state_dict=True # offload the state_dict as needed
|
16 |
+
)
|
17 |
+
|
18 |
+
TITLE = "<h1><center>Meta-Llama-3.1-70B-Instruct-AWQ-INT4</center></h1>"
|
19 |
+
|
20 |
+
PLACEHOLDER = """
|
21 |
+
<center>
|
22 |
+
<p>Hi! How can I help you today?</p>
|
23 |
+
</center>
|
24 |
+
"""
|
25 |
+
|
26 |
+
DESCRIPTION = """
|
27 |
+
<h3>MODEL: <a href="https://hf.co/hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4">Meta-Llama-3.1-70B-Instruct-AWQ-INT4</a></h3>
|
28 |
+
<center>
|
29 |
+
<p>This model is designed for conversational interactions.</p>
|
30 |
+
</center>
|
31 |
+
"""
|
32 |
+
|
33 |
+
CSS = """
|
34 |
+
.duplicate-button {
|
35 |
+
margin: auto !important;
|
36 |
+
color: white !important;
|
37 |
+
background: black !important;
|
38 |
+
border-radius: 100vh !important;
|
39 |
+
}
|
40 |
+
h3 {
|
41 |
+
text-align: center;
|
42 |
+
}
|
43 |
+
"""
|
44 |
+
|
45 |
+
device = "cuda" # for GPU usage or "cpu" for CPU usage
|
46 |
+
|
47 |
+
@spaces.GPU()
|
48 |
+
def stream_chat(
|
49 |
+
message: str,
|
50 |
+
history: list,
|
51 |
+
system_prompt: str,
|
52 |
+
temperature: float = 0.8,
|
53 |
+
max_new_tokens: int = 1024,
|
54 |
+
top_p: float = 1.0,
|
55 |
+
top_k: int = 20,
|
56 |
+
penalty: float = 1.2,
|
57 |
+
):
|
58 |
+
print(f'message: {message}')
|
59 |
+
print(f'history: {history}')
|
60 |
+
|
61 |
+
conversation = [
|
62 |
+
{"role": "system", "content": system_prompt}
|
63 |
+
]
|
64 |
+
for prompt, answer in history:
|
65 |
+
conversation.extend([
|
66 |
+
{"role": "user", "content": prompt},
|
67 |
+
{"role": "assistant", "content": answer},
|
68 |
+
])
|
69 |
+
|
70 |
+
conversation.append({"role": "user", "content": message})
|
71 |
+
|
72 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
73 |
+
|
74 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
75 |
+
|
76 |
+
generate_kwargs = dict(
|
77 |
+
input_ids=input_ids,
|
78 |
+
max_new_tokens = max_new_tokens,
|
79 |
+
do_sample = False if temperature == 0 else True,
|
80 |
+
top_p = top_p,
|
81 |
+
top_k = top_k,
|
82 |
+
temperature = temperature,
|
83 |
+
eos_token_id=[128001,128008,128009],
|
84 |
+
streamer=streamer,
|
85 |
+
)
|
86 |
+
|
87 |
+
with torch.no_grad():
|
88 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
89 |
+
thread.start()
|
90 |
+
|
91 |
+
buffer = ""
|
92 |
+
for new_text in streamer:
|
93 |
+
buffer += new_text
|
94 |
+
yield buffer
|
95 |
+
|
96 |
+
|
97 |
+
chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
|
98 |
+
|
99 |
+
with gr.Blocks(css=CSS, theme="soft") as demo:
|
100 |
+
gr.HTML(TITLE)
|
101 |
+
gr.HTML(DESCRIPTION)
|
102 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
103 |
+
gr.ChatInterface(
|
104 |
+
fn=stream_chat,
|
105 |
+
chatbot=chatbot,
|
106 |
+
fill_height=True,
|
107 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
108 |
+
additional_inputs=[
|
109 |
+
gr.Textbox(
|
110 |
+
value="You are a helpful assistant",
|
111 |
+
label="System Prompt",
|
112 |
+
render=False,
|
113 |
+
),
|
114 |
+
gr.Slider(
|
115 |
+
minimum=0,
|
116 |
+
maximum=1,
|
117 |
+
step=0.1,
|
118 |
+
value=0.8,
|
119 |
+
label="Temperature",
|
120 |
+
render=False,
|
121 |
+
),
|
122 |
+
gr.Slider(
|
123 |
+
minimum=128,
|
124 |
+
maximum=8192,
|
125 |
+
step=1,
|
126 |
+
value=1024,
|
127 |
+
label="Max new tokens",
|
128 |
+
render=False,
|
129 |
+
),
|
130 |
+
gr.Slider(
|
131 |
+
minimum=0.0,
|
132 |
+
maximum=1.0,
|
133 |
+
step=0.1,
|
134 |
+
value=1.0,
|
135 |
+
label="top_p",
|
136 |
+
render=False,
|
137 |
+
),
|
138 |
+
gr.Slider(
|
139 |
+
minimum=1,
|
140 |
+
maximum=20,
|
141 |
+
step=1,
|
142 |
+
value=20,
|
143 |
+
label="top_k",
|
144 |
+
render=False,
|
145 |
+
),
|
146 |
+
gr.Slider(
|
147 |
+
minimum=0.0,
|
148 |
+
maximum=2.0,
|
149 |
+
step=0.1,
|
150 |
+
value=1.2,
|
151 |
+
label="Repetition penalty",
|
152 |
+
render=False,
|
153 |
+
),
|
154 |
+
],
|
155 |
+
examples=[
|
156 |
+
["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],
|
157 |
+
["What are 5 creative things I could do with my kids' art? I don't want to throw them away, but it's also so much clutter."],
|
158 |
+
["Tell me a random fun fact about the Roman Empire."],
|
159 |
+
["Show me a code snippet of a website's sticky header in CSS and JavaScript."],
|
160 |
+
],
|
161 |
+
cache_examples=False,
|
162 |
+
)
|
163 |
+
|
164 |
+
|
165 |
+
if __name__ == "__main__":
|
166 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
timm
|
3 |
+
einops
|
4 |
+
torch
|
5 |
+
torchvision
|
6 |
+
Pillow
|
7 |
+
transformers
|
8 |
+
autoawq
|