Я постоянно получаю эту ошибку:
APIRmovedInV1: вы пытались получить доступ к openai.Image, но это
больше не поддерживается в openai>=1.0.0 — см. README по адресу
https://github.com/openai/openai-python для API. Вы можете бежать
Код: Выделить всё
openai migrate1.0.0. Альтернативно вы можете прикрепить свою установку к старой версии, например. pip install openai==0.28 Подробное
руководство по миграции доступно здесь:
https://github.com/openai/openai-python/discussions/742
Это мой код:
Код: Выделить всё
import streamlit as st
import openai
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import numpy as np
# Set up your OpenAI API key
openai.api_key = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# Function to calculate the brightness of an image
def calculate_brightness(image):
# Convert image to grayscale
gray_image = image.convert("L")
# Calculate average pixel brightness
brightness = np.mean(np.array(gray_image))
return brightness
# Function to determine text color based on brightness
def get_text_color(background_image):
brightness_threshold = 127.5
brightness = calculate_brightness(background_image)
if brightness < brightness_threshold:
return "white"
else:
return "black"
# Function to generate logo image based on organization name
def generate_logo_image(organization_name):
# Split organization name into words
words = organization_name.split()
# If organization name has more than one word, take the first letter of each word
if len(words) > 1:
logo_text = "".join(word[0] for word in words)
# If organization name has only one word, take the first letter
else:
logo_text = organization_name[0]
# Define font and size for logo
logo_font_size = 64
logo_font = ImageFont.truetype("arialbd.ttf", logo_font_size) # Use a bold font for the logo
# Create a blank white image for the logo
logo_image = Image.new("RGB", (logo_font.getbbox(logo_text)[2], logo_font.getbbox(logo_text)[3]), "white")
draw = ImageDraw.Draw(logo_image)
# Calculate text position to center the logo
text_x = (logo_image.width - draw.textlength(logo_text, font=logo_font)) / 2
text_y = (logo_image.height - logo_font_size) / 2
# Add text to the logo image
draw.text((text_x, text_y), logo_text, fill="black", font=logo_font)
return logo_image
# Function to generate business card image
def generate_business_card(name, designation, contact_number, email_address, logo_image, background_image):
# Create a blank white image
card_width, card_height = 3.5 * 300, 2 * 300 # Standard business card size: 3.5 x 2 inches at 300 DPI
business_card = Image.new("RGB", (int(card_width), int(card_height)), "white")
# Add background image if provided
if background_image:
business_card.paste(background_image, (0, 0))
draw = ImageDraw.Draw(business_card)
# Determine text color based on background image brightness
text_color = get_text_color(background_image)
# Add logo image to the header
if logo_image:
logo_width, logo_height = logo_image.size
logo_x = (card_width - logo_width) / 2
logo_y = 20
business_card.paste(logo_image, (int(logo_x), int(logo_y)))
# Define font and size for text
font_size = 36
font = ImageFont.truetype("arialbd.ttf", font_size) # Use a bold font
# Define contact information
contact_info = f"{name}\n{designation}\n{contact_number}\n{email_address}"
# Calculate text width for contact information
contact_text_width = max(draw.textlength(line, font=font) for line in contact_info.split("\n"))
# Add contact information to the card
contact_text_x = (card_width - contact_text_width) / 2
contact_text_y = (card_height - font_size * 4) / 2
draw.multiline_text((contact_text_x, contact_text_y), contact_info, fill=text_color, font=font, align="center")
return business_card
# Define Streamlit app
def main():
st.title("Business Card Generator")
# Input fields for user information
name = st.text_input("Enter Name:")
designation = st.text_input("Enter Designation:")
contact_number = st.text_input("Enter Contact Number:")
email_address = st.text_input("Enter Email Address:")
organization_name = st.text_input("Enter Organization Name:")
# Generate logo image based on organization name
if organization_name:
logo_image = generate_logo_image(organization_name)
else:
logo_image = None
# Prompt for background image generation
st.header("Background Image Generation")
background_prompt = st.text_area("Provide a prompt for the background image generation:")
# Button to generate business card
if st.button("Generate Business Card"):
if name and designation and contact_number and email_address:
# Generate background image
if background_prompt:
background_response = openai.Image.create(prompt=background_prompt)
background_image = Image.open(background_response.url)
else:
background_image = None
# Generate business card image
business_card_image = generate_business_card(name, designation, contact_number, email_address, logo_image, background_image)
# Display business card image
st.image(business_card_image, caption="Business Card", use_column_width=True)
# Run the app
if __name__ == "__main__":
main()
Я пытался исправить свой код в связи с этим, но я потерял весь поток, поэтому мне пришлось вернуть его обратно к базовому коду, над которым я работал, который я добавил в сообщение.
Кто-нибудь может помочь мне с моей проблемой? Я знаю, что почти у цели, но все же чувствую, что зашел слишком далеко. Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/781 ... nger-suppo
Мобильная версия