File size: 2,697 Bytes
aec954b
 
 
 
f1cff78
aec954b
 
 
 
 
 
 
 
f1cff78
aec954b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e829c4e
aec954b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

from transformers import pipeline
from langchain import PromptTemplate, LLMChain

from langchain_community.chat_models import ChatGooglePalm
import requests
import os
import streamlit as st


os.environ["GOOGLE_API_KEY"] = "AIzaSyD29fEos3V6S2L-AGSQgNu03GqZEIgJads"
os.environ ["HUGGINGFACEHUB_API_TOKEN"] = "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"

llm = ChatGooglePalm(temperature = 0.5)


#image to text

def image2text(url):
    image_to_text = pipeline("image-to-text", model = "Salesforce/blip-image-captioning-large")

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

#story teller
def generate_story(scenario):
    template = """"
    You are a story teller;
    you can generate a creative fun story based on a sample narrative, the story should not be more than 100 words;

    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 text2speech(message):
     API_URL = "https://api-inference.huggingface.co/models/espnet/fastspeech2_conformer"
     headers = {"Authorization": "Bearer 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"}
     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="Your Image to Audio Story", page_icon="🦜")
    st.header("Turn Your Image to Audio Story")
    uploaded_file = st.file_uploader("Select an Image...")

    if uploaded_file is not None:
        print(uploaded_file)
        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 = image2text(uploaded_file.name)
        st.subheader("Image Details:")
        st.write(scenario)

        story = generate_story(scenario)
        st.subheader("Story:")
        st.write(story)

        text2speech(story)
        st.subheader("Generated Audio:")
        st.audio("audio.flac", format="audio/flac")

        # Add a download link for the audio
        st.subheader("Download Audio:")
        with open("audio.flac", "rb") as audio_file:
            st.download_button(label="Download Audio", data=audio_file, file_name="generated_audio.flac", mime="audio/flac")


if __name__ == "__main__":
    main()