Spaces:
Runtime error
Runtime error
import streamlit as st | |
from langchain.prompts import PromptTemplate | |
from langchain.chat_models import ChatOpenAI | |
from langchain.base_language import BaseLanguageModel | |
from langchain.chains.llm import LLMChain | |
import openai | |
import os | |
# Set up the API key (you can also use st.secrets for security) | |
OPENAI_API_KEY = st.text_input("OPENAI_API_KEY", type="password") | |
prompt_file = "prompt_template.txt" | |
class ProductDescGen(LLMChain): | |
"""LLM Chain specifically for generating multi-paragraph rich text product description using emojis.""" | |
def from_llm(cls, llm: BaseLanguageModel, prompt: str, **kwargs: Any) -> ProductDescGen: | |
"""Load ProductDescGen Chain from LLM.""" | |
return cls(llm=llm, prompt=prompt, **kwargs) | |
def product_desc_generator(product_name, keywords, api_key): | |
with open(prompt_file, "r") as file: | |
prompt_template = file.read() | |
PROMPT = PromptTemplate(input_variables=["product_name", "keywords"], template=prompt_template) | |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.7, openai_api_key=api_key) | |
ProductDescGen_chain = ProductDescGen.from_llm(llm=llm, prompt=PROMPT) | |
ProductDescGen_query = ProductDescGen_chain.apply_and_parse([{"product_name": product_name, "keywords": keywords}]) | |
return ProductDescGen_query[0]["text"] | |
st.title("Product Description Generator") | |
st.write( | |
"Generate multi-paragraph rich text product descriptions for your products instantly!" | |
" Provide the product name and keywords related to the product." | |
) | |
product_name = st.text_input("Product Name", "Nike Shoes") | |
keywords = st.text_input("Keywords (separated by commas)", "black shoes, leather shoes for men, water resistant") | |
if st.button("Generate Description"): | |
if OPENAI_API_KEY: | |
description = product_desc_generator(product_name, keywords, OPENAI_API_KEY) | |
st.subheader("Product Description:") | |
st.text(description) | |
else: | |
st.warning("Please provide your OpenAI API Key.") | |