-1

I am new to Flask and I am trying to make a blog post generating bot with the GPT and the DALL-E API. But no matter what I try I always get UnboundLocalError: cannot access local variable 'content' where it is not associated with a value Can somebody help me

Here is my python code:

from flask import Flask , render_template, redirect , request
import openai

openai.api_key = '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'

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def index():
  if request.method == 'POST':
    prompT = request.form.get("prompt")
    numberimages = request.form.get("numimg")
    content = generate_comp(prompT)
    images = generate_dalle_image(prompT, numberimages)
  return render_template('index.html', content=content, images=images)


def generate_comp(promp):
  compleation = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
          {"role": "system", "content": "You are a helpful blog post writing assistant."},
          {"role": "user", "content": promp},
          ],
  max_tokens=300
  )
  r =  compleation.choices[0].text.strip()
  return r



def generate_dalle_image(prompt, num_images):
    response = openai.Image.create(
        model="image-alpha-001",  # DALL-E model for image generation
        prompt=prompt,
        n=num_images
    )
    image_url = response['data'][0]['url']
    return image_url

app.run(host="0.0.0.0", port=5000, debug=True)

and my HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SEOblog.AI</title>
    <style>
body{
    background-color: #242942;
}
form{
    display:flex;
    justify-content: center;
}
input[type="text"], input[type = "number"]{
    background-color: #40424b;
    padding: 10px 15px;
    border-radius: 10px;
    border:0;
    margin-right:10px;
}
input[type="text"]::placeholder{
    color: #919191;
}
input[type="submit"]{   
    background-color: #145e1a;
    margin-left: 5px;
    border-radius: 10px;
    border:0;
}
input[type="number"]::placeholder{
    color: #919191;
}
    </style>
</head>
<body>
    <form action="#" method="post">

<input name="prompt" type="text" placeholder= "The Middle Ages....." >
<input name="numimg" type="number" placeholder="Images" >
<input type="submit" value="Generate">
    </form>
<p>{{content}}</p>
<h2>Images:</h2>
{%for image in images%}
    <img src="{{ image }}" alt="Generated Image">
{%endfor%}
</body>
</html>

I tried with different variations of the functions but I still got the same mistake.I really don't know why it gives me that mistake when the variable content clearly has a value

1 Answers1

1

To fix the error, you can initialize the content and images variables with default values before if.

@app.route('/', methods=['GET', 'POST'])
def index():
    content = ""  # Initialize content with a default value
    images = []   # Initialize images as an empty list

Dont share your api key public!