import tensorflow
import dotenv
import transformers
from tensorflow import keras
from dotenv import find_dotenv, load_dotenv
from transformers import pipeline
import langchain
from langchain import PromptTemplate, LLMChain, OpenAI

import requests
import os
import openai

import streamlit as st

HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
openai.api_key = os.getenv("OPENAI_API_KEY")

load_dotenv(find_dotenv())


# img to text
def img2text(data):
    image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")

    text = image_to_text(data)[0]["generated_text"]

    print(text)
    return text


# 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 20 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


# txt to speech
def text2speech(message):
    API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
    headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
    payload = { "inputs": message }

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


def main():
    st.set_page_config(page_title="Image to Speech", page_icon="🤗")
    uploaded_file = st.file_uploader("Choose an image...", type="jpg")

    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(bytes_data, caption='Uploaded Image.', use_column_width=True)
        scenario = img2text(uploaded_file.name)
        story = generate_story(scenario)
        text2speech(story)
        
        with st.expander("scenario"):
            st.write(scenario)
        with st.expander("story"):
            st.write(story)
        
        st.audio('audio.flac')


if __name__ == '__main__':
    main()

langchain.OpenAI langchain.PromptTemplate
[((324, 361), 'os.getenv', 'os.getenv', (['"""HUGGINGFACEHUB_API_TOKEN"""'], {}), "('HUGGINGFACEHUB_API_TOKEN')\n", (333, 361), False, 'import os\n'), ((379, 406), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (388, 406), False, 'import os\n'), ((420, 433), 'dotenv.find_dotenv', 'find_dotenv', ([], {}), '()\n', (431, 433), False, 'from dotenv import find_dotenv, load_dotenv\n'), ((491, 563), 'transformers.pipeline', 'pipeline', (['"""image-to-text"""'], {'model': '"""Salesforce/blip-image-captioning-base"""'}), "('image-to-text', model='Salesforce/blip-image-captioning-base')\n", (499, 563), False, 'from transformers import pipeline\n'), ((905, 968), 'langchain.PromptTemplate', 'PromptTemplate', ([], {'template': 'template', 'input_variables': "['scenario']"}), "(template=template, input_variables=['scenario'])\n", (919, 968), False, 'from langchain import PromptTemplate, LLMChain, OpenAI\n'), ((1449, 1502), 'requests.post', 'requests.post', (['API_URL'], {'headers': 'headers', 'json': 'payload'}), '(API_URL, headers=headers, json=payload)\n', (1462, 1502), False, 'import requests\n'), ((1601, 1664), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Image to Speech"""', 'page_icon': '"""🤗"""'}), "(page_title='Image to Speech', page_icon='🤗')\n", (1619, 1664), True, 'import streamlit as st\n'), ((1685, 1735), 'streamlit.file_uploader', 'st.file_uploader', (['"""Choose an image..."""'], {'type': '"""jpg"""'}), "('Choose an image...', type='jpg')\n", (1701, 1735), True, 'import streamlit as st\n'), ((1942, 2012), 'streamlit.image', 'st.image', (['bytes_data'], {'caption': '"""Uploaded Image."""', 'use_column_width': '(True)'}), "(bytes_data, caption='Uploaded Image.', use_column_width=True)\n", (1950, 2012), True, 'import streamlit as st\n'), ((2287, 2309), 'streamlit.audio', 'st.audio', (['"""audio.flac"""'], {}), "('audio.flac')\n", (2295, 2309), True, 'import streamlit as st\n'), ((999, 1048), 'langchain.OpenAI', 'OpenAI', ([], {'model_name': '"""gpt-3.5-turbo"""', 'temperature': '(1)'}), "(model_name='gpt-3.5-turbo', temperature=1)\n", (1005, 1048), False, 'from langchain import PromptTemplate, LLMChain, OpenAI\n'), ((2151, 2174), 'streamlit.expander', 'st.expander', (['"""scenario"""'], {}), "('scenario')\n", (2162, 2174), True, 'import streamlit as st\n'), ((2188, 2206), 'streamlit.write', 'st.write', (['scenario'], {}), '(scenario)\n', (2196, 2206), True, 'import streamlit as st\n'), ((2220, 2240), 'streamlit.expander', 'st.expander', (['"""story"""'], {}), "('story')\n", (2231, 2240), True, 'import streamlit as st\n'), ((2254, 2269), 'streamlit.write', 'st.write', (['story'], {}), '(story)\n', (2262, 2269), True, 'import streamlit as st\n')]