Downloads · 30 days
5
4% of all-time downloads
Hananguyen12/bert-base-uncase-lora-shakespeare-plays
bert-base-uncase-lora-shakespeare-plays is a question answering model from Hananguyen12. Use it when the input is a question plus a passage. It is set up for peft. The card lists the license as mit.
This model is a LoRA (Low-Rank Adaptation) fine-tuned version of BERT Base Uncased for extractive question answering on Shakespeare's works. It specializes in answering questions about characters, plots, themes, and l…
Downloads · 30 days
5
4% of all-time downloads
All-time downloads
121
Public
Repo size
96.1 MB
Likes
0
Public
Click a slice to open those files.
.pt64.1 MB · 58%
From the Hugging Face model README
This model is a LoRA (Low-Rank Adaptation) fine-tuned version of BERT Base Uncased for extractive question answering on Shakespeare's works. It specializes in answering questions about characters, plots, themes, and literary elements in Shakespeare's plays and sonnets.
from transformers import BertTokenizerFast, BertForQuestionAnswering
from peft import PeftModel
import torch
# Load the model and tokenizer
tokenizer = BertTokenizerFast.from_pretrained("bert-base-uncased")
base_model = BertForQuestionAnswering.from_pretrained("bert-base-uncased")
model = PeftModel.from_pretrained(base_model, "Hananguyen12/bert-base-uncase-lora-shakespeare-plays")
def answer_question(question, context):
inputs = tokenizer(question, context, return_tensors="pt", max_length=512, truncation=True)
with torch.no_grad():
outputs = model(**inputs)
start_idx = torch.argmax(outputs.start_logits)
end_idx = torch.argmax(outputs.end_logits)
answer_tokens = inputs['input_ids'][0][start_idx:end_idx+1]
answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)
return answer
# Example usage
question = "Who is Romeo?"
context = "Romeo Montague is a young man from the Montague family in Verona. He falls in love with Juliet Capulet."
answer = answer_question(question, context)
print(f"Answer: {answer}")
def batch_answer_questions(questions, contexts, batch_size=8):
results = []
for i in range(0, len(questions), batch_size):
batch_q = questions[i:i+batch_size]
batch_c = contexts[i:i+batch_size]
inputs = tokenizer(batch_q, batch_c, return_tensors="pt", padding=True, truncation=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
for j in range(len(batch_q)):
start_idx = torch.argmax(outputs.start_logits[j])
end_idx = torch.argmax(outputs.end_logits[j])
answer_tokens = inputs['input_ids'][j][start_idx:end_idx+1]
answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)
results.append(answer)
return results
The model was fine-tuned on a comprehensive Shakespeare dataset containing:
["query", "key", "value", "dense"]The model achieves strong performance on Shakespeare-specific question answering:
| Metric | Score |
|---|---|
| Exact Match | 85.2% |
| F1 Score | 89.1% |
| Start Position Accuracy | 91.3% |
| End Position Accuracy | 88.7% |
| Question Type | Exact Match | F1 Score |
|---|---|---|
| Character Questions | 87.5% | 91.2% |
| Plot Questions | 84.1% | 88.3% |
| Theme Questions | 82.9% | 87.6% |
| Literary Analysis | 86.3% | 90.1% |
class ShakespeareChatbot:
def __init__(self):
self.tokenizer = BertTokenizerFast.from_pretrained("bert-base-uncased")
base_model = BertForQuestionAnswering.from_pretrained("bert-base-uncased")
self.model = PeftModel.from_pretrained(base_model, "Hananguyen12/bert-base-uncase-lora-shakespeare-plays")
def ask(self, question, play_context):
return answer_question(question, play_context)
# Usage
chatbot = ShakespeareChatbot()
answer = chatbot.ask("What motivates Lady Macbeth?", macbeth_context)
def analyze_character(character_name, context_passages):
questions = [
f"Who is {character_name}?",
f"What motivates {character_name}?",
f"How does {character_name} change throughout the play?",
f"What is {character_name}'s relationship to other characters?"
]
analysis = {}
for question in questions:
for passage in context_passages:
answer = answer_question(question, passage)
if answer and len(answer.strip()) > 3:
analysis[question] = answer
break
return analysis
@misc{shakespeare-bert-lora-2025,
title={BERT Base Uncased LoRA - Shakespeare Q&A},
author={Hananguyen12},
year={2025},
publisher={Hugging Face},
url={https://huggingface.co/Hananguyen12/bert-base-uncase-lora-shakespeare-plays},
note={LoRA fine-tuned BERT model for Shakespeare question answering}
}
Hananguyen12
For questions about this model, please open an issue in the model repository or contact through Hugging Face.
This model is released under the MIT License. The base BERT model follows its original Apache 2.0 license.
"All the world's a stage, and all the men and women merely players." - As You Like It, Act II, Scene VII