Spaces:
Running
Running
Revert app.py
Browse files
app.py
CHANGED
|
@@ -6,21 +6,23 @@ from dotenv import load_dotenv
|
|
| 6 |
# Load environment variables from the .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
|
|
|
| 9 |
client = OpenAI()
|
|
|
|
| 10 |
if client.api_key is None:
|
| 11 |
raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.")
|
| 12 |
|
|
|
|
| 13 |
APP_PASSWORD = os.getenv("APP_PASSWORD")
|
| 14 |
|
| 15 |
def generate_image(hair_color, eye_color, person, age, style, password):
|
| 16 |
if password != APP_PASSWORD:
|
| 17 |
return "Incorrect password.", None
|
| 18 |
|
| 19 |
-
prompt
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
print("Prompt:", prompt)
|
| 25 |
|
| 26 |
try:
|
|
@@ -28,38 +30,39 @@ def generate_image(hair_color, eye_color, person, age, style, password):
|
|
| 28 |
model="dall-e-3",
|
| 29 |
prompt=prompt,
|
| 30 |
size="1024x1024",
|
|
|
|
| 31 |
n=1
|
| 32 |
)
|
|
|
|
|
|
|
| 33 |
image_url = response.data[0].url
|
|
|
|
| 34 |
return prompt, image_url
|
| 35 |
except Exception as e:
|
| 36 |
print("An error occurred:", e)
|
| 37 |
return "An error occurred while generating the image.", None
|
| 38 |
|
| 39 |
hair_color_options = ["Black", "Brown", "Blonde", "Red", "Gray", "White", "Green", "Rainbow", "Blue"]
|
| 40 |
-
eye_color_options
|
| 41 |
-
person_options
|
| 42 |
-
style_options
|
| 43 |
-
"Van Gogh Painting", "Abstract Art", "Realistic Portrait"]
|
| 44 |
|
|
|
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=generate_image,
|
| 47 |
inputs=[
|
| 48 |
gr.Dropdown(choices=hair_color_options, label="Hair Color"),
|
| 49 |
-
gr.Dropdown(choices=eye_color_options,
|
| 50 |
-
gr.Dropdown(choices=person_options,
|
| 51 |
-
gr.Number(label="Age", value=12, minimum=8, maximum=120, step=1),
|
| 52 |
-
gr.Dropdown(choices=style_options,
|
| 53 |
gr.Textbox(label="Password", type="password"),
|
| 54 |
],
|
| 55 |
-
outputs=[
|
| 56 |
-
gr.Textbox(label="Prompt"),
|
| 57 |
-
gr.Image(label="Generated Image")
|
| 58 |
-
],
|
| 59 |
title="Profile Picture Generator",
|
| 60 |
description="Enter the attributes for your profile picture.",
|
| 61 |
-
concurrency_limit=1
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 6 |
# Load environment variables from the .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
# Ensure the OPENAI_API_KEY environment variable is set; it's automagically loaded into the client
|
| 10 |
client = OpenAI()
|
| 11 |
+
|
| 12 |
if client.api_key is None:
|
| 13 |
raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.")
|
| 14 |
|
| 15 |
+
# Load the APP_PASSWORD from the environment
|
| 16 |
APP_PASSWORD = os.getenv("APP_PASSWORD")
|
| 17 |
|
| 18 |
def generate_image(hair_color, eye_color, person, age, style, password):
|
| 19 |
if password != APP_PASSWORD:
|
| 20 |
return "Incorrect password.", None
|
| 21 |
|
| 22 |
+
# Constructing the prompt based on user input
|
| 23 |
+
prompt = f"Create a beautiful artistic profile picture of a smiling {age} year-old {person} with {hair_color} hair, {eye_color} eyes. Style: {style}."
|
| 24 |
+
|
| 25 |
+
# Displaying the prompt to the user
|
|
|
|
| 26 |
print("Prompt:", prompt)
|
| 27 |
|
| 28 |
try:
|
|
|
|
| 30 |
model="dall-e-3",
|
| 31 |
prompt=prompt,
|
| 32 |
size="1024x1024",
|
| 33 |
+
quality="standard",
|
| 34 |
n=1
|
| 35 |
)
|
| 36 |
+
|
| 37 |
+
# Get the image URL from the response
|
| 38 |
image_url = response.data[0].url
|
| 39 |
+
|
| 40 |
return prompt, image_url
|
| 41 |
except Exception as e:
|
| 42 |
print("An error occurred:", e)
|
| 43 |
return "An error occurred while generating the image.", None
|
| 44 |
|
| 45 |
hair_color_options = ["Black", "Brown", "Blonde", "Red", "Gray", "White", "Green", "Rainbow", "Blue"]
|
| 46 |
+
eye_color_options = ["Blue", "Brown", "Green", "Gray", "Hazel"]
|
| 47 |
+
person_options = ["Boy", "Girl"]
|
| 48 |
+
style_options = ["Fortnite Character", "3D Pixar Render", "Picasso Painting", "Van Gogh Painting", "Abstract Art", "Realistic Portrait"]
|
|
|
|
| 49 |
|
| 50 |
+
# Define the Gradio interface
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=generate_image,
|
| 53 |
inputs=[
|
| 54 |
gr.Dropdown(choices=hair_color_options, label="Hair Color"),
|
| 55 |
+
gr.Dropdown(choices=eye_color_options, label="Eye Color"),
|
| 56 |
+
gr.Dropdown(choices=person_options, label="Person"),
|
| 57 |
+
gr.Number(label="Age", value=12, minimum=8, maximum=120, step=1, precision=0),
|
| 58 |
+
gr.Dropdown(choices=style_options, label="Style"),
|
| 59 |
gr.Textbox(label="Password", type="password"),
|
| 60 |
],
|
| 61 |
+
outputs=[gr.Text(label="Prompt"), gr.Image(label="Generated Image")],
|
|
|
|
|
|
|
|
|
|
| 62 |
title="Profile Picture Generator",
|
| 63 |
description="Enter the attributes for your profile picture.",
|
| 64 |
+
concurrency_limit=1 # Set concurrency limit here
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Launch the Gradio app
|
| 68 |
+
iface.launch()
|