Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -59,36 +59,22 @@ 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 |
|
66 |
-
#
|
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=
|
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,38 +84,22 @@ def text2story(text):
|
|
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 |
-
#
|
108 |
-
|
109 |
-
|
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
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
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
|
133 |
return story_text
|
134 |
|
135 |
# Basic Streamlit interface
|
@@ -151,10 +121,7 @@ if uploaded_file is not None:
|
|
151 |
# Text to Story
|
152 |
with st.spinner("Creating story..."):
|
153 |
story = text2story(caption)
|
154 |
-
|
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..."):
|
|
|
59 |
text = image_to_text(image)[0]["generated_text"]
|
60 |
return text
|
61 |
|
62 |
+
# Improved text-to-story function with natural ending
|
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 |
+
# Generate a longer text to ensure we get a complete story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
story_result = generator(
|
69 |
prompt,
|
70 |
+
max_length=300,
|
71 |
num_return_sequences=1,
|
72 |
temperature=0.7,
|
73 |
do_sample=True
|
74 |
)
|
75 |
|
76 |
story_text = story_result[0]['generated_text']
|
77 |
+
story_text = story_text.replace(prompt, "Once upon a time, ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# Find natural ending points (end of sentences)
|
80 |
periods = [i for i, char in enumerate(story_text) if char == '.']
|
|
|
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 |
+
# Get the index where the story should reasonably end (after at least 100 characters)
|
90 |
+
min_story_length = 100
|
91 |
+
suitable_endings = [i for i in all_endings if i >= min_story_length]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
if suitable_endings:
|
94 |
+
# Find an ending that completes a thought (not just the first sentence)
|
95 |
+
if len(suitable_endings) > 2:
|
96 |
+
# Use the third sentence ending or later for a more complete story
|
97 |
+
return story_text[:suitable_endings[2]+1]
|
98 |
+
else:
|
99 |
+
# If we don't have many sentences, use the last one we found
|
100 |
+
return story_text[:suitable_endings[-1]+1]
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
# If no good ending is found, return as is
|
103 |
return story_text
|
104 |
|
105 |
# Basic Streamlit interface
|
|
|
121 |
# Text to Story
|
122 |
with st.spinner("Creating story..."):
|
123 |
story = text2story(caption)
|
124 |
+
st.write(f"Story: {story}")
|
|
|
|
|
|
|
125 |
|
126 |
# Text to Audio
|
127 |
with st.spinner("Generating audio..."):
|