Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,57 @@
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline, AutoTokenizer, TextIteratorStreamer
|
|
|
|
| 4 |
import torch
|
|
|
|
| 5 |
from threading import Thread
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def generate(
|
| 16 |
message,
|
| 17 |
history,
|
|
@@ -23,35 +63,28 @@ def generate(
|
|
| 23 |
top_k=50,
|
| 24 |
max_new_tokens=256,
|
| 25 |
):
|
|
|
|
| 26 |
try:
|
| 27 |
-
pipe =
|
| 28 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token=os.environ["token"])
|
| 29 |
-
tokenizer.eos_token = "<|im_end|>"
|
| 30 |
-
print(tokenizer)
|
| 31 |
-
pipe.tokenizer = tokenizer
|
| 32 |
prompt = f"<|im_start|>system\n{system}<|im_end|>\n"
|
| 33 |
for (user_turn, assistant_turn) in history:
|
| 34 |
prompt += f"<|im_start|>user\n{user_turn}<|im_end|>\n<|im_start|>assistant\n{assistant_turn}<|im_end|>\n"
|
| 35 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 36 |
-
|
| 37 |
streamer = TextIteratorStreamer(pipe.tokenizer, timeout=240.0, skip_prompt=True, skip_special_tokens=True)
|
| 38 |
generation_kwargs = dict(
|
| 39 |
-
text_inputs=prompt,
|
| 40 |
-
streamer=streamer,
|
| 41 |
-
max_new_tokens=max_new_tokens,
|
| 42 |
-
do_sample=True,
|
| 43 |
-
top_p=top_p,
|
| 44 |
-
min_p=min_p,
|
| 45 |
-
top_k=top_k,
|
| 46 |
-
temperature=temperature,
|
| 47 |
-
num_beams=1,
|
| 48 |
-
repetition_penalty=1.1
|
| 49 |
)
|
| 50 |
-
|
| 51 |
t = Thread(target=pipe.__call__, kwargs=generation_kwargs)
|
| 52 |
t.start()
|
| 53 |
-
|
| 54 |
-
outputs = []
|
| 55 |
for chunk in streamer:
|
| 56 |
outputs.append(chunk)
|
| 57 |
yield "".join(outputs)
|
|
@@ -62,8 +95,6 @@ def generate(
|
|
| 62 |
print(f"An error occurred: {e}")
|
| 63 |
yield "An error occurred during generation."
|
| 64 |
|
| 65 |
-
model_choices = ["Locutusque/Liberalis-Cogitator-Turbo-Llama-3.1-8B", "Locutusque/Esmeralda-31B", "Locutusque/lmsys-llama-3.1-8b", "Locutusque/lmsys-llama-3.2-3b", "Locutusque/CollectiveLM-Falcon-3-7B", "Locutusque/StockQwen-2.5-7B"]
|
| 66 |
-
# What at the best options?
|
| 67 |
g = gr.ChatInterface(
|
| 68 |
fn=generate,
|
| 69 |
additional_inputs=[
|
|
@@ -73,10 +104,12 @@ g = gr.ChatInterface(
|
|
| 73 |
gr.components.Slider(minimum=0, maximum=1, value=0.95, label="Top p"),
|
| 74 |
gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Min P"),
|
| 75 |
gr.components.Slider(minimum=0, maximum=100, step=1, value=15, label="Top k"),
|
| 76 |
-
gr.components.Slider(minimum=1, maximum=8192, step=1, value=1024, label="Max tokens"),
|
| 77 |
],
|
| 78 |
title="Locutusque's Language Models",
|
| 79 |
description="Try out Locutusque's language models here! Credit goes to Mediocreatmybest for this space. You may also find some experimental preview models that have not been made public here.",
|
| 80 |
)
|
|
|
|
| 81 |
if __name__ == "__main__":
|
|
|
|
| 82 |
g.launch()
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
import torch
|
| 6 |
+
import gc
|
| 7 |
from threading import Thread
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
model_choices = ["Locutusque/Liberalis-Cogitator-Turbo-Llama-3.1-8B", "Locutusque/Esmeralda-31B", "Locutusque/lmsys-llama-3.1-8b", "Locutusque/lmsys-llama-3.2-3b", "Locutusque/CollectiveLM-Falcon-3-7B", "Locutusque/StockQwen-2.5-7B"]
|
| 11 |
+
|
| 12 |
+
# Holds the single currently-loaded model
|
| 13 |
+
CURRENT = {"name": None, "pipe": None}
|
| 14 |
+
|
| 15 |
+
def predownload_all_models():
|
| 16 |
+
"""Download model weights to the local HF cache without loading them onto any device."""
|
| 17 |
+
for model_name in model_choices:
|
| 18 |
+
print(f"Downloading {model_name}...")
|
| 19 |
+
snapshot_download(repo_id=model_name, token=os.environ["token"])
|
| 20 |
+
print("All models downloaded to cache.")
|
| 21 |
+
|
| 22 |
+
def _unload_current():
|
| 23 |
+
if CURRENT["pipe"] is not None:
|
| 24 |
+
print(f"Unloading {CURRENT['name']}...")
|
| 25 |
+
del CURRENT["pipe"]
|
| 26 |
+
CURRENT["pipe"] = None
|
| 27 |
+
CURRENT["name"] = None
|
| 28 |
+
gc.collect()
|
| 29 |
+
if torch.cuda.is_available():
|
| 30 |
+
torch.cuda.empty_cache()
|
| 31 |
+
|
| 32 |
+
def get_model(model_name):
|
| 33 |
+
# If a different model is loaded, unload it first
|
| 34 |
+
if CURRENT["name"] != model_name:
|
| 35 |
+
_unload_current()
|
| 36 |
+
task = "image-text-to-text" if model_name == "Locutusque/Esmeralda-31B" else "text-generation"
|
| 37 |
+
print(f"Loading {model_name} onto GPU...")
|
| 38 |
+
pipe = pipeline(
|
| 39 |
+
task,
|
| 40 |
+
model=model_name,
|
| 41 |
+
device_map="cuda",
|
| 42 |
+
torch_dtype=torch.bfloat16,
|
| 43 |
+
trust_remote_code=True,
|
| 44 |
+
token=os.environ["token"],
|
| 45 |
+
use_fast=True,
|
| 46 |
+
)
|
| 47 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token=os.environ["token"])
|
| 48 |
+
tokenizer.eos_token = "<|im_end|>"
|
| 49 |
+
pipe.tokenizer = tokenizer
|
| 50 |
+
CURRENT["name"] = model_name
|
| 51 |
+
CURRENT["pipe"] = pipe
|
| 52 |
+
return CURRENT["pipe"]
|
| 53 |
+
|
| 54 |
+
@spaces.GPU(duration=90)
|
| 55 |
def generate(
|
| 56 |
message,
|
| 57 |
history,
|
|
|
|
| 63 |
top_k=50,
|
| 64 |
max_new_tokens=256,
|
| 65 |
):
|
| 66 |
+
outputs = []
|
| 67 |
try:
|
| 68 |
+
pipe = get_model(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
prompt = f"<|im_start|>system\n{system}<|im_end|>\n"
|
| 70 |
for (user_turn, assistant_turn) in history:
|
| 71 |
prompt += f"<|im_start|>user\n{user_turn}<|im_end|>\n<|im_start|>assistant\n{assistant_turn}<|im_end|>\n"
|
| 72 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
| 73 |
streamer = TextIteratorStreamer(pipe.tokenizer, timeout=240.0, skip_prompt=True, skip_special_tokens=True)
|
| 74 |
generation_kwargs = dict(
|
| 75 |
+
text_inputs=prompt,
|
| 76 |
+
streamer=streamer,
|
| 77 |
+
max_new_tokens=max_new_tokens,
|
| 78 |
+
do_sample=True,
|
| 79 |
+
top_p=top_p,
|
| 80 |
+
min_p=min_p,
|
| 81 |
+
top_k=top_k,
|
| 82 |
+
temperature=temperature,
|
| 83 |
+
num_beams=1,
|
| 84 |
+
repetition_penalty=1.1,
|
| 85 |
)
|
|
|
|
| 86 |
t = Thread(target=pipe.__call__, kwargs=generation_kwargs)
|
| 87 |
t.start()
|
|
|
|
|
|
|
| 88 |
for chunk in streamer:
|
| 89 |
outputs.append(chunk)
|
| 90 |
yield "".join(outputs)
|
|
|
|
| 95 |
print(f"An error occurred: {e}")
|
| 96 |
yield "An error occurred during generation."
|
| 97 |
|
|
|
|
|
|
|
| 98 |
g = gr.ChatInterface(
|
| 99 |
fn=generate,
|
| 100 |
additional_inputs=[
|
|
|
|
| 104 |
gr.components.Slider(minimum=0, maximum=1, value=0.95, label="Top p"),
|
| 105 |
gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Min P"),
|
| 106 |
gr.components.Slider(minimum=0, maximum=100, step=1, value=15, label="Top k"),
|
| 107 |
+
gr.components.Slider(minimum=1, maximum=8192, step=1, value=1024, label="Max tokens"),
|
| 108 |
],
|
| 109 |
title="Locutusque's Language Models",
|
| 110 |
description="Try out Locutusque's language models here! Credit goes to Mediocreatmybest for this space. You may also find some experimental preview models that have not been made public here.",
|
| 111 |
)
|
| 112 |
+
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
predownload_all_models()
|
| 115 |
g.launch()
|