File size: 2,286 Bytes
1733285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import requests
import streamlit as st

from dotenv import find_dotenv, load_dotenv
from transformers import pipeline

from langchain import PromptTemplate, LLMChain
from langchain.llms import GooglePalm

load_dotenv(find_dotenv())

llm = GooglePalm(temperature=0.9, google_api_key=os.getenv("GOOGLE_API_KEY"))

# Iamge to Text
def image_to_text(url):
    #load a transformer
    image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")

    text = image_to_text(url)[0]['generated_text']

    print (text)
    return text

# llm
def generate_story(scenario):
    template = """
    you are a very good story teller and a very rude person:
    you can generate a short fairy tail based on a single narrative, the story should take 5 seconds to read.

    CONTEXT: {scenario}
    STORY:
    """

    prompt = PromptTemplate(template=template, input_variables=["scenario"])
    story_llm = LLMChain(llm=llm, prompt=prompt, verbose=True)
    story = story_llm.predict(scenario=scenario)
    print(story)
    return story

# text to speech

def text_to_speech(message):
    API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
    headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
    payload = {"inputs": message}

    response = requests.post(API_URL, headers=headers, json=payload)
    print(response.content)
    with open('audio.mp3', 'wb') as audio_file:
        audio_file.write(response.content)

def main():
    st.set_page_config(page_title="Image to Story", page_icon="πŸ“š", layout="wide")

    st.title("Image to Story")
    uploaded_file = st.file_uploader("Choose an image...", type="png")

    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 = image_to_text(uploaded_file.name)
        story = generate_story(scenario)
        text_to_speech(story)

        with st.expander("scenerio"):
            st.write(scenario)
        with st.expander("story"):
            st.write(story)

        st.audio("audio.mp3")

if __name__ == '__main__':
    main()