Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,14 +26,23 @@ def generate_caption(image):
|
|
26 |
caption = image_to_text(image)[0]['generated_text']
|
27 |
return caption
|
28 |
|
29 |
-
# Function to generate story from caption
|
30 |
def generate_story(caption):
|
31 |
prompt = f"Once upon a time, {caption} "
|
32 |
-
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
return story
|
38 |
|
39 |
# Function to convert text to speech
|
@@ -60,7 +69,8 @@ if uploaded_file is not None:
|
|
60 |
|
61 |
# Generate story
|
62 |
story = generate_story(caption)
|
63 |
-
|
|
|
64 |
st.write(story)
|
65 |
|
66 |
# Generate audio
|
|
|
26 |
caption = image_to_text(image)[0]['generated_text']
|
27 |
return caption
|
28 |
|
29 |
+
# Function to generate story from caption (less than 100 words)
|
30 |
def generate_story(caption):
|
31 |
prompt = f"Once upon a time, {caption} "
|
32 |
+
|
33 |
+
# Set max_length to control story length (approximately 100 words)
|
34 |
+
# Typical English word is ~5 characters, so ~500 characters ≈ 100 words
|
35 |
+
story = story_generator(prompt, max_length=100, do_sample=True)[0]['generated_text']
|
36 |
+
|
37 |
+
# Ensure story doesn't exceed 100 words
|
38 |
+
words = story.split()
|
39 |
+
if len(words) > 100:
|
40 |
+
words = words[:100]
|
41 |
+
story = " ".join(words)
|
42 |
+
# Add period to the end if needed
|
43 |
+
if not story.endswith(('.', '!', '?')):
|
44 |
+
story += '.'
|
45 |
+
|
46 |
return story
|
47 |
|
48 |
# Function to convert text to speech
|
|
|
69 |
|
70 |
# Generate story
|
71 |
story = generate_story(caption)
|
72 |
+
word_count = len(story.split())
|
73 |
+
st.write(f"### Your Story ({word_count} words)")
|
74 |
st.write(story)
|
75 |
|
76 |
# Generate audio
|