aiimage / app.py
mertol1's picture
Update app.py
16cf6ae verified
import gradio as gr
import requests
from PIL import Image
from io import BytesIO
# Replace 'YOUR_API_KEY' with your actual DeepAI API key
API_KEY = "5afe345e-57fd-4fd7-8a77-d06ce59f07ad"
DEEP_AI_URL = "https://api.deepai.org/api/text2img"
def generate_image(prompt):
# Set up the API request parameters
data = {
'text': prompt,
'api_key': API_KEY,
}
# Make the request to the DeepAI API
response = requests.post(DEEP_AI_URL, data=data)
# Check if the request was successful
if response.status_code == 200:
# Parse the response to get the image URL or binary data
response_data = response.json()
image_url = response_data['output_url']
# For the sake of this example, we assume the image URL is directly downloadable
image_response = requests.get(image_url)
image = Image.open(BytesIO(image_response.content))
# Convert the image to a format that Gradio can display
return image
else:
return "Error generating image"
iface = gr.Interface(
fn=generate_image,
inputs=gr.inputs.Text(label="Enter a prompt"),
outputs=gr.outputs.Image(type="auto", label="Generated Image"),
title="AI Image Generator",
description="Enter a text prompt to generate an image."
)
iface.launch(share=True)