koey811 commited on
Commit
4dfca06
·
verified ·
1 Parent(s): 8d508da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -17,9 +17,9 @@ except ImportError:
17
  # Load the image captioning model
18
  caption_model = pipeline("image-to-text", model="unography/blip-large-long-cap")
19
 
20
- #story_generator = pipeline("text-generation", model="distilbert/distilgpt2")
21
 
22
- story_generator = pipeline("text-generation", model="isarth/distill_gpt2_story_generator")
23
 
24
  def generate_caption(image):
25
  # Generate the caption for the uploaded image
@@ -28,18 +28,18 @@ def generate_caption(image):
28
 
29
  def generate_story(caption):
30
  # Generate the story based on the caption using the GPT-2 model
31
- prompt = f"Once upon a time, in a world inspired by the image of {caption}, a magical story took place. The fairy tale, suitable for children aged 3-10, unfolds as follows:\n\nIntroduction: Set the scene and introduce the main characters.\n\nConflict: Present a problem or challenge the characters face.\n\nResolution: Describe how the characters solve the problem or overcome the challenge.\n\nMoral: Conclude with a positive message or lesson learned.\n\nThe story should be well-structured, engaging, and easy to understand for young children. Let's begin:\n\n"
32
  story = story_generator(prompt, max_length=500, num_return_sequences=1)[0]["generated_text"]
33
 
34
  # Extract the story text from the generated output
35
  story_parts = story.split("\n\n")
36
- if len(story_parts) > 5:
37
- story = "\n\n".join(story_parts[5:]).strip()
38
  else:
39
  story = "\n\n".join(story_parts).strip()
40
 
41
  # Post-process the story (example: remove inappropriate words)
42
- inappropriate_words = ["violence", "horror", "scary"]
43
  for word in inappropriate_words:
44
  story = story.replace(word, "")
45
 
 
17
  # Load the image captioning model
18
  caption_model = pipeline("image-to-text", model="unography/blip-large-long-cap")
19
 
20
+ story_generator = pipeline("text-generation", model="distilbert/distilgpt2")
21
 
22
+ #story_generator = pipeline("text-generation", model="isarth/distill_gpt2_story_generator")
23
 
24
  def generate_caption(image):
25
  # Generate the caption for the uploaded image
 
28
 
29
  def generate_story(caption):
30
  # Generate the story based on the caption using the GPT-2 model
31
+ prompt = f"Once upon a time, in a world inspired by the image of {caption}, a delightful children's story took place. The story, suitable for ages 3-10, goes like this:\n\nIntroduction (1-2 sentences): Introduce the main character(s) and the setting.\n\nBeginning (2-3 sentences): Describe the character's normal life or routine.\n\nMiddle (3-4 sentences): Present a problem or challenge the character faces.\n\nEnd (2-3 sentences): Show how the character solves the problem or learns a lesson.\n\nThe story should be simple, engaging, and convey a positive message. Let's begin the tale:\n\n"
32
  story = story_generator(prompt, max_length=500, num_return_sequences=1)[0]["generated_text"]
33
 
34
  # Extract the story text from the generated output
35
  story_parts = story.split("\n\n")
36
+ if len(story_parts) > 7:
37
+ story = "\n\n".join(story_parts[7:]).strip()
38
  else:
39
  story = "\n\n".join(story_parts).strip()
40
 
41
  # Post-process the story (example: remove inappropriate words)
42
+ inappropriate_words = ["violence", "horror", "scary", "adult", "death", "gun", "shoot"]
43
  for word in inappropriate_words:
44
  story = story.replace(word, "")
45