Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import streamlit as st
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# img2text
|
6 |
+
def img2text(url):
|
7 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
8 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
9 |
+
return text
|
10 |
+
|
11 |
+
# text2story
|
12 |
+
def text2story(text):
|
13 |
+
text_to_story_model = pipeline("text-generation", model="distilbert/distilgpt2")
|
14 |
+
if isinstance(text, list):
|
15 |
+
text="".join(text)# Ensure input is a single string
|
16 |
+
story_text = text_to_story_model(text, max_length=100, num_return_sequences=1)
|
17 |
+
return story_text[0]['generated text']
|
18 |
+
|
19 |
+
# text2audio
|
20 |
+
def text2audio(story_text):
|
21 |
+
text_to_audio_model = pipeline("text-to-speech",,model="facebook/mms-tts-eng")
|
22 |
+
audio_data = text_to_audio_model(story_text)
|
23 |
+
return audio_data
|
24 |
+
|
25 |
+
#main part
|
26 |
+
st.set_page_config(page_title="Your Image to Audio Story",
|
27 |
+
page_icon="🦜")
|
28 |
+
st.header("Turn Your Image to Story")
|
29 |
+
uploaded_file= st.file_uploader("Select an Image...")
|
30 |
+
|
31 |
+
if uploaded_file is not None:
|
32 |
+
print(uploaded_file)
|
33 |
+
bytes_data = uploaded_file.getvalue()
|
34 |
+
with open(uploaded_file.name,"wb") as file:
|
35 |
+
file.write(bytes_data)
|
36 |
+
st.image(uploaded_file,caption="Uploaded Image",
|
37 |
+
use_column_width=True)
|
38 |
+
|
39 |
+
#Stage 1:Image to Text
|
40 |
+
st.text('Processing img2text...')
|
41 |
+
scenario = img2text(uploaded_file.name)
|
42 |
+
st.write(scenario)
|
43 |
+
|
44 |
+
#Stage 2: Text to Story
|
45 |
+
st.text('Generating a story...')
|
46 |
+
story = text2story(scenario)
|
47 |
+
st.write(story)
|
48 |
+
|
49 |
+
#Stage 3:Story to Audio data
|
50 |
+
st.text('Generating audio data...')
|
51 |
+
audio_data =text2audio(story)
|
52 |
+
|
53 |
+
# Play button
|
54 |
+
if st.button("Play Audio"):
|
55 |
+
st.audio(audio_data'audio']
|
56 |
+
format="audio/wav",
|
57 |
+
start_time=0,
|
58 |
+
sample_rate = audio_data['sampling_rate'])
|
59 |
+
|