Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from threading import Thread
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "huihui-ai/Huihui-Qwen3.5-35B-A3B-abliterated"
|
| 7 |
+
|
| 8 |
+
print("Loading tokenizer...")
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
+
|
| 11 |
+
print("Loading model (4-bit quantized)...")
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
MODEL_ID,
|
| 14 |
+
quantization_config=BitsAndBytesConfig(
|
| 15 |
+
load_in_4bit=True,
|
| 16 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 17 |
+
bnb_4bit_quant_type="nf4",
|
| 18 |
+
),
|
| 19 |
+
device_map="auto",
|
| 20 |
+
torch_dtype=torch.bfloat16,
|
| 21 |
+
)
|
| 22 |
+
print("Model loaded!")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def chat(message, history):
|
| 26 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 27 |
+
for msg in history:
|
| 28 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
text = tokenizer.apply_chat_template(
|
| 32 |
+
messages, tokenize=False, add_generation_prompt=True, enable_thinking=False
|
| 33 |
+
)
|
| 34 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 35 |
+
|
| 36 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 37 |
+
thread = Thread(
|
| 38 |
+
target=model.generate,
|
| 39 |
+
kwargs=dict(
|
| 40 |
+
**inputs,
|
| 41 |
+
max_new_tokens=2048,
|
| 42 |
+
temperature=0.7,
|
| 43 |
+
top_k=20,
|
| 44 |
+
top_p=0.95,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
streamer=streamer,
|
| 47 |
+
),
|
| 48 |
+
)
|
| 49 |
+
thread.start()
|
| 50 |
+
|
| 51 |
+
partial = ""
|
| 52 |
+
for token in streamer:
|
| 53 |
+
partial += token
|
| 54 |
+
yield partial
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
demo = gr.ChatInterface(
|
| 58 |
+
chat,
|
| 59 |
+
title="Huihui-Qwen3.5-35B-A3B Abliterated",
|
| 60 |
+
description="Chat with the abliterated Qwen3.5-35B-A3B model (4-bit quantized, uncensored)",
|
| 61 |
+
type="messages",
|
| 62 |
+
)
|
| 63 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|