Update app.py
Browse files
app.py
CHANGED
|
@@ -7,21 +7,19 @@ import os
|
|
| 7 |
|
| 8 |
def img2text(img_path):
|
| 9 |
captioner = pipeline(
|
| 10 |
-
|
| 11 |
-
model=
|
| 12 |
-
device_map=
|
| 13 |
)
|
| 14 |
result = captioner(img_path)
|
| 15 |
return result[0]["generated_text"]
|
| 16 |
|
| 17 |
def text2story(scenario):
|
| 18 |
generator = pipeline(
|
| 19 |
-
|
| 20 |
-
model=
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
torch_dtype=torch.float16,
|
| 24 |
-
max_length=100,
|
| 25 |
num_return_sequences=1
|
| 26 |
)
|
| 27 |
prompt = f"Create a children's story based on: {scenario}"
|
|
@@ -29,17 +27,22 @@ def text2story(scenario):
|
|
| 29 |
return story
|
| 30 |
|
| 31 |
def text2audio(story_text):
|
| 32 |
-
tts = gTTS(text=story_text, lang=
|
| 33 |
-
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=
|
| 34 |
tts.save(audio_file.name)
|
| 35 |
return audio_file.name
|
| 36 |
|
| 37 |
-
|
| 38 |
def main():
|
| 39 |
-
st.set_page_config(
|
|
|
|
|
|
|
|
|
|
| 40 |
st.header("Upload Your Image")
|
| 41 |
|
| 42 |
-
uploaded_file = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if uploaded_file:
|
| 45 |
temp_img = os.path.join(tempfile.gettempdir(), uploaded_file.name)
|
|
@@ -48,38 +51,28 @@ def main():
|
|
| 48 |
|
| 49 |
st.image(uploaded_file)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
with st.status(
|
| 53 |
scenario = img2text(temp_img)
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# ๆ
ไบ็ๆๅค็็ถๆ
|
| 59 |
-
with st.status("๐ Generating a story...", expanded=True) as status:
|
| 60 |
story = text2story(scenario)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.subheader("Story")
|
| 64 |
-
st.write(story)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
with st.status(
|
| 68 |
audio_path = text2audio(story)
|
| 69 |
-
status.update(label="๐ง Audio conversion completed!", state="complete")
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
|
|
|
| 73 |
os.unlink(temp_img)
|
| 74 |
os.unlink(audio_path)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
if st.button("๐ต Play Audio Story"):
|
| 78 |
-
st.audio(
|
| 79 |
-
audio_data["audio"],
|
| 80 |
-
format="audio/mp3",
|
| 81 |
-
start_time=0,
|
| 82 |
-
sample_rate=audio_data["sampling_rate"]
|
| 83 |
-
)
|
| 84 |
-
if __name__ == "__main__":
|
| 85 |
main()
|
|
|
|
| 7 |
|
| 8 |
def img2text(img_path):
|
| 9 |
captioner = pipeline(
|
| 10 |
+
'image-to-text',
|
| 11 |
+
model='nlpconnect/vit-gpt2-image-captioning',
|
| 12 |
+
device_map='auto'
|
| 13 |
)
|
| 14 |
result = captioner(img_path)
|
| 15 |
return result[0]["generated_text"]
|
| 16 |
|
| 17 |
def text2story(scenario):
|
| 18 |
generator = pipeline(
|
| 19 |
+
'text-generation',
|
| 20 |
+
model='gpt2',
|
| 21 |
+
device_map='auto',
|
| 22 |
+
max_length=200,
|
|
|
|
|
|
|
| 23 |
num_return_sequences=1
|
| 24 |
)
|
| 25 |
prompt = f"Create a children's story based on: {scenario}"
|
|
|
|
| 27 |
return story
|
| 28 |
|
| 29 |
def text2audio(story_text):
|
| 30 |
+
tts = gTTS(text=story_text, lang='en')
|
| 31 |
+
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
| 32 |
tts.save(audio_file.name)
|
| 33 |
return audio_file.name
|
| 34 |
|
|
|
|
| 35 |
def main():
|
| 36 |
+
st.set_page_config(
|
| 37 |
+
page_title='Image to Story',
|
| 38 |
+
page_icon='๐'
|
| 39 |
+
)
|
| 40 |
st.header("Upload Your Image")
|
| 41 |
|
| 42 |
+
uploaded_file = st.file_uploader(
|
| 43 |
+
"Choose Image",
|
| 44 |
+
type=['jpg', 'png', 'jpeg']
|
| 45 |
+
)
|
| 46 |
|
| 47 |
if uploaded_file:
|
| 48 |
temp_img = os.path.join(tempfile.gettempdir(), uploaded_file.name)
|
|
|
|
| 51 |
|
| 52 |
st.image(uploaded_file)
|
| 53 |
|
| 54 |
+
# ๅพ็ๅๆ
|
| 55 |
+
with st.status('๐ผ๏ธ Processing image...'):
|
| 56 |
scenario = img2text(temp_img)
|
| 57 |
+
st.write("Image Caption:", scenario)
|
| 58 |
|
| 59 |
+
# ็ๆๆ
ไบ
|
| 60 |
+
with st.status('๐ Generating story...'):
|
|
|
|
|
|
|
| 61 |
story = text2story(scenario)
|
| 62 |
+
st.subheader('Story')
|
| 63 |
+
st.write(story)
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# ็ๆ้ณ้ข
|
| 66 |
+
with st.status('๐ Converting audio...'):
|
| 67 |
audio_path = text2audio(story)
|
|
|
|
| 68 |
|
| 69 |
+
# ๆทปๅ ๆญๆพๆ้ฎ๏ผๆฐๅข้จๅ๏ผ
|
| 70 |
+
if st.button('โถ๏ธ Play Audio Story'):
|
| 71 |
+
st.audio(audio_path, format="audio/mp3")
|
| 72 |
|
| 73 |
+
# ๆธ
็ๆไปถ
|
| 74 |
os.unlink(temp_img)
|
| 75 |
os.unlink(audio_path)
|
| 76 |
|
| 77 |
+
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
main()
|