Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def generate_comic(short_story):
|
| 6 |
sentences = short_story.split('. ')
|
|
@@ -8,25 +29,12 @@ def generate_comic(short_story):
|
|
| 8 |
|
| 9 |
for sentence in sentences:
|
| 10 |
if sentence.strip():
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
print("API Response:", response.json())
|
| 18 |
-
|
| 19 |
-
image_url = response.json().get('output_url')
|
| 20 |
-
if image_url:
|
| 21 |
-
print("Generated Image URL:", image_url) # Debugging URL
|
| 22 |
-
|
| 23 |
-
# Convert image to base64
|
| 24 |
-
try:
|
| 25 |
-
image_data = requests.get(image_url).content
|
| 26 |
-
base64_image = base64.b64encode(image_data).decode('utf-8')
|
| 27 |
-
images.append(f"data:image/png;base64,{base64_image}")
|
| 28 |
-
except Exception as e:
|
| 29 |
-
print(f"Error fetching image: {e}")
|
| 30 |
|
| 31 |
# Create comic HTML
|
| 32 |
comic_html = '<div style="display: flex; flex-direction: column;">'
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import base64
|
| 4 |
+
import openai
|
| 5 |
+
|
| 6 |
+
# Set your OpenAI API key
|
| 7 |
+
openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your actual OpenAI API key
|
| 8 |
+
|
| 9 |
+
def generate_images_from_text(text):
|
| 10 |
+
# Generate an image using OpenAI's DALL-E model or similar
|
| 11 |
+
response = openai.Image.create(
|
| 12 |
+
prompt=text,
|
| 13 |
+
n=1,
|
| 14 |
+
size="512x512"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
image_url = response['data'][0]['url']
|
| 18 |
+
|
| 19 |
+
# Fetch the image from the URL
|
| 20 |
+
image_response = requests.get(image_url)
|
| 21 |
+
|
| 22 |
+
# Convert the image to Base64 for embedding in HTML
|
| 23 |
+
base64_image = base64.b64encode(image_response.content).decode('utf-8')
|
| 24 |
+
return f"data:image/png;base64,{base64_image}"
|
| 25 |
|
| 26 |
def generate_comic(short_story):
|
| 27 |
sentences = short_story.split('. ')
|
|
|
|
| 29 |
|
| 30 |
for sentence in sentences:
|
| 31 |
if sentence.strip():
|
| 32 |
+
# Generate image from sentence
|
| 33 |
+
try:
|
| 34 |
+
base64_image = generate_images_from_text(sentence)
|
| 35 |
+
images.append(base64_image)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Error generating image: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Create comic HTML
|
| 40 |
comic_html = '<div style="display: flex; flex-direction: column;">'
|