|
import gradio as gr |
|
import torch |
|
from peft import PeftModel, PeftConfig |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
BASE_MODEL = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B" |
|
ADAPTER_REPO = "cheberle/autotrain-35swc-b4r9z" |
|
|
|
|
|
peft_config = PeftConfig.from_pretrained(ADAPTER_REPO) |
|
print("PEFT Base Model:", peft_config.base_model_name_or_path) |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True) |
|
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, trust_remote_code=True) |
|
|
|
|
|
model = PeftModel.from_pretrained(base_model, ADAPTER_REPO) |
|
|
|
def classify_text(text): |
|
""" |
|
Simple prompting approach: we ask the model to return a single classification label |
|
(e.g., 'positive', 'negative', etc.). |
|
You can refine this prompt, add chain-of-thought, or multiple classes as needed. |
|
""" |
|
prompt = f"Below is some text.\nText: {text}\nPlease classify the sentiment (positive or negative):" |
|
inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
|
with torch.no_grad(): |
|
outputs = model.generate(**inputs, max_new_tokens=64) |
|
answer = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
return answer |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## Qwen + LoRA Adapter: Text Classification Demo") |
|
input_box = gr.Textbox(lines=3, label="Enter text") |
|
output_box = gr.Textbox(lines=3, label="Model's generated output (classification)") |
|
|
|
classify_btn = gr.Button("Classify") |
|
classify_btn.click(fn=classify_text, inputs=input_box, outputs=output_box) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |