Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,41 +39,40 @@ def text2story(text, _models):
|
|
| 39 |
"""Generate a story from text - using the original implementation"""
|
| 40 |
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. "
|
| 41 |
|
| 42 |
-
# Generate
|
| 43 |
story_result = _models["story_generator"](
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Find natural ending points (end of sentences)
|
| 54 |
periods = [i for i, char in enumerate(story_text) if char == '.']
|
| 55 |
question_marks = [i for i, char in enumerate(story_text) if char == '?']
|
| 56 |
exclamation_marks = [i for i, char in enumerate(story_text) if char == '!']
|
| 57 |
-
|
| 58 |
-
# Combine all ending punctuation and sort
|
| 59 |
all_endings = sorted(periods + question_marks + exclamation_marks)
|
| 60 |
-
|
| 61 |
-
# If we have any sentence endings
|
| 62 |
if all_endings:
|
| 63 |
-
# Get the index where the story should reasonably end (after at least 100 characters)
|
| 64 |
min_story_length = 100
|
| 65 |
suitable_endings = [i for i in all_endings if i >= min_story_length]
|
| 66 |
-
|
| 67 |
if suitable_endings:
|
| 68 |
-
# Find an ending that completes a thought (not just the first sentence)
|
| 69 |
if len(suitable_endings) > 2:
|
| 70 |
-
# Use the third sentence ending or later for a more complete story
|
| 71 |
return story_text[:suitable_endings[2]+1]
|
| 72 |
else:
|
| 73 |
-
# If we don't have many sentences, use the last one we found
|
| 74 |
return story_text[:suitable_endings[-1]+1]
|
| 75 |
-
|
| 76 |
-
# If no good ending is found, return as is
|
| 77 |
return story_text
|
| 78 |
|
| 79 |
# Text-to-speech function
|
|
|
|
| 39 |
"""Generate a story from text - using the original implementation"""
|
| 40 |
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. "
|
| 41 |
|
| 42 |
+
# Generate the story
|
| 43 |
story_result = _models["story_generator"](
|
| 44 |
+
prompt,
|
| 45 |
+
max_length=300,
|
| 46 |
+
num_return_sequences=1,
|
| 47 |
+
temperature=0.7,
|
| 48 |
+
do_sample=True
|
| 49 |
)
|
| 50 |
+
|
| 51 |
+
full_text = story_result[0]['generated_text']
|
| 52 |
+
|
| 53 |
+
# If the generated text includes the prompt at the beginning, remove it.
|
| 54 |
+
if full_text.startswith(prompt):
|
| 55 |
+
story_text = full_text[len(prompt):].strip()
|
| 56 |
+
else:
|
| 57 |
+
story_text = full_text
|
| 58 |
+
|
| 59 |
# Find natural ending points (end of sentences)
|
| 60 |
periods = [i for i, char in enumerate(story_text) if char == '.']
|
| 61 |
question_marks = [i for i, char in enumerate(story_text) if char == '?']
|
| 62 |
exclamation_marks = [i for i, char in enumerate(story_text) if char == '!']
|
| 63 |
+
|
|
|
|
| 64 |
all_endings = sorted(periods + question_marks + exclamation_marks)
|
| 65 |
+
|
|
|
|
| 66 |
if all_endings:
|
|
|
|
| 67 |
min_story_length = 100
|
| 68 |
suitable_endings = [i for i in all_endings if i >= min_story_length]
|
| 69 |
+
|
| 70 |
if suitable_endings:
|
|
|
|
| 71 |
if len(suitable_endings) > 2:
|
|
|
|
| 72 |
return story_text[:suitable_endings[2]+1]
|
| 73 |
else:
|
|
|
|
| 74 |
return story_text[:suitable_endings[-1]+1]
|
| 75 |
+
|
|
|
|
| 76 |
return story_text
|
| 77 |
|
| 78 |
# Text-to-speech function
|