Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,47 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
-
|
4 |
-
def img2text(url):
|
5 |
-
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
6 |
-
text = image_to_text_model(url)[0]["generated_text"]
|
7 |
-
return text
|
8 |
-
|
9 |
-
def text2story(text):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
st.
|
30 |
-
|
31 |
-
st.
|
32 |
-
|
33 |
-
st.
|
34 |
-
|
35 |
-
st.
|
36 |
-
|
37 |
-
st.
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
st.audio(audio_data)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
def img2text(url):
|
5 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
6 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
7 |
+
return text
|
8 |
+
|
9 |
+
def text2story(text):
|
10 |
+
story_generator = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B")
|
11 |
+
prompt = f"Create a story of maximum 100 words based upon {text}"
|
12 |
+
generated = story_generator(prompt, max_length=100)
|
13 |
+
story_text = generated[0]['generated_text']
|
14 |
+
return story_text
|
15 |
+
|
16 |
+
def text2audio(story_text):
|
17 |
+
audio_data = pipeline("text-to-speech", model="ElvisTsang/facebook-mms-tts-eng")
|
18 |
+
return audio_data
|
19 |
+
|
20 |
+
st.set_page_config(page_title="Once Upon A Time - Storytelling Application", page_icon="ππ°π¦π§")
|
21 |
+
st.header("Create a story of yours with an image!")
|
22 |
+
uploaded_file = st.file_uploader("Upload an image of your story!")
|
23 |
+
|
24 |
+
if uploaded_file is not None:
|
25 |
+
print(uploaded_file)
|
26 |
+
bytes_data = uploaded_file.getvalue()
|
27 |
+
with open(uploaded_file.name, "wb") as file:
|
28 |
+
file.write(bytes_data)
|
29 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
30 |
+
|
31 |
+
st.text('Processing img2text...')
|
32 |
+
scenario = img2text(uploaded_file.name)
|
33 |
+
st.write(scenario)
|
34 |
+
|
35 |
+
st.text('Generating a story...')
|
36 |
+
story = text2story(scenario)
|
37 |
+
st.write(story)
|
38 |
+
|
39 |
+
st.text('Generating audio data...')
|
40 |
+
audio_data =text2audio(story)
|
41 |
+
|
42 |
+
if st.button("Story Time!"):
|
43 |
+
st.audio(audio_data['audio'],
|
44 |
+
format="audio/wav",
|
45 |
+
start_time=0,
|
46 |
+
sample_rate = audio_data['sampling_rate'])
|
47 |
st.audio(audio_data)
|