Spaces:
Running
on
Zero
Running
on
Zero
slimfrikha-tii
commited on
Commit
•
72cc672
0
Parent(s):
falcon3 release
Browse files- .gitattributes +35 -0
- README.md +14 -0
- app.py +292 -0
- logs/.keepfile +0 -0
- requirements.txt +3 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Falcon3 Demo
|
3 |
+
emoji: 🦅
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.0.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
short_description: F3-DEMO
|
12 |
+
---
|
13 |
+
|
14 |
+
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
ADDED
@@ -0,0 +1,292 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from threading import Thread
|
4 |
+
from typing import List, Optional, Tuple, Dict
|
5 |
+
import gradio as gr
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
7 |
+
import spaces
|
8 |
+
from pathlib import Path
|
9 |
+
from huggingface_hub import CommitScheduler
|
10 |
+
import uuid
|
11 |
+
import json
|
12 |
+
|
13 |
+
|
14 |
+
# Constants
|
15 |
+
SYSTEM_PROMPT = """You are a helpful friendly assistant Falcon3 from TII, try to follow instructions as much as possible. Be accurate and brief. The Technology Innovation Institute (TII) is a leading global research center dedicated to pushing the frontiers of knowledge. Our teams of scientists, researchers and engineers work in an open, flexible and agile environment to deliver discovery science and transformative technologies.\nWe are part of the Abu Dhabi Government's Advanced Technology Research Council (ATRC), which oversees technology research in the emirate."""
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
TITLE = "<h1><center>Falcon3 Instruct Playground</center></h1>"
|
18 |
+
|
19 |
+
# Custom CSS with dark theme
|
20 |
+
CSS = """
|
21 |
+
.duplicate-button {
|
22 |
+
margin: auto !important;
|
23 |
+
color: white !important;
|
24 |
+
background: black !important;
|
25 |
+
border-radius: 100vh !important;
|
26 |
+
}
|
27 |
+
|
28 |
+
h3 {
|
29 |
+
text-align: center;
|
30 |
+
/* Fix for chat container */
|
31 |
+
.chat-container {
|
32 |
+
height: 500px !important;
|
33 |
+
overflow-y: auto !important;
|
34 |
+
flex-direction: column !important;
|
35 |
+
}
|
36 |
+
|
37 |
+
.messages-container {
|
38 |
+
flex-grow: 1 !important;
|
39 |
+
overflow-y: auto !important;
|
40 |
+
padding-right: 10px !important;
|
41 |
+
}
|
42 |
+
|
43 |
+
.contain {
|
44 |
+
height: 100% !important;
|
45 |
+
}
|
46 |
+
|
47 |
+
.model-selector {
|
48 |
+
margin: 1em auto !important;
|
49 |
+
max-width: 500px !important;
|
50 |
+
background: #1f2937 !important;
|
51 |
+
padding: 1em !important;
|
52 |
+
border-radius: 8px !important;
|
53 |
+
}
|
54 |
+
|
55 |
+
/* Style for radio group container */
|
56 |
+
.radio-group {
|
57 |
+
background: #1f2937 !important;
|
58 |
+
padding: 1em !important;
|
59 |
+
border-radius: 8px !important;
|
60 |
+
gap: 10px !important;
|
61 |
+
}
|
62 |
+
|
63 |
+
/* Style for radio options */
|
64 |
+
.radio-group label {
|
65 |
+
background: #374151 !important;
|
66 |
+
border: none !important;
|
67 |
+
border-radius: 8px !important;
|
68 |
+
padding: 8px 16px !important;
|
69 |
+
color: white !important;
|
70 |
+
}
|
71 |
+
|
72 |
+
/* Selected radio option */
|
73 |
+
.radio-group label[data-selected="true"] {
|
74 |
+
background: #6366f1 !important;
|
75 |
+
color: white !important;
|
76 |
+
}
|
77 |
+
|
78 |
+
button {
|
79 |
+
border-radius: 8px !important;
|
80 |
+
}
|
81 |
+
"""
|
82 |
+
|
83 |
+
# Global variables to store loaded models and tokenizers
|
84 |
+
loaded_models = {}
|
85 |
+
loaded_tokenizers = {}
|
86 |
+
|
87 |
+
def load_model(model_size: str):
|
88 |
+
"""Load model and tokenizer based on selected size"""
|
89 |
+
if model_size not in loaded_models:
|
90 |
+
model_map = {
|
91 |
+
"1B": "tiiuae/Falcon3-1B-Instruct",
|
92 |
+
"3B": "tiiuae/Falcon3-3B-Instruct",
|
93 |
+
"7B": "tiiuae/Falcon3-7B-Instruct",
|
94 |
+
"10B": "tiiuae/Falcon3-10B-Instruct",
|
95 |
+
|
96 |
+
}
|
97 |
+
|
98 |
+
model_path = model_map[model_size]
|
99 |
+
print(f"Loading model: {model_path}")
|
100 |
+
|
101 |
+
loaded_models[model_size] = AutoModelForCausalLM.from_pretrained(
|
102 |
+
model_path,
|
103 |
+
torch_dtype=torch.bfloat16,
|
104 |
+
).to(device)
|
105 |
+
loaded_tokenizers[model_size] = AutoTokenizer.from_pretrained(model_path)
|
106 |
+
|
107 |
+
return loaded_models[model_size], loaded_tokenizers[model_size]
|
108 |
+
|
109 |
+
#load_model("10B")
|
110 |
+
#load_model("3B")
|
111 |
+
#load_model("1B")
|
112 |
+
load_model("7B")
|
113 |
+
|
114 |
+
|
115 |
+
logs_id = os.getenv("LOGS_ID")
|
116 |
+
logs_token = os.getenv("HF_LOGS_TOKEN")
|
117 |
+
|
118 |
+
logs_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
119 |
+
logs_folder = logs_file.parent
|
120 |
+
|
121 |
+
scheduler = CommitScheduler(
|
122 |
+
repo_id=logs_id,
|
123 |
+
repo_type="dataset",
|
124 |
+
folder_path=logs_folder,
|
125 |
+
path_in_repo="data",
|
126 |
+
every=5,
|
127 |
+
token=logs_token,
|
128 |
+
private=True,
|
129 |
+
)
|
130 |
+
|
131 |
+
@spaces.GPU
|
132 |
+
def stream_chat(
|
133 |
+
message: str,
|
134 |
+
history: list,
|
135 |
+
model_size: str,
|
136 |
+
temperature: float = 0.3,
|
137 |
+
max_new_tokens: int = 1024,
|
138 |
+
top_p: float = 1.0,
|
139 |
+
top_k: int = 20,
|
140 |
+
repetition_penalty: float = 1.2,
|
141 |
+
):
|
142 |
+
model, tokenizer = load_model(model_size)
|
143 |
+
|
144 |
+
# Create new history list with current message
|
145 |
+
new_history = history + [[message, ""]]
|
146 |
+
|
147 |
+
conversation = []
|
148 |
+
# Only include previous messages in the conversation
|
149 |
+
for prompt, answer in history:
|
150 |
+
conversation.extend([
|
151 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
152 |
+
{"role": "user", "content": prompt},
|
153 |
+
{"role": "assistant", "content": answer},
|
154 |
+
])
|
155 |
+
|
156 |
+
conversation.append({"role": "user", "content": message})
|
157 |
+
|
158 |
+
input_text = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
159 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
160 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=40.0, skip_prompt=True, skip_special_tokens=True)
|
161 |
+
|
162 |
+
generate_kwargs = dict(
|
163 |
+
input_ids=inputs,
|
164 |
+
max_new_tokens=max_new_tokens,
|
165 |
+
do_sample=False if temperature == 0 else True,
|
166 |
+
top_p=top_p,
|
167 |
+
top_k=top_k,
|
168 |
+
temperature=temperature,
|
169 |
+
repetition_penalty=repetition_penalty,
|
170 |
+
streamer=streamer,
|
171 |
+
pad_token_id=tokenizer.pad_token_id,
|
172 |
+
)
|
173 |
+
|
174 |
+
with torch.no_grad():
|
175 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
176 |
+
thread.start()
|
177 |
+
|
178 |
+
buffer = ""
|
179 |
+
for new_text in streamer:
|
180 |
+
buffer += new_text
|
181 |
+
buffer = buffer.replace("\nUser", "")
|
182 |
+
buffer = buffer.replace("\nSystem", "")
|
183 |
+
new_history[-1][1] = buffer
|
184 |
+
yield new_history
|
185 |
+
|
186 |
+
with scheduler.lock:
|
187 |
+
with logs_file.open("a") as f:
|
188 |
+
f.write(json.dumps({"input": input_text.replace(SYSTEM_PROMPT, ""), "output": buffer.replace(SYSTEM_PROMPT, ""), "model": model_size}))
|
189 |
+
f.write("\n")
|
190 |
+
|
191 |
+
def clear_input():
|
192 |
+
return ""
|
193 |
+
|
194 |
+
def add_message(message: str, history: list):
|
195 |
+
if message.strip() != "":
|
196 |
+
history = history + [[message, ""]]
|
197 |
+
return history
|
198 |
+
|
199 |
+
def clear_session() -> Tuple[str, List]:
|
200 |
+
return '', []
|
201 |
+
|
202 |
+
def choose_model(radio: str) -> Tuple[gr.Markdown, gr.Chatbot, str]:
|
203 |
+
mark_ = gr.Markdown(value=f"<center><font size=8>Falcon3 {radio} ChatBot</center>")
|
204 |
+
chatbot = gr.Chatbot(label=f'Falcon3-{radio}-Instruct', value=[])
|
205 |
+
return mark_, chatbot, ""
|
206 |
+
|
207 |
+
def main():
|
208 |
+
with gr.Blocks(css=CSS, theme="soft") as demo:
|
209 |
+
gr.HTML(TITLE)
|
210 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
211 |
+
with gr.Row():
|
212 |
+
options_models = ["1B", "3B", "7B", "10B"]
|
213 |
+
radio = gr.Radio(
|
214 |
+
choices=options_models,
|
215 |
+
label="Model Size:",
|
216 |
+
value="7B",
|
217 |
+
elem_classes="radio-group"
|
218 |
+
)
|
219 |
+
|
220 |
+
with gr.Row():
|
221 |
+
with gr.Accordion(label="Chat Interface", open=True):
|
222 |
+
mark_ = gr.Markdown("""<center><font size=8>Falcon3 7B ChatBot </center>""")
|
223 |
+
|
224 |
+
chatbot = gr.Chatbot(
|
225 |
+
label='Falcon3-7B-Instruct',
|
226 |
+
height=500,
|
227 |
+
container=True,
|
228 |
+
elem_classes=["chat-container"]
|
229 |
+
)
|
230 |
+
|
231 |
+
with gr.Accordion(label="⚙️ Parameters", open=False):
|
232 |
+
temperature = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.3, label="Temperature")
|
233 |
+
max_new_tokens = gr.Slider(minimum=128, maximum=32768, step=128, value=1024, label="Max new tokens")
|
234 |
+
top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1.0, label="Top-p")
|
235 |
+
top_k = gr.Slider(minimum=1, maximum=100, step=1, value=20, label="Top-k")
|
236 |
+
repetition_penalty = gr.Slider(minimum=1.0, maximum=2.0, step=0.1, value=1.2, label="Repetition penalty")
|
237 |
+
|
238 |
+
textbox = gr.Textbox(lines=1, label='Input')
|
239 |
+
|
240 |
+
with gr.Row():
|
241 |
+
clear_history = gr.Button("🧹 Clear History")
|
242 |
+
submit = gr.Button("🚀 Send")
|
243 |
+
|
244 |
+
# Chain of events for submit button
|
245 |
+
submit_event = submit.click(
|
246 |
+
fn=add_message,
|
247 |
+
inputs=[textbox, chatbot],
|
248 |
+
outputs=chatbot,
|
249 |
+
queue=False
|
250 |
+
).then(
|
251 |
+
fn=clear_input,
|
252 |
+
outputs=textbox,
|
253 |
+
queue=False
|
254 |
+
).then(
|
255 |
+
fn=stream_chat,
|
256 |
+
inputs=[textbox, chatbot, radio,
|
257 |
+
temperature, max_new_tokens, top_p, top_k, repetition_penalty],
|
258 |
+
outputs=chatbot,
|
259 |
+
show_progress=True
|
260 |
+
)
|
261 |
+
|
262 |
+
# Chain of events for enter key
|
263 |
+
enter_event = textbox.submit(
|
264 |
+
fn=add_message,
|
265 |
+
inputs=[textbox, chatbot],
|
266 |
+
outputs=chatbot,
|
267 |
+
queue=False
|
268 |
+
).then(
|
269 |
+
fn=clear_input,
|
270 |
+
outputs=textbox,
|
271 |
+
queue=False
|
272 |
+
).then(
|
273 |
+
fn=stream_chat,
|
274 |
+
inputs=[textbox, chatbot, radio,
|
275 |
+
temperature, max_new_tokens, top_p, top_k, repetition_penalty],
|
276 |
+
outputs=chatbot,
|
277 |
+
show_progress=True
|
278 |
+
)
|
279 |
+
|
280 |
+
clear_history.click(fn=clear_session,
|
281 |
+
outputs=[textbox, chatbot])
|
282 |
+
|
283 |
+
radio.change(choose_model,
|
284 |
+
inputs=[radio],
|
285 |
+
outputs=[mark_, chatbot, textbox])
|
286 |
+
|
287 |
+
|
288 |
+
#demo.queue(api_open=False, default_concurrency_limit=40)
|
289 |
+
demo.launch()
|
290 |
+
|
291 |
+
if __name__ == "__main__":
|
292 |
+
main()
|
logs/.keepfile
ADDED
File without changes
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
torch
|
3 |
+
transformers==4.46.2
|