Update app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,7 @@ bnb_config = BitsAndBytesConfig(
|
|
| 22 |
load_in_4bit=True,
|
| 23 |
bnb_4bit_quant_type="nf4",
|
| 24 |
bnb_4bit_use_double_quant=True,
|
| 25 |
-
bnb_4bit_compute_dtype=torch.float16,
|
| 26 |
)
|
| 27 |
|
| 28 |
print("⏳ Loading Base Model in 4-bit onto T4 GPU...")
|
|
@@ -36,7 +36,15 @@ base_model = AutoModelForCausalLM.from_pretrained(
|
|
| 36 |
print("⏳ Attaching Fine-Tuned LoRA Adapter...")
|
| 37 |
model = PeftModel.from_pretrained(base_model, ADAPTER_NAME)
|
| 38 |
model.eval()
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
SYSTEM_PROMPT = (
|
| 42 |
"You are an expert human academic writer. Rewrite the provided AI text into natural, human-written prose "
|
|
@@ -57,7 +65,6 @@ def humanize_full_essay(essay_text, temperature):
|
|
| 57 |
rewritten_paragraphs = []
|
| 58 |
|
| 59 |
for p in paragraphs:
|
| 60 |
-
# Preserve short headings verbatim
|
| 61 |
if len(p.split()) <= 8 and not p.endswith("."):
|
| 62 |
rewritten_paragraphs.append(p)
|
| 63 |
yield "\n\n".join(rewritten_paragraphs)
|
|
@@ -104,7 +111,6 @@ def humanize_full_essay(essay_text, temperature):
|
|
| 104 |
|
| 105 |
rewritten_paragraphs.append(partial.strip())
|
| 106 |
|
| 107 |
-
# Clear cached VRAM between paragraphs to keep T4 memory usage flat
|
| 108 |
if torch.cuda.is_available():
|
| 109 |
torch.cuda.empty_cache()
|
| 110 |
|
|
@@ -114,7 +120,7 @@ demo = gr.Interface(
|
|
| 114 |
gr.Textbox(lines=16, label="Paste Full AI Essay"),
|
| 115 |
gr.Slider(minimum=0.3, maximum=1.0, value=0.75, step=0.05, label="Temperature"),
|
| 116 |
],
|
| 117 |
-
outputs=gr.Textbox(lines=16, label="Humanized Output (Nvidia T4
|
| 118 |
title="Academic Humanizer (Qwen2.5-14B Fine-Tuned)",
|
| 119 |
)
|
| 120 |
|
|
|
|
| 22 |
load_in_4bit=True,
|
| 23 |
bnb_4bit_quant_type="nf4",
|
| 24 |
bnb_4bit_use_double_quant=True,
|
| 25 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 26 |
)
|
| 27 |
|
| 28 |
print("⏳ Loading Base Model in 4-bit onto T4 GPU...")
|
|
|
|
| 36 |
print("⏳ Attaching Fine-Tuned LoRA Adapter...")
|
| 37 |
model = PeftModel.from_pretrained(base_model, ADAPTER_NAME)
|
| 38 |
model.eval()
|
| 39 |
+
|
| 40 |
+
print("⚡ Compiling model with torch.compile() for accelerated token generation...")
|
| 41 |
+
# Optimizes the execution graph for faster inference throughput on T4
|
| 42 |
+
try:
|
| 43 |
+
model = torch.compile(model, mode="reduce-overhead", fullgraph=False)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"⚠️ torch.compile() skipped or failed: {e}. Falling back to standard execution.")
|
| 46 |
+
|
| 47 |
+
print("✅ Model, Adapter & Compilation ready on Nvidia T4!")
|
| 48 |
|
| 49 |
SYSTEM_PROMPT = (
|
| 50 |
"You are an expert human academic writer. Rewrite the provided AI text into natural, human-written prose "
|
|
|
|
| 65 |
rewritten_paragraphs = []
|
| 66 |
|
| 67 |
for p in paragraphs:
|
|
|
|
| 68 |
if len(p.split()) <= 8 and not p.endswith("."):
|
| 69 |
rewritten_paragraphs.append(p)
|
| 70 |
yield "\n\n".join(rewritten_paragraphs)
|
|
|
|
| 111 |
|
| 112 |
rewritten_paragraphs.append(partial.strip())
|
| 113 |
|
|
|
|
| 114 |
if torch.cuda.is_available():
|
| 115 |
torch.cuda.empty_cache()
|
| 116 |
|
|
|
|
| 120 |
gr.Textbox(lines=16, label="Paste Full AI Essay"),
|
| 121 |
gr.Slider(minimum=0.3, maximum=1.0, value=0.75, step=0.05, label="Temperature"),
|
| 122 |
],
|
| 123 |
+
outputs=gr.Textbox(lines=16, label="Humanized Output (Nvidia T4 - Compiled)"),
|
| 124 |
title="Academic Humanizer (Qwen2.5-14B Fine-Tuned)",
|
| 125 |
)
|
| 126 |
|