I am very new to python and only know the basics, so basically I am calling openai and getting a response in return and want to write that response in a .txt file.
I want to convert the response in json before writing in the file. my response is already in json format but weird when print it shows json format with json {}
with it, this is my script
def get_json(image_file, category):
with open(image_file, "rb") as image:
response = openai_client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": f"Analyze this image and provide the following attributes: color theme, font style, and a short description of about 4-7 words. Categorize it as {category}. Return the result as a JSON object."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64.b64encode(image.read()).decode()}"}},
],
}
],
temperature=1,
max_tokens=4095,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
return response.choices[0].message.content
with open(file, 'a') as file:
for filename in os.listdir(images_folder):
filepath = os.path.join(images_folder, filename)
result =get_json(filepath, 'hero')
file.write(result + '\n')
json_result = json.loads(result)
print(json_result)
this is the result i am getting enter image description here
i want to remove the text '''json'''
tried to convert it into json by json.loads(result)
but getting the following error:-
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
You are asking it to return a JSON object in you prompt, "Return the result as a JSON object." that it why! If you give it the same prompt using the website, you will notice that the response is nicely formatted, that is because of those "```json ...content ```" markdown formatting.
You can solve it using two methods:
import json
# For example, this is the content in response:
response = '```json{"color_theme": "Mint Green and Black","font_style": "Sans-serif","short_description": "Web Publishing Platform","category": "hero"}```'
# Replace and assign back to original content
response = response.replace("```json", "")
response = response.replace("```", "")
# Don't forget to convert to JSON as it is a string right now:
json_result = json.loads(response)
import json
# For example, this is the content in response:
response = '```json{"color_theme": "Mint Green and Black","font_style": "Sans-serif","short_description": "Web Publishing Platform","category": "hero"}```'
# "```json" is 7 character long, but slicing count start from 0. "{" is at 7th character.
# "```" is 3 character long (at the end).
response = response[7:-3]
# Don't forget to convert to JSON as it is a string right now:
json_result = json.loads(response)
And for writing to the .txt
file, you can use json.dump()
as follows:
import json
response = '```json{"color_theme": "Mint Green and Black","font_style": "Sans-serif","short_description": "Web Publishing Platform","category": "hero"}```'
response = response[7:-3]
response = json.loads(response)
# Write to response.txt file (overwriting it).
with open("response.txt", "w") as file:
json.dump(response, file)