File size: 1,590 Bytes
6bdc5d4
 
 
cf56f61
baa8f63
6722d35
 
 
 
6bdc5d4
 
06cfd6d
6bdc5d4
6722d35
6bdc5d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from dotenv import load_dotenv
import os
load_dotenv()


hf_token=os.getenv("HF_TOKEN")

# Load the model and tokenizer
model_name = "openai-community/gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name,token=hf_token)

# Function to generate blog content
def generate_blog(topic, keywords):
    prompt_template = f"""
    You are a technical content writer. Write a detailed and informative blog on the following topic.

    Topic: {topic}

    Keywords: {keywords}

    Make sure the blog covers the following sections:
    1. Introduction
    2. Detailed Explanation
    3. Examples
    4. Conclusion
    
    Blog:
    """

    inputs = tokenizer(prompt_template, return_tensors="pt", max_length=512, truncation=True)
    outputs = model.generate(inputs.input_ids, max_length=800, num_return_sequences=1, temperature=0.7)
    blog_content = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return blog_content

# Gradio interface
iface = gr.Interface(
    fn=generate_blog,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter the blog topic", label="Blog Topic"),
        gr.Textbox(lines=2, placeholder="Enter keywords (comma-separated)", label="Keywords")
    ],
    outputs=gr.Textbox(label="Generated Blog Content"),
    title="Technical Blog Generator",
    description="Generate a detailed technical blog by providing a topic and relevant keywords."
)

if __name__ == "__main__":
    iface.launch()