|
import gradio as gr |
|
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
import re |
|
import time |
|
|
|
|
|
model_id = "Gensyn/Qwen2.5-0.5B-Instruct" |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
model = AutoModelForCausalLM.from_pretrained(model_id) |
|
|
|
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1) |
|
|
|
|
|
def generate_news_content(title, progress=gr.Progress(track_tqdm=True)): |
|
prompt = f""" |
|
You are a professional news content writer. |
|
|
|
Given a news title, generate the following in English: |
|
|
|
1. A news article of 300-400 words. |
|
2. A catchy SEO title (max 70 characters). |
|
3. A meta description (max 150 characters). |
|
|
|
News Title: "{title}" |
|
|
|
Output format strictly like this: |
|
### Article: |
|
<article> |
|
|
|
### SEO Title: |
|
<seo title> |
|
|
|
### Meta Description: |
|
<meta description> |
|
""" |
|
|
|
|
|
progress(0, desc="Generating content...") |
|
|
|
|
|
result = generator(prompt, max_new_tokens=192, temperature=0.7, top_p=0.9) |
|
text = result[0]['generated_text'] |
|
|
|
|
|
progress(0.5, desc="Parsing results...") |
|
|
|
|
|
article = re.search(r"### Article:\s*(.*?)\s*### SEO Title:", text, re.DOTALL) |
|
seo_title = re.search(r"### SEO Title:\s*(.*?)\s*### Meta Description:", text, re.DOTALL) |
|
meta_desc = re.search(r"### Meta Description:\s*(.*)", text, re.DOTALL) |
|
|
|
article_text = article.group(1).strip() if article else "Article not generated." |
|
seo_title_text = seo_title.group(1).strip() if seo_title else "SEO Title not generated." |
|
meta_desc_text = meta_desc.group(1).strip() if meta_desc else "Meta Description not generated." |
|
|
|
|
|
progress(1, desc="Done!") |
|
|
|
return article_text, seo_title_text, meta_desc_text |
|
|
|
|
|
title_input = gr.Textbox(label="News Title", placeholder="Enter a news title here") |
|
|
|
article_output = gr.Textbox(label="Generated Article", lines=20) |
|
seo_title_output = gr.Textbox(label="SEO Title") |
|
meta_desc_output = gr.Textbox(label="Meta Description") |
|
|
|
gr.Interface( |
|
fn=generate_news_content, |
|
inputs=title_input, |
|
outputs=[article_output, seo_title_output, meta_desc_output], |
|
title="AI News Article Generator (Qwen2.5-0.5B Instruct)", |
|
description="Fast article, SEO title, and meta description generator using Qwen2.5-0.5B Instruct model." |
|
).launch() |
|
|