Spaces:
Runtime error
Runtime error
Commit
·
8a4cad7
1
Parent(s):
a8450e3
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,38 @@
|
|
| 1 |
-
|
| 2 |
import openai
|
| 3 |
-
import gradio
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Define
|
| 10 |
def generate_text(prompt):
|
| 11 |
response = openai.Completion.create(
|
| 12 |
-
|
| 13 |
prompt=prompt,
|
| 14 |
-
max_tokens=
|
| 15 |
n=1,
|
| 16 |
stop=None,
|
| 17 |
-
temperature=0.
|
| 18 |
)
|
| 19 |
return response.choices[0].text.strip()
|
| 20 |
|
| 21 |
-
# Define
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
image_return = model.predict(model_prefix + ": " + text)
|
| 33 |
-
else:
|
| 34 |
-
image_return = None
|
| 35 |
-
|
| 36 |
-
# Return the generated image
|
| 37 |
-
return image_return[0] if image_return is not None else None
|
| 38 |
-
|
| 39 |
-
# Define the Gradio interface
|
| 40 |
-
prompt_input = Textbox("Enter a prompt", lines=3)
|
| 41 |
-
text_output = Label("")
|
| 42 |
-
image_output = Image()
|
| 43 |
-
|
| 44 |
-
iface = gr.Interface(
|
| 45 |
-
generate_image,
|
| 46 |
-
inputs=prompt_input,
|
| 47 |
-
outputs=[text_output, image_output],
|
| 48 |
-
title="Image Generator",
|
| 49 |
-
description="Generates an image based on a prompt using OpenAI's GPT-3 and DALL-E models."
|
| 50 |
)
|
| 51 |
|
| 52 |
-
# Update the output boxes with the generated text and image
|
| 53 |
-
def update_text_output(text, image_url):
|
| 54 |
-
text_output.text = text
|
| 55 |
-
text_output.style['font-size'] = '8px' # Change the font size here
|
| 56 |
-
|
| 57 |
-
iface.interface_result_hooks = [update_text_output]
|
| 58 |
-
|
| 59 |
# Launch the interface
|
| 60 |
-
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
+
import gradio
|
| 3 |
+
|
| 4 |
+
# Initialize OpenAI API credentials
|
| 5 |
+
openai.api_key = "YOUR_API_KEY"
|
| 6 |
|
| 7 |
+
# Load the text-to-image generation model
|
| 8 |
+
model_path = "models/dreamlike-art/dreamlike-photoreal-2.0"
|
| 9 |
+
model_prefix = "photoreal style"
|
| 10 |
+
model = gradio.Interface.load(model_path)
|
| 11 |
|
| 12 |
+
# Define a function to generate text response using OpenAI API
|
| 13 |
def generate_text(prompt):
|
| 14 |
response = openai.Completion.create(
|
| 15 |
+
engine="davinci",
|
| 16 |
prompt=prompt,
|
| 17 |
+
max_tokens=1024,
|
| 18 |
n=1,
|
| 19 |
stop=None,
|
| 20 |
+
temperature=0.7,
|
| 21 |
)
|
| 22 |
return response.choices[0].text.strip()
|
| 23 |
|
| 24 |
+
# Define a function to generate image from text prompt using the loaded model
|
| 25 |
+
def process_prompt(prompt):
|
| 26 |
+
prompt += ", in " + model_prefix
|
| 27 |
+
image_return = model(prompt)
|
| 28 |
+
return image_return
|
| 29 |
+
|
| 30 |
+
# Create a Gradio interface with an input textbox for the prompt and output sections for image and text
|
| 31 |
+
interface = gradio.Interface(
|
| 32 |
+
fn=lambda prompt: (process_prompt(prompt), generate_text(prompt)),
|
| 33 |
+
inputs=gradio.inputs.Textbox(label="Enter Prompt"),
|
| 34 |
+
outputs=[gradio.outputs.Image(label="Generated Image"), gradio.outputs.Textbox(label="Generated Text")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Launch the interface
|
| 38 |
+
interface.launch()
|