Tobias Geisler
concurrency limit = 1
b0362bf
import os
from openai import OpenAI
import gradio as gr
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
# Ensure the OPENAI_API_KEY environment variable is set; it's automagically loaded into the client
client = OpenAI()
if client.api_key is None:
raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.")
# Load the APP_PASSWORD from the environment
APP_PASSWORD = os.getenv("APP_PASSWORD")
def generate_image(hair_color, eye_color, person, age, style, password):
if password != APP_PASSWORD:
return "Incorrect password.", None
# Constructing the prompt based on user input
prompt = f"Create a beautiful artistic profile picture of a smiling {age} year-old {person} with {hair_color} hair, {eye_color} eyes. Style: {style}."
# Displaying the prompt to the user
print("Prompt:", prompt)
try:
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1
)
# Get the image URL from the response
image_url = response.data[0].url
return prompt, image_url
except Exception as e:
print("An error occurred:", e)
return "An error occurred while generating the image.", None
hair_color_options = ["Black", "Brown", "Blonde", "Red", "Gray", "White", "Green", "Rainbow", "Blue"]
eye_color_options = ["Blue", "Brown", "Green", "Gray", "Hazel"]
person_options = ["Boy", "Girl"]
style_options = ["Fortnite Character", "3D Pixar Render", "Picasso Painting", "Van Gogh Painting", "Abstract Art", "Realistic Portrait"]
# Define the Gradio interface
iface = gr.Interface(
fn=generate_image,
inputs=[
gr.Dropdown(choices=hair_color_options, label="Hair Color"),
gr.Dropdown(choices=eye_color_options, label="Eye Color"),
gr.Dropdown(choices=person_options, label="Person"),
gr.Number(label="Age", value=12, minimum=8, maximum=120, step=1, precision=0),
gr.Dropdown(choices=style_options, label="Style"),
gr.Textbox(label="Password", type="password"),
],
outputs=[gr.Text(label="Prompt"), gr.Image(label="Generated Image")],
title="Profile Picture Generator",
description="Enter the attributes for your profile picture.",
concurrency_limit=1 # Set concurrency limit here
)
# Launch the Gradio app
iface.launch()