imagetostory / app.py
KunalKharalkar's picture
Upload 2 files
9e375c7
raw
history blame
2.2 kB
from dotenv import find_dotenv, load_dotenv
from transformers import pipeline
from langchain import PromptTemplate, LLMChain, OpenAI
import requests
import os
import streamlit as st
load_dotenv(find_dotenv())
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
#module1: image to text
def imgtotxt(url):
img_to_txt = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
text = img_to_txt(url)[0]["generated_text"]
print(text)
return text
#module2: llm
def generate_story(scenario):
template = """
You are a story teller;
You can generate a short story based on a simple narrative, the story should be no more than 50 words;
CONTEXT: {scenario}
STORY:
"""
prompt = PromptTemplate(template=template, input_variables=["scenario"])
story_llm = LLMChain(llm=OpenAI(model_name="gpt-3.5-turbo", temperature=1), prompt=prompt, verbose=True)
story = story_llm.predict(scenario=scenario)
print(story)
return story
#module3: text to speech
def texttospeech(message):
API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
payloads = {
"inputs": message
}
response = requests.post(API_URL, headers=headers, json=payloads)
with open('audio.flac', 'wb') as file:
file.write(response.content)
def main():
st.set_page_config(page_title="Image to Audio Story", page_icon="🗣️")
st.header("Turn Image into Audio Story")
uploaded_file = st.file_uploader("Choose an Image...", type="jpg")
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue()
with open(uploaded_file.name, "wb") as file:
file.write(bytes_data)
st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
scenario = imgtotxt(uploaded_file.name)
story= generate_story(scenario)
texttospeech(story)
with st.expander("Scenario"):
st.write(scenario)
with st.expander("Story"):
st.write(story)
st.audio("audio.flac")
if __name__ == '__main__':
main()