DeepLearn_V6 / src /streamlit_app.py
gordon20002000's picture
Update src/streamlit_app.py
809790b verified
import altair as alt
import numpy as np
import pandas as pd
import streamlit as st
from transformers import pipeline
from PIL import Image
# Creates a brief description for the pictures
def generate_caption(image):
with st.spinner("Analysing the Pictures for Key Message..."):
# Loads the BLIP model to examine and describe the picture
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
caption = image_to_text(image)[0]["generated_text"]
return caption
# Builds a story from the picture’s description
def generate_story(caption):
with st.spinner("Enhancing the Story for better Details..."):
# Uses the text generation model to create a story based on the description
pipe = pipeline("text-generation", model="TheBloke/phi-2-GGUF")
story = pipe(caption)[0]['generated_text']
return story
# Turns the story into audio
def generate_audio(story):
with st.spinner("Turning story into News audio..."):
# Uses a speech model to turn description into audio
pipe = pipeline("text-to-speech", model="hexgrad/Kokoro-82M")
audio = pipe(story)
return audio
# Streamlit UI: Makes a simple interface to generate the audio
# Displays the title
st.title("Tool for the Reporter - Turning the News Photo into Audio")
# Describes the app for users
st.write("Please upload the News Photo within 200MB")
# Allows picture uploads
uploaded_file = st.file_uploader("Upload the Photo below", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
# Shows the uploaded picture
image = Image.open(uploaded_file)
st.image(image, caption="Your Picture!", use_container_width=True)
# Gets the picture’s description
image_caption = generate_caption(image)
st.subheader("Phot Description:")
st.write(image_caption)
# Generate the News descriptions
story_telling = generate_story(image_caption)
st.subheader("The News:")
st.write(story_telling)
# Generates audio
audio = generate_audio(story_telling)
if st.button("Hear the News"):
st.audio(audio['audio'],
format="audio/wav",
start_time=0,
sample_rate=audio['sampling_rate'])