Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,8 @@ except ImportError:
|
|
| 18 |
caption_model = pipeline("image-to-text", model="unography/blip-large-long-cap")
|
| 19 |
|
| 20 |
# Load the GPT-2 model for story generation
|
| 21 |
-
story_generator = pipeline("text-generation", model="gpt2")
|
|
|
|
| 22 |
|
| 23 |
def generate_caption(image):
|
| 24 |
# Generate the caption for the uploaded image
|
|
@@ -35,9 +36,24 @@ def generate_caption(image):
|
|
| 35 |
|
| 36 |
#return story
|
| 37 |
|
| 38 |
-
def generate_story(caption):
|
| 39 |
# Generate the story based on the caption
|
| 40 |
-
story = story_generator(caption, max_length=200, num_return_sequences=1)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return story
|
| 42 |
|
| 43 |
def convert_to_audio(story):
|
|
|
|
| 18 |
caption_model = pipeline("image-to-text", model="unography/blip-large-long-cap")
|
| 19 |
|
| 20 |
# Load the GPT-2 model for story generation
|
| 21 |
+
#story_generator = pipeline("text-generation", model="gpt2")
|
| 22 |
+
story_generator = pipeline("text-generation", model="distilbert/distilgpt2")
|
| 23 |
|
| 24 |
def generate_caption(image):
|
| 25 |
# Generate the caption for the uploaded image
|
|
|
|
| 36 |
|
| 37 |
#return story
|
| 38 |
|
| 39 |
+
#def generate_story(caption):
|
| 40 |
# Generate the story based on the caption
|
| 41 |
+
#story = story_generator(caption, max_length=200, num_return_sequences=1)[0]["generated_text"]
|
| 42 |
+
#return story
|
| 43 |
+
|
| 44 |
+
def generate_story(caption):
|
| 45 |
+
# Generate the story based on the caption using the GPT-2 model
|
| 46 |
+
prompt = f"Once upon a time, based on the image described as '{caption}', here is a short, simple, and engaging story for children aged 3-10. The story should be easy to understand, use age-appropriate language, and convey a positive message. Focus on the main elements in the image and create a story that sparks their imagination:\n\n"
|
| 47 |
+
story = story_generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
| 48 |
+
|
| 49 |
+
# Extract the story text from the generated output
|
| 50 |
+
story = story.split("\n\n")[1].strip()
|
| 51 |
+
|
| 52 |
+
# Post-process the story (example: remove inappropriate words)
|
| 53 |
+
inappropriate_words = ["violence", "horror", "scary"]
|
| 54 |
+
for word in inappropriate_words:
|
| 55 |
+
story = story.replace(word, "")
|
| 56 |
+
|
| 57 |
return story
|
| 58 |
|
| 59 |
def convert_to_audio(story):
|