Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
response += token
|
| 40 |
yield response
|
| 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 |
if __name__ == "__main__":
|
| 69 |
-
demo.
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from threading import Thread
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
MODEL_ID = os.getenv("MODEL_ID", "GenueAI/Matrix-Prime-8B")
|
| 10 |
+
|
| 11 |
+
tokenizer = None
|
| 12 |
+
model = None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def load_model():
|
| 16 |
+
global tokenizer, model
|
| 17 |
+
|
| 18 |
+
if tokenizer is not None and model is not None:
|
| 19 |
+
return tokenizer, model
|
| 20 |
+
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 22 |
+
if tokenizer.pad_token_id is None:
|
| 23 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 24 |
+
|
| 25 |
+
kwargs = {
|
| 26 |
+
"trust_remote_code": True,
|
| 27 |
+
"torch_dtype": torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 28 |
+
"low_cpu_mem_usage": True,
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if torch.cuda.is_available():
|
| 32 |
+
kwargs["device_map"] = "auto"
|
| 33 |
|
| 34 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, **kwargs)
|
| 35 |
+
if not torch.cuda.is_available():
|
| 36 |
+
model = model.to("cpu")
|
| 37 |
|
| 38 |
+
model.eval()
|
| 39 |
+
return tokenizer, model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
| 41 |
|
| 42 |
+
def build_prompt(message, history, system_prompt):
|
| 43 |
+
messages = []
|
| 44 |
+
if system_prompt.strip():
|
| 45 |
+
messages.append({"role": "system", "content": system_prompt.strip()})
|
| 46 |
+
|
| 47 |
+
for user_message, assistant_message in history:
|
| 48 |
+
if user_message:
|
| 49 |
+
messages.append({"role": "user", "content": user_message})
|
| 50 |
+
if assistant_message:
|
| 51 |
+
messages.append({"role": "assistant", "content": assistant_message})
|
| 52 |
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
+
tok, _ = load_model()
|
| 56 |
+
if hasattr(tok, "apply_chat_template") and tok.chat_template:
|
| 57 |
+
return tok.apply_chat_template(
|
| 58 |
+
messages,
|
| 59 |
+
tokenize=False,
|
| 60 |
+
add_generation_prompt=True,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
prompt = ""
|
| 64 |
+
for item in messages:
|
| 65 |
+
role = item["role"].capitalize()
|
| 66 |
+
prompt += f"{role}: {item['content']}\n"
|
| 67 |
+
return prompt + "Assistant:"
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def chat(message, history, system_prompt, max_new_tokens, temperature, top_p, repetition_penalty):
|
| 71 |
+
if not message.strip():
|
| 72 |
+
yield ""
|
| 73 |
+
return
|
| 74 |
|
| 75 |
+
tok, mdl = load_model()
|
| 76 |
+
prompt = build_prompt(message, history, system_prompt)
|
| 77 |
+
inputs = tok(prompt, return_tensors="pt").to(mdl.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
streamer = TextIteratorStreamer(tok, skip_prompt=True, skip_special_tokens=True)
|
| 80 |
+
generation_kwargs = {
|
| 81 |
+
**inputs,
|
| 82 |
+
"streamer": streamer,
|
| 83 |
+
"max_new_tokens": int(max_new_tokens),
|
| 84 |
+
"temperature": float(temperature),
|
| 85 |
+
"top_p": float(top_p),
|
| 86 |
+
"repetition_penalty": float(repetition_penalty),
|
| 87 |
+
"do_sample": temperature > 0,
|
| 88 |
+
"pad_token_id": tok.pad_token_id,
|
| 89 |
+
"eos_token_id": tok.eos_token_id,
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
thread = Thread(target=mdl.generate, kwargs=generation_kwargs)
|
| 93 |
+
thread.start()
|
| 94 |
+
|
| 95 |
+
response = ""
|
| 96 |
+
for token in streamer:
|
| 97 |
response += token
|
| 98 |
yield response
|
| 99 |
|
| 100 |
|
| 101 |
+
with gr.Blocks(title="Matrix Prime 8B Chat") as demo:
|
| 102 |
+
gr.Markdown("# Matrix Prime 8B Chat")
|
| 103 |
+
gr.Markdown(f"Chat with `{MODEL_ID}` from Hugging Face.")
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column(scale=4):
|
| 107 |
+
chatbot = gr.ChatInterface(
|
| 108 |
+
fn=chat,
|
| 109 |
+
additional_inputs=[
|
| 110 |
+
gr.Textbox(
|
| 111 |
+
label="System prompt",
|
| 112 |
+
value="You are a helpful assistant.",
|
| 113 |
+
lines=3,
|
| 114 |
+
),
|
| 115 |
+
gr.Slider(64, 4096, value=512, step=32, label="Max new tokens"),
|
| 116 |
+
gr.Slider(0.0, 2.0, value=0.7, step=0.05, label="Temperature"),
|
| 117 |
+
gr.Slider(0.05, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 118 |
+
gr.Slider(1.0, 2.0, value=1.1, step=0.05, label="Repetition penalty"),
|
| 119 |
+
],
|
| 120 |
+
textbox=gr.Textbox(
|
| 121 |
+
placeholder="Ask Matrix Prime 8B anything...",
|
| 122 |
+
container=False,
|
| 123 |
+
scale=7,
|
| 124 |
+
),
|
| 125 |
+
submit_btn="Send",
|
| 126 |
+
stop_btn="Stop",
|
| 127 |
+
retry_btn="Retry",
|
| 128 |
+
undo_btn="Undo",
|
| 129 |
+
clear_btn="Clear",
|
| 130 |
+
)
|
| 131 |
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
+
demo.queue()
|
| 135 |
+
demo.launch()
|