File size: 1,409 Bytes
11ef4ab
d43d27c
9118ff9
 
 
11ef4ab
9118ff9
 
613efdf
9118ff9
 
 
 
 
d0584c9
9118ff9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0584c9
9118ff9
 
d0584c9
9118ff9
 
 
 
 
 
 
 
 
d0584c9
9118ff9
 
 
 
d0584c9
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


import gradio as gr
import openai
import re

# Set up OpenAI API credentials
openai.api_key = os.environ["api"]

# Define the function that generates the blog article
def generate_article(topic):
    # Use OpenAI's GPT-3 to generate the article
    prompt = f"Write a blog post about {topic} with 5 different sections."
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt,
        max_tokens=2048,
        n=1,
        stop=None,
        temperature=0.5,
    )
    article = response.choices[0].text

    # Clean up the article text
    article = re.sub('\n', ' ', article)
    article = re.sub('\s+', ' ', article)

    # Split the article into 5 sections
    section_length = len(article) // 5
    sections = [article[i:i+section_length] for i in range(0, len(article), section_length)]

    # Combine the sections into a formatted blog post
    blog_post = f"# {topic}\n\n"
    for i in range(5):
        blog_post += f"## Section {i+1}\n\n{sections[i]}\n\n"
    
    return blog_post

# Set up the Gradio interface
iface = gr.Interface(
    generate_article,
    inputs=gr.inputs.Textbox("Enter a topic for your blog post"),
    outputs=gr.outputs.HTML(),
    title="Blog Post Generator",
    description="Generate a blog post on a given topic with 5 different sections using OpenAI's GPT-3 text model.",
)

# Launch the interface
iface.launch(share=True)