Blessin commited on
Commit
a49157a
·
1 Parent(s): 87f3c54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -3,22 +3,26 @@ import openai
3
  import requests
4
  from PIL import Image
5
  from io import BytesIO
6
- import textwrap
7
 
8
- def create_dalle_prompts(story_title, paragraph):
9
- return f"high quality, highly detailed, Picture an imaginative illustration inspired by the fusion of Gustav Doré, Yuko Higuchi, and Syd Mead, The central theme could revolve around a fantastical cityscape where mythical creatures coexist with futuristic elements, creating a harmonious blend of the past, present, and future, The color palette would be a dynamic fusion of Doré's classical monochromes, Higuchi's playful and vibrant hues, and Mead's sleek and futuristic tones, This collaborative masterpiece would be a visual journey through a world where the boundaries of time and genre seamlessly dissolve, inviting viewers to explore the richness of a cross-temporal and fantastical narrative, by yukisakura, high detailed, '{story_title}' - {textwrap.shorten(paragraph, width=50)}"
10
 
11
- def generate_image(api_key, prompt):
12
- openai.api_key = api_key
13
- response = openai.Image.create(
14
- prompt=prompt,
15
- n=1,
16
- size="1024x1024"
17
- )
18
- image_url = response['data'][0]['url']
19
- response = requests.get(image_url)
20
- image = Image.open(BytesIO(response.content))
21
- return image
 
 
 
 
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
- # Create DALLE prompts for each paragraph
46
- dalle_prompts = [create_dalle_prompts(story_title, para) for para in paragraphs]
 
 
 
 
47
 
48
- images = [generate_image(api_key, prompt) for prompt in dalle_prompts]
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
  )