Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +26 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Load the tokenizer
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("rxpbtn21/t5-small-lora-summarizer")
|
| 9 |
+
|
| 10 |
+
# Load the base model and then the LoRA adapter
|
| 11 |
+
base_model = AutoModelForSeq2SeqLM.from_pretrained("t5-small", device_map="auto")
|
| 12 |
+
model = PeftModel.from_pretrained(base_model, "rxpbtn21/t5-small-lora-summarizer")
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
def summarize(text):
|
| 16 |
+
inputs = tokenizer(text, max_length=512, truncation=True, return_tensors="pt")
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model.generate(inputs["input_ids"].to(model.device), num_beams=4, max_new_tokens=128, early_stopping=True)
|
| 19 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 20 |
+
return summary
|
| 21 |
+
|
| 22 |
+
# Create Gradio interface
|
| 23 |
+
iface = gr.Interface(fn=summarize, inputs="text", outputs="text", title="LoRA Fine-tuned T5-small Summarizer")
|
| 24 |
+
|
| 25 |
+
# Launch the interface
|
| 26 |
+
iface.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
peft
|
| 5 |
+
accelerate
|
| 6 |
+
gradio
|
| 7 |
+
sentencepiece
|