Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,11 +13,17 @@ def img2text(url):
|
|
13 |
url (str): Path to the image file.
|
14 |
|
15 |
Returns:
|
16 |
-
str: Generated text caption from the image.
|
17 |
"""
|
18 |
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
19 |
text = image_to_text_model(url)[0]["generated_text"]
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Function to generate a kid-friendly story from the text caption
|
23 |
def text2story(text):
|
@@ -28,24 +34,24 @@ def text2story(text):
|
|
28 |
text (str): Text caption generated from the image.
|
29 |
|
30 |
Returns:
|
31 |
-
str: Generated story suitable for kids aged 3-10, within
|
32 |
"""
|
33 |
-
# Add a prompt to ensure the story is
|
34 |
-
prompt = f"Write a happy
|
35 |
|
36 |
# Load the text generation model
|
37 |
story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
|
38 |
|
39 |
# Generate the story
|
40 |
-
story = story_generator(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
|
41 |
|
42 |
# Remove the prompt from the generated story
|
43 |
story = story.replace(prompt, "").strip()
|
44 |
|
45 |
# Ensure the story is within 95 words
|
46 |
story_words = story.split()
|
47 |
-
if len(story_words) >
|
48 |
-
story = " ".join(story_words[:
|
49 |
|
50 |
return story
|
51 |
|
|
|
13 |
url (str): Path to the image file.
|
14 |
|
15 |
Returns:
|
16 |
+
str: Generated text caption from the image, without words like "illustration".
|
17 |
"""
|
18 |
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
19 |
text = image_to_text_model(url)[0]["generated_text"]
|
20 |
+
|
21 |
+
# Remove unwanted words like "illustration"
|
22 |
+
unwanted_words = ["illustration", "drawing", "sketch", "picture", "dream", "imagination"]
|
23 |
+
for word in unwanted_words:
|
24 |
+
text = text.replace(word, "")
|
25 |
+
|
26 |
+
return text.strip()
|
27 |
|
28 |
# Function to generate a kid-friendly story from the text caption
|
29 |
def text2story(text):
|
|
|
34 |
text (str): Text caption generated from the image.
|
35 |
|
36 |
Returns:
|
37 |
+
str: Generated story suitable for kids aged 3-10, within 100 words.
|
38 |
"""
|
39 |
+
# Add a prompt to ensure the story is happy, fun, and appropriate for kids
|
40 |
+
prompt = f"Write a happy and fun story for kids aged 3-10 based on the following scenario: {text}. The story should be cheerful, with no sad, violent, or scary elements. Avoid any mention of guns, weapons, or conflict. The story must have a clear beginning, middle, and end. Keep the story under 100 words."
|
41 |
|
42 |
# Load the text generation model
|
43 |
story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
|
44 |
|
45 |
# Generate the story
|
46 |
+
story = story_generator(prompt, max_length=150, num_return_sequences=1, temperature=0.7, top_k=50, top_p=0.9)[0]["generated_text"]
|
47 |
|
48 |
# Remove the prompt from the generated story
|
49 |
story = story.replace(prompt, "").strip()
|
50 |
|
51 |
# Ensure the story is within 95 words
|
52 |
story_words = story.split()
|
53 |
+
if len(story_words) > 100:
|
54 |
+
story = " ".join(story_words[:100])
|
55 |
|
56 |
return story
|
57 |
|