Dayem121's picture
Update text_generation.py
190ff41 verified
Raw
History Blame Contribute Delete
1.23 kB
from transformers import pipeline
# ✅ Use a lightweight instruction-tuned model (fast for Spaces CPU)
generator = pipeline("text2text-generation", model="google/flan-t5-small")
def generate_text_content(topic, content_format="Blog Post", tone="Casual", audience="General"):
"""
Generate content (title, outline, and body) based on user input.
"""
try:
# Construct structured prompt
prompt = (
f"You are an expert content creator and copywriter.\n"
f"Create a {tone.lower()} {content_format.lower()} about {topic} "
f"for {audience if audience else 'a general audience'}.\n"
"Include:\n"
"1. Catchy title\n"
"2. Detailed outline\n"
"3. Full content\n"
"4. Call-to-action"
)
# Generate response (fast + bounded length)
response = generator(
prompt,
max_length=200, # keep small for speed
do_sample=True,
temperature=0.7,
top_p=0.9,
num_return_sequences=1,
)
return response[0]["generated_text"]
except Exception as e:
return f"⚠️ Error generating content: {str(e)}"