Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -59,22 +59,36 @@ def img2text(image):
|
|
59 |
text = image_to_text(image)[0]["generated_text"]
|
60 |
return text
|
61 |
|
62 |
-
# Improved text-to-story function with
|
63 |
def text2story(text):
|
64 |
generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
65 |
-
prompt = f"Write a short children's story based on this: {text}. The story should have a clear beginning, middle, and end. Keep it under 150 words. Once upon a time, "
|
66 |
|
67 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
story_result = generator(
|
69 |
prompt,
|
70 |
-
max_length=
|
71 |
num_return_sequences=1,
|
72 |
temperature=0.7,
|
73 |
do_sample=True
|
74 |
)
|
75 |
|
76 |
story_text = story_result[0]['generated_text']
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# Find natural ending points (end of sentences)
|
80 |
periods = [i for i, char in enumerate(story_text) if char == '.']
|
@@ -84,22 +98,38 @@ def text2story(text):
|
|
84 |
# Combine all ending punctuation and sort
|
85 |
all_endings = sorted(periods + question_marks + exclamation_marks)
|
86 |
|
|
|
|
|
|
|
|
|
87 |
# If we have any sentence endings
|
88 |
if all_endings:
|
89 |
-
#
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
if suitable_endings:
|
94 |
-
# Find
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
# If
|
103 |
return story_text
|
104 |
|
105 |
# Basic Streamlit interface
|
@@ -121,7 +151,10 @@ if uploaded_file is not None:
|
|
121 |
# Text to Story
|
122 |
with st.spinner("Creating story..."):
|
123 |
story = text2story(caption)
|
124 |
-
|
|
|
|
|
|
|
125 |
|
126 |
# Text to Audio
|
127 |
with st.spinner("Generating audio..."):
|
|
|
59 |
text = image_to_text(image)[0]["generated_text"]
|
60 |
return text
|
61 |
|
62 |
+
# Improved text-to-story function with longer stories (approaching 100 words)
|
63 |
def text2story(text):
|
64 |
generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
|
|
65 |
|
66 |
+
# Specifically ask for a longer story (150-200 words) to ensure we get at least 100
|
67 |
+
prompt = f"""Write a children's story based on this: {text}.
|
68 |
+
The story should have a clear beginning, middle, and end.
|
69 |
+
Make the story approximately 150-200 words long with descriptive language.
|
70 |
+
Start with "Once upon a time, "
|
71 |
+
"""
|
72 |
+
|
73 |
+
# Generate a longer text with higher max_length to ensure we get a complete story
|
74 |
story_result = generator(
|
75 |
prompt,
|
76 |
+
max_length=500, # Increased max length
|
77 |
num_return_sequences=1,
|
78 |
temperature=0.7,
|
79 |
do_sample=True
|
80 |
)
|
81 |
|
82 |
story_text = story_result[0]['generated_text']
|
83 |
+
|
84 |
+
# Extract just the story part (after the prompt)
|
85 |
+
if "Once upon a time, " in story_text:
|
86 |
+
# Find the index of "Once upon a time" and extract from there
|
87 |
+
start_idx = story_text.find("Once upon a time, ")
|
88 |
+
story_text = story_text[start_idx:]
|
89 |
+
else:
|
90 |
+
# If we can't find the exact phrase, try to find the story after the prompt
|
91 |
+
story_text = story_text.replace(prompt, "Once upon a time, ")
|
92 |
|
93 |
# Find natural ending points (end of sentences)
|
94 |
periods = [i for i, char in enumerate(story_text) if char == '.']
|
|
|
98 |
# Combine all ending punctuation and sort
|
99 |
all_endings = sorted(periods + question_marks + exclamation_marks)
|
100 |
|
101 |
+
# Count words in the story so far
|
102 |
+
def count_words(text):
|
103 |
+
return len(text.split())
|
104 |
+
|
105 |
# If we have any sentence endings
|
106 |
if all_endings:
|
107 |
+
# Find endings that give us stories of approximately 100 words or more
|
108 |
+
target_word_count = 100
|
109 |
+
min_word_count = 80 # Allow slightly shorter stories that end naturally
|
110 |
+
|
111 |
+
suitable_endings = []
|
112 |
+
for ending_idx in all_endings:
|
113 |
+
candidate_text = story_text[:ending_idx+1]
|
114 |
+
word_count = count_words(candidate_text)
|
115 |
+
if word_count >= min_word_count:
|
116 |
+
suitable_endings.append((ending_idx, word_count))
|
117 |
|
118 |
if suitable_endings:
|
119 |
+
# Find the ending that gets us closest to our target word count
|
120 |
+
suitable_endings.sort(key=lambda x: abs(x[1] - target_word_count))
|
121 |
+
best_ending_idx = suitable_endings[0][0]
|
122 |
+
return story_text[:best_ending_idx+1]
|
123 |
+
|
124 |
+
# If we couldn't find a good ending point, try to get at least 80-100 words
|
125 |
+
if len(all_endings) > 0:
|
126 |
+
for i in range(len(all_endings)-1, -1, -1):
|
127 |
+
ending_idx = all_endings[i]
|
128 |
+
candidate_text = story_text[:ending_idx+1]
|
129 |
+
if count_words(candidate_text) >= 80:
|
130 |
+
return candidate_text
|
131 |
|
132 |
+
# If all else fails, return the story as is
|
133 |
return story_text
|
134 |
|
135 |
# Basic Streamlit interface
|
|
|
151 |
# Text to Story
|
152 |
with st.spinner("Creating story..."):
|
153 |
story = text2story(caption)
|
154 |
+
# Display word count for transparency
|
155 |
+
word_count = len(story.split())
|
156 |
+
st.write(f"Story ({word_count} words):")
|
157 |
+
st.write(story)
|
158 |
|
159 |
# Text to Audio
|
160 |
with st.spinner("Generating audio..."):
|