CoolFace
Apppublic

CHA0sTIG3R/BlogWriterApp

sourceHugging Faceupdated 3y agoView on Hugging Face
37likes
app.py46 linesDownload Raw Back to root
1from flask import Flask, render_template, request2from dotenv import load_dotenv, find_dotenv3from openai import OpenAI4 5load_dotenv(find_dotenv())6 7app = Flask(__name__)8client = OpenAI()9 10def generate_post(tone, topic, length, instructions):11    prompt = f"Write a {tone} blog post about {topic}. Make sure the blog post is no longer than {length} words and ends with a conclusion. {instructions}"12    response = client.chat.completions.create(13        model="gpt-3.5-turbo",14        messages=[15            {"role": "system", "content": f"You are an expert {tone} blogger and creative writer."},16            {"role": "user", "content": prompt}17        ],18        max_tokens=600,19        temperature=0.7,20        top_p=0.9,21        frequency_penalty=0,22        presence_penalty=0,23        stop=["In conclusion", "In summary"]24    )25    return response.choices[0].message.content.strip()26 27@app.route('/')28def index():29    return render_template('index.html')30 31@app.route('/generate', methods=['POST'])32def generate_blog():33    # Retrieve form data34    topic = request.form.get('topic') 35    tone = request.form.get('tone')36    length = request.form.get('length')37    instructions = request.form.get('instructions')38    39    # Here, you would add your model inference code to generate the blog post40    generated_text = generate_post(tone, topic, length, instructions)41 42    return render_template('results.html', generated_text=generated_text)43 44if __name__ == '__main__':45    app.run(debug=True)46