koey811 commited on
Commit
478e2ea
·
verified ·
1 Parent(s): 44bb517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py CHANGED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from gtts import gTTS
4
+ import os
5
+
6
+ def generate_caption(image):
7
+ # Load the image captioning model
8
+ caption_model = pipeline("image-to-text", model="facebook/blip-image-captioning-base")
9
+
10
+ # Generate the caption for the uploaded image
11
+ caption = caption_model(image)[0]["generated_text"]
12
+
13
+ return caption
14
+
15
+ def generate_story(caption):
16
+ # Load the text generation model
17
+ text_generation_model = pipeline("text-generation", model="gpt2")
18
+
19
+ # Generate the story based on the caption
20
+ story = text_generation_model(caption, max_length=200, num_return_sequences=1)[0]["generated_text"]
21
+
22
+ return story
23
+
24
+ def convert_to_audio(story):
25
+ # Convert the story to audio using gTTS
26
+ tts = gTTS(text=story, lang="en")
27
+ tts.save("story_audio.mp3")
28
+
29
+ def main():
30
+ st.title("Storytelling Application")
31
+
32
+ # File uploader for the image
33
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
34
+
35
+ if uploaded_image is not None:
36
+ # Display the uploaded image
37
+ st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
38
+
39
+ # Generate the caption for the image
40
+ caption = generate_caption(uploaded_image)
41
+ st.subheader("Generated Caption:")
42
+ st.write(caption)
43
+
44
+ # Generate the story based on the caption
45
+ story = generate_story(caption)
46
+ st.subheader("Generated Story:")
47
+ st.write(story)
48
+
49
+ # Convert the story to audio
50
+ convert_to_audio(story)
51
+
52
+ # Display the audio player
53
+ audio_file = open("story_audio.mp3", "rb")
54
+ audio_bytes = audio_file.read()
55
+ st.audio(audio_bytes, format="audio/mp3")
56
+
57
+ if __name__ == "__main__":
58
+ main()