Spaces:
Runtime error
Runtime error
Update pages/descriptionGen.py
Browse files- pages/descriptionGen.py +40 -25
pages/descriptionGen.py
CHANGED
@@ -1,50 +1,65 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
from langchain.prompts import PromptTemplate
|
3 |
from langchain.chat_models import ChatOpenAI
|
4 |
from langchain.base_language import BaseLanguageModel
|
5 |
from langchain.chains.llm import LLMChain
|
|
|
6 |
import openai
|
7 |
import os
|
8 |
|
9 |
-
#
|
10 |
-
OPENAI_API_KEY = st.text_input("OPENAI_API_KEY", type="password")
|
11 |
-
|
12 |
prompt_file = "prompt_template.txt"
|
13 |
|
|
|
14 |
class ProductDescGen(LLMChain):
|
15 |
-
"""LLM Chain specifically for generating multi
|
16 |
|
17 |
@classmethod
|
18 |
-
def from_llm(
|
|
|
|
|
19 |
"""Load ProductDescGen Chain from LLM."""
|
20 |
return cls(llm=llm, prompt=prompt, **kwargs)
|
21 |
|
22 |
-
|
|
|
23 |
with open(prompt_file, "r") as file:
|
24 |
prompt_template = file.read()
|
25 |
|
26 |
-
PROMPT = PromptTemplate(
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
ProductDescGen_chain = ProductDescGen.from_llm(llm=llm, prompt=PROMPT)
|
30 |
-
ProductDescGen_query = ProductDescGen_chain.apply_and_parse(
|
|
|
|
|
31 |
return ProductDescGen_query[0]["text"]
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
st.write(
|
36 |
-
|
37 |
-
|
38 |
-
)
|
39 |
-
|
40 |
-
product_name = st.text_input("Product Name", "Nike Shoes")
|
41 |
-
keywords = st.text_input("Keywords (separated by commas)", "black shoes, leather shoes for men, water resistant")
|
42 |
|
43 |
-
if st.button("Generate Description"):
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
import streamlit as st
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from langchain.chat_models import ChatOpenAI
|
5 |
from langchain.base_language import BaseLanguageModel
|
6 |
from langchain.chains.llm import LLMChain
|
7 |
+
from typing import Any
|
8 |
import openai
|
9 |
import os
|
10 |
|
11 |
+
# OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
|
|
|
|
12 |
prompt_file = "prompt_template.txt"
|
13 |
|
14 |
+
|
15 |
class ProductDescGen(LLMChain):
|
16 |
+
"""LLM Chain specifically for generating multi paragraph rich text product description using emojis."""
|
17 |
|
18 |
@classmethod
|
19 |
+
def from_llm(
|
20 |
+
cls, llm: BaseLanguageModel, prompt: str, **kwargs: Any
|
21 |
+
) -> ProductDescGen:
|
22 |
"""Load ProductDescGen Chain from LLM."""
|
23 |
return cls(llm=llm, prompt=prompt, **kwargs)
|
24 |
|
25 |
+
|
26 |
+
def product_desc_generator(product_name, keywords, OPENAI_API):
|
27 |
with open(prompt_file, "r") as file:
|
28 |
prompt_template = file.read()
|
29 |
|
30 |
+
PROMPT = PromptTemplate(
|
31 |
+
input_variables=["product_name", "keywords"], template=prompt_template
|
32 |
+
)
|
33 |
+
llm = ChatOpenAI(
|
34 |
+
model_name="gpt-3.5-turbo",
|
35 |
+
temperature=0.7,
|
36 |
+
# openai_api_key=OPENAI_API_KEY,
|
37 |
+
openai_api_key=OPENAI_API,
|
38 |
+
)
|
39 |
|
40 |
ProductDescGen_chain = ProductDescGen.from_llm(llm=llm, prompt=PROMPT)
|
41 |
+
ProductDescGen_query = ProductDescGen_chain.apply_and_parse(
|
42 |
+
[{"product_name": product_name, "keywords": keywords}]
|
43 |
+
)
|
44 |
return ProductDescGen_query[0]["text"]
|
45 |
|
46 |
+
def main():
|
47 |
+
st.title("Product Description Generator")
|
48 |
+
st.write(
|
49 |
+
"Generate multi-paragraph rich text product descriptions for your products instantly!"
|
50 |
+
" Provide the product name and keywords related to the product."
|
51 |
+
)
|
52 |
+
OPENAI_API = st.text_input("Open Ai Key", "yut_86876vv87tbyuttvubyurvt")
|
53 |
+
product_name = st.text_input("Product Name", "Nike Shoes")
|
54 |
+
keywords = st.text_input("Keywords (separated by commas)", "black shoes, leather shoes for men, water resistant")
|
55 |
|
56 |
+
if st.button("Generate Description"):
|
57 |
+
OPENAI_API_KEY:
|
58 |
+
description = product_desc_generator(product_name, keywords, OPENAI_API)
|
59 |
+
st.subheader("Product Description:")
|
60 |
+
st.text(description)
|
61 |
+
else:
|
62 |
+
st.warning("Please provide your OpenAI API Key.")
|
63 |
|
64 |
+
if __name__ == "__main__":
|
65 |
+
main()
|