Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,043 Bytes
72cc672 eda8c36 72cc672 2965039 72cc672 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
import os
import torch
from threading import Thread
from typing import List, Optional, Tuple, Dict
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
import spaces
from pathlib import Path
from huggingface_hub import CommitScheduler
import uuid
import json
# Constants
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."""
device = "cuda" if torch.cuda.is_available() else "cpu"
TITLE = "<h1><center>Falcon3 Instruct Playground</center></h1>"
SUB_TITLE = "<h2><center>Try out also <a href='https://chat.falconllm.tii.ae/'>our demo</a> powered by <a href='https://www.openinnovation.ai/'>OpenInnovation AI</a> </center></h2>"
# Custom CSS with dark theme
CSS = """
.duplicate-button {
margin: auto !important;
color: white !important;
background: black !important;
border-radius: 100vh !important;
}
h3 {
text-align: center;
/* Fix for chat container */
.chat-container {
height: 500px !important;
overflow-y: auto !important;
flex-direction: column !important;
}
.messages-container {
flex-grow: 1 !important;
overflow-y: auto !important;
padding-right: 10px !important;
}
.contain {
height: 100% !important;
}
.model-selector {
margin: 1em auto !important;
max-width: 500px !important;
background: #1f2937 !important;
padding: 1em !important;
border-radius: 8px !important;
}
/* Style for radio group container */
.radio-group {
background: #1f2937 !important;
padding: 1em !important;
border-radius: 8px !important;
gap: 10px !important;
}
/* Style for radio options */
.radio-group label {
background: #374151 !important;
border: none !important;
border-radius: 8px !important;
padding: 8px 16px !important;
color: white !important;
}
/* Selected radio option */
.radio-group label[data-selected="true"] {
background: #6366f1 !important;
color: white !important;
}
button {
border-radius: 8px !important;
}
"""
# Global variables to store loaded models and tokenizers
loaded_models = {}
loaded_tokenizers = {}
def load_model(model_size: str):
"""Load model and tokenizer based on selected size"""
if model_size not in loaded_models:
model_map = {
"1B": "tiiuae/Falcon3-1B-Instruct",
"3B": "tiiuae/Falcon3-3B-Instruct",
"7B": "tiiuae/Falcon3-7B-Instruct",
"10B": "tiiuae/Falcon3-10B-Instruct",
}
model_path = model_map[model_size]
print(f"Loading model: {model_path}")
loaded_models[model_size] = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
).to(device)
loaded_tokenizers[model_size] = AutoTokenizer.from_pretrained(model_path)
return loaded_models[model_size], loaded_tokenizers[model_size]
#load_model("10B")
#load_model("3B")
#load_model("1B")
load_model("7B")
logs_id = os.getenv("LOGS_ID")
logs_token = os.getenv("HF_LOGS_TOKEN")
logs_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
logs_folder = logs_file.parent
scheduler = CommitScheduler(
repo_id=logs_id,
repo_type="dataset",
folder_path=logs_folder,
path_in_repo="data",
every=5,
token=logs_token,
private=True,
)
@spaces.GPU
def stream_chat(
message: str,
history: list,
model_size: str,
temperature: float = 0.3,
max_new_tokens: int = 1024,
top_p: float = 1.0,
top_k: int = 20,
repetition_penalty: float = 1.2,
):
model, tokenizer = load_model(model_size)
# Create new history list with current message
new_history = history + [[message, ""]]
conversation = []
# Only include previous messages in the conversation
for prompt, answer in history:
conversation.extend([
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
{"role": "assistant", "content": answer},
])
conversation.append({"role": "user", "content": message})
input_text = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
streamer = TextIteratorStreamer(tokenizer, timeout=40.0, skip_prompt=True, skip_special_tokens=True)
generate_kwargs = dict(
input_ids=inputs,
max_new_tokens=max_new_tokens,
do_sample=False if temperature == 0 else True,
top_p=top_p,
top_k=top_k,
temperature=temperature,
repetition_penalty=repetition_penalty,
streamer=streamer,
pad_token_id=tokenizer.pad_token_id,
)
with torch.no_grad():
thread = Thread(target=model.generate, kwargs=generate_kwargs)
thread.start()
buffer = ""
for new_text in streamer:
buffer += new_text
buffer = buffer.replace("\nUser", "")
buffer = buffer.replace("\nSystem", "")
new_history[-1][1] = buffer
yield new_history
with scheduler.lock:
with logs_file.open("a") as f:
f.write(json.dumps({"input": input_text.replace(SYSTEM_PROMPT, ""), "output": buffer.replace(SYSTEM_PROMPT, ""), "model": model_size}))
f.write("\n")
def clear_input():
return ""
def add_message(message: str, history: list):
if message.strip() != "":
history = history + [[message, ""]]
return history
def clear_session() -> Tuple[str, List]:
return '', []
def choose_model(radio: str) -> Tuple[gr.Markdown, gr.Chatbot, str]:
mark_ = gr.Markdown(value=f"<center><font size=8>Falcon3 {radio} ChatBot</center>")
chatbot = gr.Chatbot(label=f'Falcon3-{radio}-Instruct', value=[])
return mark_, chatbot, ""
def main():
with gr.Blocks(css=CSS, theme="soft") as demo:
gr.HTML(TITLE)
gr.HTML(SUB_TITLE)
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
with gr.Row():
options_models = ["1B", "3B", "7B", "10B"]
radio = gr.Radio(
choices=options_models,
label="Model Size:",
value="7B",
elem_classes="radio-group"
)
with gr.Row():
with gr.Accordion(label="Chat Interface", open=True):
mark_ = gr.Markdown("""<center><font size=8>Falcon3 7B ChatBot </center>""")
chatbot = gr.Chatbot(
label='Falcon3-7B-Instruct',
height=500,
container=True,
elem_classes=["chat-container"]
)
with gr.Accordion(label="⚙️ Parameters", open=False):
temperature = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.3, label="Temperature")
max_new_tokens = gr.Slider(minimum=128, maximum=32768, step=128, value=1024, label="Max new tokens")
top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1.0, label="Top-p")
top_k = gr.Slider(minimum=1, maximum=100, step=1, value=20, label="Top-k")
repetition_penalty = gr.Slider(minimum=1.0, maximum=2.0, step=0.1, value=1.2, label="Repetition penalty")
textbox = gr.Textbox(lines=1, label='Input')
with gr.Row():
clear_history = gr.Button("🧹 Clear History")
submit = gr.Button("🚀 Send")
# Chain of events for submit button
submit_event = submit.click(
fn=add_message,
inputs=[textbox, chatbot],
outputs=chatbot,
queue=False
).then(
fn=clear_input,
outputs=textbox,
queue=False
).then(
fn=stream_chat,
inputs=[textbox, chatbot, radio,
temperature, max_new_tokens, top_p, top_k, repetition_penalty],
outputs=chatbot,
show_progress=True
)
# Chain of events for enter key
enter_event = textbox.submit(
fn=add_message,
inputs=[textbox, chatbot],
outputs=chatbot,
queue=False
).then(
fn=clear_input,
outputs=textbox,
queue=False
).then(
fn=stream_chat,
inputs=[textbox, chatbot, radio,
temperature, max_new_tokens, top_p, top_k, repetition_penalty],
outputs=chatbot,
show_progress=True
)
clear_history.click(fn=clear_session,
outputs=[textbox, chatbot])
radio.change(choose_model,
inputs=[radio],
outputs=[mark_, chatbot, textbox])
#demo.queue(api_open=False, default_concurrency_limit=40)
demo.launch()
if __name__ == "__main__":
main() |