Spaces:
Running
Running
import streamlit as st | |
from langchain_openai import ChatOpenAI | |
from langchain_core.prompts import ChatPromptTemplate | |
from langchain.schema import StrOutputParser | |
chat_llm = ChatOpenAI( | |
openai_api_key=st.secrets["OPENAI_API_KEY"], | |
model=st.secrets["OPENAI_MODEL"], | |
) | |
caption_examples_file_path = "./data/caption_examples.txt" | |
prompt = ChatPromptTemplate.from_messages( | |
[ | |
( | |
"system", | |
"You are a instagram post writer and you writing captions for posts for company that make hiking and bicycle backpacks. Responde with 2 sentence max, use context as example", | |
), | |
( "system", "{example}" ), | |
( "human", "{question}" ), | |
] | |
) | |
chat_chain = prompt | chat_llm | StrOutputParser() | |
with open(caption_examples_file_path, "r") as file: | |
caption_examples = file.read() | |
if "visibility" not in st.session_state: | |
st.session_state.visibility = "visible" | |
st.session_state.disabled = False | |
st.session_state.placeholder = "Example: Generate post about backpack Black Mamba with volume of 22 liters" | |
st.set_page_config( | |
page_title="Backpack title generator", | |
page_icon="π", | |
) | |
st.write("# Welcome to InstaGPT! π") | |
st.markdown("""Writes caption for a desired backpacks in style of Osprey""") | |
text_input = st.text_input( | |
"Enter some text π", | |
label_visibility=st.session_state.visibility, | |
disabled=st.session_state.disabled, | |
placeholder=st.session_state.placeholder, | |
) | |
predefined_strings = ["Generate post about backpack Black Mamba with volume of 22 liters", "Write a post about giveaway of 3 backpacks from new collection", "Create new post about backpack Adventure for 250 dollars"] | |
st.write("Examples: \n\n Generate post about backpack Black Mamba with volume of 22 liters \n\n Write a post about giveaway of 3 backpacks from new collection \n\n Create new post about backpack Adventure for 250 dollars") | |
generate_respose = st.button("Generate") | |
if text_input: | |
st.write("You entered: ", text_input) | |
if generate_respose: | |
if text_input == "": | |
st.write("Enter something") | |
else: | |
response = chat_chain.invoke( | |
{ | |
"example": caption_examples, | |
"question": text_input, | |
} | |
) | |
st.write("Generated: ", response) | |