Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,22 +59,59 @@ def img2text(image):
|
|
| 59 |
text = image_to_text(image)[0]["generated_text"]
|
| 60 |
return text
|
| 61 |
|
| 62 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def text2story(text):
|
| 64 |
generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Generate a longer text to ensure we get a complete story
|
| 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 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# Find natural ending points (end of sentences)
|
| 80 |
periods = [i for i, char in enumerate(story_text) if char == '.']
|
|
@@ -84,20 +121,39 @@ 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 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# If no good ending is found, return as is
|
| 103 |
return story_text
|
|
@@ -121,7 +177,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 |
+
# Helper function to count words
|
| 63 |
+
def count_words(text):
|
| 64 |
+
return len(text.split())
|
| 65 |
+
|
| 66 |
+
# Improved text-to-story function without "Once upon a time" constraint
|
| 67 |
def text2story(text):
|
| 68 |
generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
| 69 |
+
|
| 70 |
+
# Ask for a story without specifying how to start
|
| 71 |
+
prompt = f"""Write a children's story based on this: {text}.
|
| 72 |
+
The story should have a clear beginning, middle, and end.
|
| 73 |
+
Make the story approximately 150-200 words long with descriptive language.
|
| 74 |
+
"""
|
| 75 |
|
| 76 |
# Generate a longer text to ensure we get a complete story
|
| 77 |
story_result = generator(
|
| 78 |
prompt,
|
| 79 |
+
max_length=500,
|
| 80 |
num_return_sequences=1,
|
| 81 |
temperature=0.7,
|
| 82 |
do_sample=True
|
| 83 |
)
|
| 84 |
|
| 85 |
+
full_text = story_result[0]['generated_text']
|
| 86 |
+
|
| 87 |
+
# Try to extract just the story part (after the prompt)
|
| 88 |
+
# Look for paragraph breaks or clear story beginnings
|
| 89 |
+
potential_starts = [
|
| 90 |
+
"\n\n",
|
| 91 |
+
"\n",
|
| 92 |
+
". ",
|
| 93 |
+
"! ",
|
| 94 |
+
"? "
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
# Find where the prompt ends and the actual story begins
|
| 98 |
+
story_text = full_text
|
| 99 |
+
|
| 100 |
+
# First remove the exact prompt if it appears verbatim
|
| 101 |
+
if prompt in story_text:
|
| 102 |
+
story_text = story_text.replace(prompt, "")
|
| 103 |
+
else:
|
| 104 |
+
# Look for paragraph breaks or sentence endings that might indicate
|
| 105 |
+
# where the prompt instructions end and the story begins
|
| 106 |
+
for start_marker in potential_starts:
|
| 107 |
+
if start_marker in story_text:
|
| 108 |
+
parts = story_text.split(start_marker, 1)
|
| 109 |
+
if len(parts[0]) < len(story_text) * 0.5: # If the first part is reasonably short
|
| 110 |
+
story_text = parts[1]
|
| 111 |
+
break
|
| 112 |
+
|
| 113 |
+
# Clean up any leading/trailing whitespace
|
| 114 |
+
story_text = story_text.strip()
|
| 115 |
|
| 116 |
# Find natural ending points (end of sentences)
|
| 117 |
periods = [i for i, char in enumerate(story_text) if char == '.']
|
|
|
|
| 121 |
# Combine all ending punctuation and sort
|
| 122 |
all_endings = sorted(periods + question_marks + exclamation_marks)
|
| 123 |
|
| 124 |
+
# Target approximately 100 words
|
| 125 |
+
target_word_count = 100
|
| 126 |
+
min_acceptable_words = 80
|
| 127 |
+
|
| 128 |
# If we have any sentence endings
|
| 129 |
if all_endings:
|
| 130 |
+
# Find the sentence ending that gets us closest to 100 words
|
| 131 |
+
closest_ending = None
|
| 132 |
+
closest_word_diff = float('inf')
|
| 133 |
|
| 134 |
+
for ending_idx in all_endings:
|
| 135 |
+
candidate_text = story_text[:ending_idx+1]
|
| 136 |
+
candidate_word_count = count_words(candidate_text)
|
| 137 |
+
|
| 138 |
+
# Only consider endings that give us at least min_acceptable_words
|
| 139 |
+
if candidate_word_count >= min_acceptable_words:
|
| 140 |
+
word_diff = abs(candidate_word_count - target_word_count)
|
| 141 |
+
|
| 142 |
+
if word_diff < closest_word_diff:
|
| 143 |
+
closest_ending = ending_idx
|
| 144 |
+
closest_word_diff = word_diff
|
| 145 |
+
|
| 146 |
+
# If we found a suitable ending, use it
|
| 147 |
+
if closest_ending is not None:
|
| 148 |
+
return story_text[:closest_ending+1]
|
| 149 |
+
|
| 150 |
+
# If we couldn't find a good ending near 100 words, but we have some sentence endings,
|
| 151 |
+
# use the last one that results in a story with at least min_acceptable_words words
|
| 152 |
+
if all_endings:
|
| 153 |
+
for ending_idx in reversed(all_endings):
|
| 154 |
+
candidate_text = story_text[:ending_idx+1]
|
| 155 |
+
if count_words(candidate_text) >= min_acceptable_words:
|
| 156 |
+
return candidate_text
|
| 157 |
|
| 158 |
# If no good ending is found, return as is
|
| 159 |
return story_text
|
|
|
|
| 177 |
# Text to Story
|
| 178 |
with st.spinner("Creating story..."):
|
| 179 |
story = text2story(caption)
|
| 180 |
+
# Display word count for transparency
|
| 181 |
+
word_count = len(story.split())
|
| 182 |
+
st.write(f"Story ({word_count} words):")
|
| 183 |
+
st.write(story)
|
| 184 |
|
| 185 |
# Text to Audio
|
| 186 |
with st.spinner("Generating audio..."):
|