Spaces:
Sleeping
Sleeping
| from transformers import pipeline | |
| #from transformers.utils import logging | |
| from langchain import PromptTemplate, LLMChain | |
| import streamlit as st | |
| #logging.set_verbosity_error() | |
| #API KEYS | |
| openai_api_key = st.secrets['OPENAI_API_KEY'] | |
| url = st.text_area('enter a url to process') | |
| def img2text(url): | |
| image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large") | |
| text = image_to_text(url)[0]["generated_text"] | |
| return text | |
| def generate_poem(scenario, openai_api_key): | |
| template = """ | |
| You are a writer and your favorite poet is Emily Dickinson. | |
| You can generate a short poem based on a simple phrase or sentence. | |
| CONTEXT: {scenario} | |
| POEM: | |
| """ | |
| prompt = PromptTemplate(template = template, input_variables = ['scenario']) | |
| # Call LLM Chain with OpenAI's API in LangChain | |
| poem_LLM = LLMChain(llm = ChatOpenAI( | |
| openai_api_key = openai_api_key, | |
| model_name = "gpt-3.5-turbo", | |
| temperature = 0.9, | |
| prompt = prompt, | |
| verbose = True | |
| )) | |
| poem = poem_LLM.predict(scenario = scenario) | |
| print(poem) | |
| return poem | |
| if url: | |
| scenario = img2text(url) | |
| poem_output = generate_poem(scenario, openai_api_key) | |
| st.write(poem_output) | |