Downloads · 30 days
11
4% of all-time downloads
8lianno/llama_poetry_fa
llama_poetry_fa is a text generation model from 8lianno. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as mit.
<p align="center" <img src="https://huggingface.co/8lianno/llamapoetryfa/resolve/main/logo.jpeg" alt="Model Logo" width="400" height="400" </p
Downloads · 30 days
11
4% of all-time downloads
All-time downloads
271
Public
Repo size
4.2 GB
Likes
1
Public
Click a slice to open those files.
.safetensors4.2 GB · 100%
From the Hugging Face model README
Model Name: DivAIn (دیوان) – A Persian Poetry-Driven Llama-Based Language Model Model URL: https://huggingface.co/8lianno/llama_poetry_fa
llama_poetry_fa is a Persian poetry generation model fine-tuned from a Llama 3.1-based checkpoint. It aims to produce stylistically coherent, culturally relevant, and metrically sound verses in response to a user’s prompt. The model focuses on generating output that aligns with classical Persian poetic traditions, such as maintaining consistent rhyme schemes and thematic unity.
meta-llama/Llama-3.1-8B-InstructThis model has been trained to understand Persian poetic structure, using additional tokens [شروع_شعر], [پایان_شعر], and [مصرع] to guide and maintain poem formatting. It can respond to various thematic prompts—whether about nature, love, historical figures, or abstract concepts—and produce a poem that attempts to follow Persian poetry norms.
This model is intended to generate Persian poetry in response to user prompts. It can be used as a creative writing aid, a tool for literary exploration, or as a source of inspiration for authors, poets, and students studying Persian literature.
Example:
Prompt: "درباره بهار شعری بسرایید"
Response:
Prompt: "درباره دریا شعری بسرایید"
Response:
Prompt: "درباره عشق بنویسید"
Response:
Prompt: "آسمان چرا می گرید؟"
Response:
While primarily designed for poetry generation, the model can be integrated into:
The model is not designed for:
The model’s content is drawn from its training data and may reflect certain cultural or poetic biases. It might:
Users should critically evaluate the generated text, especially if used in public-facing contexts.
pip install -U transformers>=4.30.0
pip install -U accelerate
pip install bitsandbytes==0.42.0
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel
class PoetryGenerator:
def __init__(self, model_path, token):
self.token = token
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# Configure quantization settings
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True
)
# Load tokenizer from the base model used during fine-tuning
self.tokenizer = AutoTokenizer.from_pretrained(
"meta-llama/Llama-3.1-8B-Instruct",
token=token,
trust_remote_code=True
)
self.tokenizer.pad_token = self.tokenizer.eos_token
# Add the special tokens that were used during training
special_tokens = {
"additional_special_tokens": [
"[شروع_شعر]",
"[پایان_شعر]",
"[مصرع]"
]
}
self.tokenizer.add_special_tokens(special_tokens)
# Load the base model
base_model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-3.1-8B-Instruct",
token=token,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.float16,
quantization_config=bnb_config
)
# Resize token embeddings to match tokenizer
base_model.resize_token_embeddings(len(self.tokenizer))
# Load the fine-tuned model from Hugging Face Hub
self.model = PeftModel.from_pretrained(
base_model,
model_path,
token=token,
device_map="auto"
)
self.model.eval()
def generate_poem(self, prompt):
formatted_prompt = f"""سوال: {prompt}
لطفا یک شعر فارسی در پاسخ به این سوال بسرایید که دارای وزن و قافیه مناسب باشد.
شعر:"""
inputs = self.tokenizer(formatted_prompt, return_tensors="pt", padding=True)
inputs = {k: v.to(self.device) for k, v in inputs.items()}
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_length=512,
num_return_sequences=1,
temperature=0.7,
top_p=0.9,
do_sample=True,
pad_token_id=self.tokenizer.pad_token_id,
eos_token_id=self.tokenizer.eos_token_id
)
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
def main():
# Use the Hugging Face Hub model path instead of a local path
generator = PoetryGenerator(
model_path="8lianno/llama_poetry_fa",
token="<YOUR_HF_TOKEN>"
)
prompts = [
"درباره بهار شعری بسرایید",
"شعری درباره عشق بنویسید",
"درباره دریا شعری بسرایید"
]
print("=== Persian Poetry Generation ===\n")
for i, prompt in enumerate(prompts, 1):
print(f"\nPrompt {i}: {prompt}")
print("\nGenerated Poetry:")
try:
poem = generator.generate_poem(prompt)
print(poem)
print("\n" + "="*50)
except Exception as e:
print(f"Error generating poem: {str(e)}")
print(f"Error type: {type(e)}")
if __name__ == "__main__":
main()