# Original code # https://www.youtube.com/watch?app=desktop&v=_j7JEDWuqLE&ab_channel=AIJason # OpenAI break # https://github.com/langchain-ai/langchain/issues/12949 import os import requests from transformers import pipeline #from langchain import PromptTemplate, LLMChain, OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.llms import OpenAI import streamlit as st #from huggingface_hub import HfApi HUGGING_FACE_API_TOKEN = st.secrets["HUGGING_FACE_API_TOKEN"] #api = HfApi() #if api.is_authenticated(): # print('HF server') # HUGGING_FACE_API_TOKEN = os.environ("HUGGING_FACE_API_TOKEN") #else: # print('Local machine') # HUGGING_FACE_API_TOKEN = st.secrets("HUGGING_FACE_API_TOKEN") # img2text def img2text(url): 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 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 # text to speech def text2speech(message): API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits" headers = {"Authorization": f"Bearer {HUGGING_FACE_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="img 2 audio story", page_icon="A") st.header("Turn img into audio story") uploaded_file = st.file_uploader("Chose 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 = 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() # Debug #scenario = img2text("photo.jpg") #story = generate_story(scenario) #text2speech(story)