Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,22 +3,26 @@ import openai
|
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def generate_story_and_images(name, story_title, story_type, api_key):
|
| 24 |
openai.api_key = api_key
|
|
@@ -42,27 +46,26 @@ def generate_story_and_images(name, story_title, story_type, api_key):
|
|
| 42 |
# Split story into four paragraphs
|
| 43 |
paragraphs = story.split('\n')[:4]
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
dalle_prompts = [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
return story, dalle_prompts, *images
|
| 50 |
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=generate_story_and_images,
|
| 53 |
inputs=[
|
| 54 |
gr.components.Textbox(label="Enter name (Example: Ella)"),
|
| 55 |
gr.components.Textbox(label="Enter story title (Example: The Enchanted Locket)"),
|
| 56 |
-
gr.components.Radio(["for children", "absurd"], label="Story Type"),
|
| 57 |
gr.components.Textbox(label="OpenAI API Key", type="password")
|
| 58 |
],
|
| 59 |
outputs=[
|
| 60 |
"text",
|
| 61 |
-
"text"
|
| 62 |
-
gr.components.Image(label="Image 1", type="pil"),
|
| 63 |
-
gr.components.Image(label="Image 2", type="pil"),
|
| 64 |
-
gr.components.Image(label="Image 3", type="pil"),
|
| 65 |
-
gr.components.Image(label="Image 4", type="pil")
|
| 66 |
],
|
| 67 |
live=True
|
| 68 |
)
|
|
|
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
+
import spacy
|
| 7 |
|
| 8 |
+
# Load the spaCy NLP model for entity extraction
|
| 9 |
+
nlp = spacy.load("en_core_web_sm")
|
| 10 |
|
| 11 |
+
def extract_character_and_action(paragraph):
|
| 12 |
+
doc = nlp(paragraph)
|
| 13 |
+
character_type = "human" # default value
|
| 14 |
+
action = "standing" # default value
|
| 15 |
+
for token in doc:
|
| 16 |
+
# Extract character type (using NOUN for simplicity)
|
| 17 |
+
if "NOUN" in token.pos_:
|
| 18 |
+
character_type = token.text
|
| 19 |
+
# Extract action (using VERB for simplicity)
|
| 20 |
+
if "VERB" in token.pos_:
|
| 21 |
+
action = token.text
|
| 22 |
+
return character_type, action
|
| 23 |
+
|
| 24 |
+
def create_dalle_prompt(character_type, action):
|
| 25 |
+
return f"1800s field-journal style line drawing of {character_type} {action}, very detailed"
|
| 26 |
|
| 27 |
def generate_story_and_images(name, story_title, story_type, api_key):
|
| 28 |
openai.api_key = api_key
|
|
|
|
| 46 |
# Split story into four paragraphs
|
| 47 |
paragraphs = story.split('\n')[:4]
|
| 48 |
|
| 49 |
+
# Extract character type and action from each paragraph and create DALLE prompts
|
| 50 |
+
dalle_prompts = []
|
| 51 |
+
for para in paragraphs:
|
| 52 |
+
character_type, action = extract_character_and_action(para)
|
| 53 |
+
dalle_prompt = create_dalle_prompt(character_type, action)
|
| 54 |
+
dalle_prompts.append(dalle_prompt)
|
| 55 |
|
| 56 |
+
return story, dalle_prompts
|
|
|
|
| 57 |
|
| 58 |
iface = gr.Interface(
|
| 59 |
fn=generate_story_and_images,
|
| 60 |
inputs=[
|
| 61 |
gr.components.Textbox(label="Enter name (Example: Ella)"),
|
| 62 |
gr.components.Textbox(label="Enter story title (Example: The Enchanted Locket)"),
|
| 63 |
+
gr.components.Radio(["for children", "absurd and humorous"], label="Story Type"),
|
| 64 |
gr.components.Textbox(label="OpenAI API Key", type="password")
|
| 65 |
],
|
| 66 |
outputs=[
|
| 67 |
"text",
|
| 68 |
+
"text"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
],
|
| 70 |
live=True
|
| 71 |
)
|