Spaces:
Runtime error
Runtime error
File size: 3,804 Bytes
990a40d | 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 | import torch
import gradio as gr
from unsloth import FastLanguageModel
from peft import PeftModel
# =========================
# Load model once at startup
# =========================
print("Loading base model...")
base_model, proc = FastLanguageModel.from_pretrained(
"unsloth/Qwen3.5-9B",
max_seq_length=2048,
load_in_4bit=True, # Recommended unless you have lots of VRAM
)
tokenizer = proc.tokenizer if hasattr(proc, "tokenizer") else proc
print("Loading LoRA adapter...")
model = PeftModel.from_pretrained(
base_model,
"XiangJinYu/Qwen3.5-9B-Humanize-DPO-Round2",
is_trainable=False,
)
if hasattr(model, "config") and getattr(model.config, "model_type", "") == "qwen3_5":
model.config.model_type = "qwen3"
FastLanguageModel.for_inference(model)
print("Model loaded successfully!")
# =========================
# Inference function
# =========================
def humanize_text(
text,
temperature,
top_p,
max_tokens,
):
if not text.strip():
return ""
instruction = (
"请将下面文本改写得更像自然人写作,"
"保持原意与事实,不要加标题或说明。"
)
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": f"{instruction}\n\n原文:{text}",
}
],
}
]
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False,
)
inputs = tokenizer(
prompt,
return_tensors="pt",
).to(model.device)
with torch.inference_mode():
outputs = model.generate(
**inputs,
max_new_tokens=int(max_tokens),
temperature=float(temperature),
top_p=float(top_p),
do_sample=True,
repetition_penalty=1.1,
)
generated = outputs[0][inputs["input_ids"].shape[1]:]
result = tokenizer.decode(
generated,
skip_special_tokens=True,
)
return result.strip()
# =========================
# Gradio UI
# =========================
with gr.Blocks(title="Qwen Humanizer") as demo:
gr.Markdown(
"""
# Qwen Humanizer
Paste academic, AI-generated, or formal text and rewrite it to sound more natural while preserving meaning.
"""
)
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Input Text",
lines=12,
placeholder="Paste text here...",
)
temperature = gr.Slider(
minimum=0.1,
maximum=1.2,
value=0.65,
step=0.05,
label="Temperature",
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.9,
step=0.05,
label="Top P",
)
max_tokens = gr.Slider(
minimum=64,
maximum=1024,
value=512,
step=32,
label="Max New Tokens",
)
btn = gr.Button("Humanize")
with gr.Column():
output_text = gr.Textbox(
label="Humanized Output",
lines=12,
)
btn.click(
fn=humanize_text,
inputs=[
input_text,
temperature,
top_p,
max_tokens,
],
outputs=output_text,
)
demo.launch() |