Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,61 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
def split_story_into_scenes(story):
|
| 12 |
-
# Split the story into sentences (scenes) for generating images
|
| 13 |
-
scenes = story.split(". ") # Splitting based on period and space
|
| 14 |
-
return [scene for scene in scenes if scene] # Remove empty scenes
|
| 15 |
|
| 16 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def generate_images(scenes):
|
| 18 |
images = []
|
| 19 |
for scene in scenes:
|
| 20 |
-
# Generate image for each scene (text prompt)
|
| 21 |
print(f"Generating image for scene: {scene}")
|
| 22 |
-
image =
|
| 23 |
-
|
|
|
|
| 24 |
return images
|
| 25 |
|
| 26 |
# Function to create the comic-strip layout
|
| 27 |
def generate_comic_strip(story):
|
| 28 |
-
scenes = split_story_into_scenes(story)
|
| 29 |
-
images = generate_images(scenes)
|
| 30 |
-
return images
|
| 31 |
|
| 32 |
# Gradio Interface
|
| 33 |
description = "GenArt Narrative: Turn your story descriptions into comic-strip visuals!"
|
| 34 |
|
| 35 |
interface = gr.Interface(
|
| 36 |
-
fn=generate_comic_strip,
|
| 37 |
-
inputs=gr.Textbox(lines=5, placeholder="Enter your short story here..."),
|
| 38 |
-
outputs=gr.Gallery(label="
|
| 39 |
title="GenArt Narrative",
|
| 40 |
description=description,
|
| 41 |
-
examples=[
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# Launch the
|
| 45 |
if __name__ == "__main__":
|
| 46 |
interface.launch()
|
| 47 |
-
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
+
# Hugging Face Inference API token (you need to get your own token from Hugging Face)
|
| 8 |
+
API_TOKEN = "your_huggingface_api_token_here"
|
| 9 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
| 10 |
|
| 11 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Function to split story into key scenes
|
| 14 |
+
def split_story_into_scenes(story):
|
| 15 |
+
scenes = story.strip().split(". ")
|
| 16 |
+
return [scene.strip() for scene in scenes if scene.strip()] # Return non-empty, stripped scenes
|
| 17 |
+
|
| 18 |
+
# Function to generate images using the Hugging Face Inference API
|
| 19 |
+
def generate_image_from_prompt(prompt):
|
| 20 |
+
payload = {"inputs": prompt}
|
| 21 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
image = Image.open(BytesIO(response.content))
|
| 24 |
+
return image
|
| 25 |
+
else:
|
| 26 |
+
print(f"Error {response.status_code}: {response.content}")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
# Function to generate images for all scenes
|
| 30 |
def generate_images(scenes):
|
| 31 |
images = []
|
| 32 |
for scene in scenes:
|
|
|
|
| 33 |
print(f"Generating image for scene: {scene}")
|
| 34 |
+
image = generate_image_from_prompt(scene)
|
| 35 |
+
if image:
|
| 36 |
+
images.append(image)
|
| 37 |
return images
|
| 38 |
|
| 39 |
# Function to create the comic-strip layout
|
| 40 |
def generate_comic_strip(story):
|
| 41 |
+
scenes = split_story_into_scenes(story)
|
| 42 |
+
images = generate_images(scenes)
|
| 43 |
+
return images
|
| 44 |
|
| 45 |
# Gradio Interface
|
| 46 |
description = "GenArt Narrative: Turn your story descriptions into comic-strip visuals!"
|
| 47 |
|
| 48 |
interface = gr.Interface(
|
| 49 |
+
fn=generate_comic_strip,
|
| 50 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter your short story here..."),
|
| 51 |
+
outputs=gr.Gallery(label="Comic Strip"),
|
| 52 |
title="GenArt Narrative",
|
| 53 |
description=description,
|
| 54 |
+
examples=[
|
| 55 |
+
["Once upon a time, a dragon guarded a secret treasure in a mystical land."]
|
| 56 |
+
],
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Launch the app
|
| 60 |
if __name__ == "__main__":
|
| 61 |
interface.launch()
|
|
|