Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# 1️⃣ Load
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
# 3️⃣ Define
|
| 19 |
def generate_text(prompt, max_tokens=50):
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
# 4️⃣
|
| 24 |
iface = gr.Interface(
|
| 25 |
fn=generate_text,
|
| 26 |
inputs=[
|
|
@@ -28,9 +29,9 @@ iface = gr.Interface(
|
|
| 28 |
gr.Slider(minimum=10, maximum=200, step=10, label="Max Tokens")
|
| 29 |
],
|
| 30 |
outputs=gr.Textbox(label="Generated Text"),
|
| 31 |
-
title="FunFox
|
| 32 |
-
description="
|
| 33 |
)
|
| 34 |
|
| 35 |
-
# 5️⃣ Launch
|
| 36 |
iface.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from peft import PeftModel
|
| 5 |
|
| 6 |
+
# 1️⃣ Load base model
|
| 7 |
+
base_model_name = "TRM-coding/PythonCopilot"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 9 |
+
tokenizer.pad_token = tokenizer.eos_token # important for causal LM
|
| 10 |
|
| 11 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, torch_dtype=torch.float16).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
|
| 13 |
+
# 2️⃣ Load PEFT model
|
| 14 |
+
peft_model_name = "DSDUDEd/funfox"
|
| 15 |
+
model = PeftModel.from_pretrained(base_model, peft_model_name)
|
| 16 |
+
model.eval()
|
|
|
|
| 17 |
|
| 18 |
+
# 3️⃣ Define text generation function
|
| 19 |
def generate_text(prompt, max_tokens=50):
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 21 |
+
outputs = model.generate(**inputs, max_new_tokens=max_tokens)
|
| 22 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
+
# 4️⃣ Build Gradio interface
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=generate_text,
|
| 27 |
inputs=[
|
|
|
|
| 29 |
gr.Slider(minimum=10, maximum=200, step=10, label="Max Tokens")
|
| 30 |
],
|
| 31 |
outputs=gr.Textbox(label="Generated Text"),
|
| 32 |
+
title="FunFox PEFT Model",
|
| 33 |
+
description="FunFox LoRA model fine-tuned on PythonCopilot base."
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# 5️⃣ Launch
|
| 37 |
iface.launch(share=True)
|