smtsead commited on
Commit
7788541
Β·
verified Β·
1 Parent(s): 3603f06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -15,15 +15,17 @@ def img2text(url):
15
  text = text.replace(word, "")
16
  return text.strip()
17
 
18
- # Function to generate a story from text using GPT-2
19
  def text2story(text):
 
 
 
20
  # Add a strong prompt to guide the model
21
  prompt = f"Write a short, happy, and fun story for kids aged 3-10 based on the following description: {text}. " \
22
  "The story should be cheerful, imaginative, and suitable for young children. " \
23
  "Avoid any scary or sad elements. Keep the story under 95 words and make sure it has a clear beginning, middle, and end."
24
 
25
- # Use the GPT-2 model to generate the story
26
- text_generator = pipeline("text-generation", model="gpt2")
27
  story = text_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
28
 
29
  # Clean up the output to remove the prompt (if necessary)
@@ -42,6 +44,12 @@ st.set_page_config(page_title="Image to Story", page_icon="πŸ“–")
42
  st.header("πŸ“– Image to Story")
43
  st.markdown("### Turn your image into a fun story!")
44
 
 
 
 
 
 
 
45
  uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
46
 
47
  if uploaded_file is not None:
@@ -58,20 +66,22 @@ if uploaded_file is not None:
58
  st.write("**What I see:**", scenario)
59
 
60
  # Stage 2: Text to Story
61
- st.text('πŸ“ Creating a story...')
62
- story = text2story(scenario)
63
- st.write("**Your Story:**", story)
 
64
 
65
- # Stage 3: Story to Audio
66
- st.text('πŸŽ™οΈ Turning your story into audio...')
67
- audio_file = text2audio(story)
68
 
69
  # Play button for audio
70
- if st.button("🎧 Listen to the Story"):
71
- st.audio(audio_file, format="audio/mp3")
72
 
73
  # Clean up the generated audio file
74
- os.remove(audio_file)
 
75
 
76
  # Add some fun prompts for kids
77
  st.markdown("### 🎨 Tips for a Great Story!")
 
15
  text = text.replace(word, "")
16
  return text.strip()
17
 
18
+ # Function to generate a story from text using GPT-Neo
19
  def text2story(text):
20
+ # Use the GPT-Neo model for storytelling
21
+ text_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-125M")
22
+
23
  # Add a strong prompt to guide the model
24
  prompt = f"Write a short, happy, and fun story for kids aged 3-10 based on the following description: {text}. " \
25
  "The story should be cheerful, imaginative, and suitable for young children. " \
26
  "Avoid any scary or sad elements. Keep the story under 95 words and make sure it has a clear beginning, middle, and end."
27
 
28
+ # Generate the story
 
29
  story = text_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
30
 
31
  # Clean up the output to remove the prompt (if necessary)
 
44
  st.header("πŸ“– Image to Story")
45
  st.markdown("### Turn your image into a fun story!")
46
 
47
+ # Initialize session state
48
+ if "story" not in st.session_state:
49
+ st.session_state.story = None
50
+ if "audio_file" not in st.session_state:
51
+ st.session_state.audio_file = None
52
+
53
  uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
54
 
55
  if uploaded_file is not None:
 
66
  st.write("**What I see:**", scenario)
67
 
68
  # Stage 2: Text to Story
69
+ if st.session_state.story is None or st.button("πŸ”„ Generate New Story"):
70
+ st.text('πŸ“ Creating a story...')
71
+ st.session_state.story = text2story(scenario)
72
+ st.write("**Your Story:**", st.session_state.story)
73
 
74
+ # Stage 3: Story to Audio
75
+ st.text('πŸŽ™οΈ Turning your story into audio...')
76
+ st.session_state.audio_file = text2audio(st.session_state.story)
77
 
78
  # Play button for audio
79
+ if st.session_state.audio_file and st.button("🎧 Listen to the Story"):
80
+ st.audio(st.session_state.audio_file, format="audio/mp3")
81
 
82
  # Clean up the generated audio file
83
+ if st.session_state.audio_file and os.path.exists(st.session_state.audio_file):
84
+ os.remove(st.session_state.audio_file)
85
 
86
  # Add some fun prompts for kids
87
  st.markdown("### 🎨 Tips for a Great Story!")