Situation
My Python script compiles and runs successfully. It creates the output file (edited.txt), but doesn't write anything to the file. API dashboard shows no usage, so I'm guessing the script never successfully calls the API. However, I'm not receiving any error codes.
Objective:
I'm writing a Python script to read in blocks of text from a file. The text blocks consist of two parts; a prompt and then a 2,000 token (approximate) text body. The script is supposed to then pass that block of text to the OpenAI API and write the response to a file called "edited.txt"
Purpose:
I'm helping the 100 Devs boot camp transform their video transcripts into HTML docs so blind and visually-impaired people can navigate the video transcripts. I've already written three Python scripts that...
- strip away time stamps
- split the text into chunks based on token count (using TikToken)
- appends the same prompt to the start of each paragraph
This script is intended to do the first editing pass on the raw transcript text before I manually edit and add HTMl formatting.
Sample Input:
(here's an example of what's on the text file being fed to this script)
Format:
prompt-text: raw-transcript-text
prompt-text: raw-transcript-text
Example:
Act as a software developer. Your job is to revise the following text to be more readable while maintaining any code syntax: We're back, did it again, did it again. Let's go. Fingers only. Hey, good morning. Good afternoon. Good evening. No matter where you're coming from. Hope you all are doing well. Welcome back everybody. ...
Act as a software developer. Your job is to revise the following text to be more readable while maintaining any code syntax: A lot of this stuff can just be a real torture in the beginning if you've never touched code. So we have folks from all ranges here. We've had folks that have like built full stack apps already and they're here to learn stuff that's going to help them get a job. ...
Source Code:
from flask import Flask, request, redirect, url_for, render_template
import os
from dotenv import load_dotenv
import openai
import json
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
MODEL_ENGINE = "gpt-3.5-turbo"
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file:
data = file.read().decode('utf-8')
blocks = data.split('\n\n')
edited_text = ''
for block in blocks:
try:
response = openai.chat.completions.create(
model=MODEL_ENGINE,
messages=[{"role": "user", "content": block}],
max_tokens=4000,
)
edited_text += response.choices[0].text.strip() + '\n\n'
except Exception as e:
print(f"An error occurred: {e}")
with open('edited.txt', 'w', encoding='utf-8') as f:
f.write(edited_text)
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True, port=8080)
Notes:
This s my first time accessing the OpenAI API. Also I'm blind so accessing debuggers is tricky. From what I can access I hear a 200 response code for the GET and POST requests. But it's possible I'm not able to access something.
Any help you can offer is greatly appreciated!
What I've Tried:
Read all available OpenAI documentation
Checked Flask events in terminal (all came back 200)
Tried using Copilot from within VS Code
Verified .env file is in same directory with properly formatted API key.
Checked API dashboard to verify key is active, has permissions and shows no usage
Verified script is creating output file as expected, but not writing to it.
When I run your code on my side, I was receiving the error:
so i replaced the code with:
I also didn't access the file from the request, files instead I did this:
The code is running fine now without any bugs.
openai.chat.completions.create method is tailored for chat-based interactions. It's designed to maintain context over a series of messages, making it suitable for creating conversational AI. So if you are not planning to build a conversation AI use the following code:
This is good practice.