Munene1 commited on
Commit
990a40d
·
verified ·
1 Parent(s): 8af97cf

UploadApp.py

Browse files
Files changed (1) hide show
  1. app.py +162 -0
app.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+
4
+ from unsloth import FastLanguageModel
5
+ from peft import PeftModel
6
+
7
+ # =========================
8
+ # Load model once at startup
9
+ # =========================
10
+
11
+ print("Loading base model...")
12
+
13
+ base_model, proc = FastLanguageModel.from_pretrained(
14
+ "unsloth/Qwen3.5-9B",
15
+ max_seq_length=2048,
16
+ load_in_4bit=True, # Recommended unless you have lots of VRAM
17
+ )
18
+
19
+ tokenizer = proc.tokenizer if hasattr(proc, "tokenizer") else proc
20
+
21
+ print("Loading LoRA adapter...")
22
+
23
+ model = PeftModel.from_pretrained(
24
+ base_model,
25
+ "XiangJinYu/Qwen3.5-9B-Humanize-DPO-Round2",
26
+ is_trainable=False,
27
+ )
28
+
29
+ if hasattr(model, "config") and getattr(model.config, "model_type", "") == "qwen3_5":
30
+ model.config.model_type = "qwen3"
31
+
32
+ FastLanguageModel.for_inference(model)
33
+
34
+ print("Model loaded successfully!")
35
+
36
+ # =========================
37
+ # Inference function
38
+ # =========================
39
+
40
+ def humanize_text(
41
+ text,
42
+ temperature,
43
+ top_p,
44
+ max_tokens,
45
+ ):
46
+ if not text.strip():
47
+ return ""
48
+
49
+ instruction = (
50
+ "请将下面文本改写得更像自然人写作,"
51
+ "保持原意与事实,不要加标题或说明。"
52
+ )
53
+
54
+ messages = [
55
+ {
56
+ "role": "user",
57
+ "content": [
58
+ {
59
+ "type": "text",
60
+ "text": f"{instruction}\n\n原文:{text}",
61
+ }
62
+ ],
63
+ }
64
+ ]
65
+
66
+ prompt = tokenizer.apply_chat_template(
67
+ messages,
68
+ tokenize=False,
69
+ add_generation_prompt=True,
70
+ enable_thinking=False,
71
+ )
72
+
73
+ inputs = tokenizer(
74
+ prompt,
75
+ return_tensors="pt",
76
+ ).to(model.device)
77
+
78
+ with torch.inference_mode():
79
+ outputs = model.generate(
80
+ **inputs,
81
+ max_new_tokens=int(max_tokens),
82
+ temperature=float(temperature),
83
+ top_p=float(top_p),
84
+ do_sample=True,
85
+ repetition_penalty=1.1,
86
+ )
87
+
88
+ generated = outputs[0][inputs["input_ids"].shape[1]:]
89
+
90
+ result = tokenizer.decode(
91
+ generated,
92
+ skip_special_tokens=True,
93
+ )
94
+
95
+ return result.strip()
96
+
97
+
98
+ # =========================
99
+ # Gradio UI
100
+ # =========================
101
+
102
+ with gr.Blocks(title="Qwen Humanizer") as demo:
103
+ gr.Markdown(
104
+ """
105
+ # Qwen Humanizer
106
+
107
+ Paste academic, AI-generated, or formal text and rewrite it to sound more natural while preserving meaning.
108
+ """
109
+ )
110
+
111
+ with gr.Row():
112
+ with gr.Column():
113
+ input_text = gr.Textbox(
114
+ label="Input Text",
115
+ lines=12,
116
+ placeholder="Paste text here...",
117
+ )
118
+
119
+ temperature = gr.Slider(
120
+ minimum=0.1,
121
+ maximum=1.2,
122
+ value=0.65,
123
+ step=0.05,
124
+ label="Temperature",
125
+ )
126
+
127
+ top_p = gr.Slider(
128
+ minimum=0.1,
129
+ maximum=1.0,
130
+ value=0.9,
131
+ step=0.05,
132
+ label="Top P",
133
+ )
134
+
135
+ max_tokens = gr.Slider(
136
+ minimum=64,
137
+ maximum=1024,
138
+ value=512,
139
+ step=32,
140
+ label="Max New Tokens",
141
+ )
142
+
143
+ btn = gr.Button("Humanize")
144
+
145
+ with gr.Column():
146
+ output_text = gr.Textbox(
147
+ label="Humanized Output",
148
+ lines=12,
149
+ )
150
+
151
+ btn.click(
152
+ fn=humanize_text,
153
+ inputs=[
154
+ input_text,
155
+ temperature,
156
+ top_p,
157
+ max_tokens,
158
+ ],
159
+ outputs=output_text,
160
+ )
161
+
162
+ demo.launch()