updated app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,60 @@
|
|
| 1 |
-
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, torch, gradio as gr
|
| 2 |
+
from threading import Thread
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 4 |
|
| 5 |
+
os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1") # faster download on Spaces
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "TildeAI/TildeOpen-30b"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=False)
|
| 9 |
+
|
| 10 |
+
# load in BF16 and let HF map devices automatically
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_ID,
|
| 13 |
+
torch_dtype=torch.bfloat16,
|
| 14 |
+
device_map="auto",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# slight speedups on A100
|
| 18 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 19 |
+
|
| 20 |
+
SYS = (
|
| 21 |
+
"You are a helpful multilingual assistant. "
|
| 22 |
+
"This is a *base* model (not instruction tuned); follow the user's request precisely."
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def build_prompt(history, user_msg):
|
| 26 |
+
# simple conversation transcript; base models don't need a special chat template
|
| 27 |
+
parts = [SYS, ""]
|
| 28 |
+
for u, a in history:
|
| 29 |
+
parts += [f"User: {u}", f"Assistant: {a}"]
|
| 30 |
+
parts += [f"User: {user_msg}", "Assistant:"]
|
| 31 |
+
return "\n".join(parts)
|
| 32 |
+
|
| 33 |
+
def chat_fn(message, history):
|
| 34 |
+
prompt = build_prompt(history, message)
|
| 35 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 36 |
+
|
| 37 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 38 |
+
gen_kwargs = dict(
|
| 39 |
+
**inputs,
|
| 40 |
+
max_new_tokens=512,
|
| 41 |
+
do_sample=True,
|
| 42 |
+
temperature=0.7,
|
| 43 |
+
top_p=0.9,
|
| 44 |
+
repetition_penalty=1.1,
|
| 45 |
+
streamer=streamer,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
t = Thread(target=model.generate, kwargs=gen_kwargs)
|
| 49 |
+
t.start()
|
| 50 |
+
partial = ""
|
| 51 |
+
for chunk in streamer:
|
| 52 |
+
partial += chunk
|
| 53 |
+
yield partial
|
| 54 |
+
|
| 55 |
+
demo = gr.ChatInterface(
|
| 56 |
+
fn=chat_fn,
|
| 57 |
+
title="TildeOpen-30B (Transformers, BF16)",
|
| 58 |
+
description="Base model; multilingual. If build fails with OOM, switch to Option B (GGUF).",
|
| 59 |
+
)
|
| 60 |
+
demo.queue().launch()
|