Spaces:
Runtime error
Runtime error
Rename pages/jarvis.py to pages/descriptionGen.py
Browse files- pages/descriptionGen.py +50 -0
- pages/jarvis.py +0 -106
pages/descriptionGen.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Set up the API key (you can also use st.secrets for security)
|
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-paragraph rich text product description using emojis."""
|
16 |
+
|
17 |
+
@classmethod
|
18 |
+
def from_llm(cls, llm: BaseLanguageModel, prompt: str, **kwargs: Any) -> ProductDescGen:
|
19 |
+
"""Load ProductDescGen Chain from LLM."""
|
20 |
+
return cls(llm=llm, prompt=prompt, **kwargs)
|
21 |
+
|
22 |
+
def product_desc_generator(product_name, keywords, api_key):
|
23 |
+
with open(prompt_file, "r") as file:
|
24 |
+
prompt_template = file.read()
|
25 |
+
|
26 |
+
PROMPT = PromptTemplate(input_variables=["product_name", "keywords"], template=prompt_template)
|
27 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.7, openai_api_key=api_key)
|
28 |
+
|
29 |
+
ProductDescGen_chain = ProductDescGen.from_llm(llm=llm, prompt=PROMPT)
|
30 |
+
ProductDescGen_query = ProductDescGen_chain.apply_and_parse([{"product_name": product_name, "keywords": keywords}])
|
31 |
+
return ProductDescGen_query[0]["text"]
|
32 |
+
|
33 |
+
st.title("Product Description Generator")
|
34 |
+
|
35 |
+
st.write(
|
36 |
+
"Generate multi-paragraph rich text product descriptions for your products instantly!"
|
37 |
+
" Provide the product name and keywords related to the product."
|
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 |
+
if OPENAI_API_KEY:
|
45 |
+
description = product_desc_generator(product_name, keywords, OPENAI_API_KEY)
|
46 |
+
st.subheader("Product Description:")
|
47 |
+
st.text(description)
|
48 |
+
else:
|
49 |
+
st.warning("Please provide your OpenAI API Key.")
|
50 |
+
|
pages/jarvis.py
DELETED
@@ -1,106 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
from pathlib import Path
|
4 |
-
from langchain_community.document_loaders import PyPDFLoader
|
5 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
-
from langchain_community.vectorstores import Chroma
|
7 |
-
from langchain.chains import ConversationalRetrievalChain
|
8 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
9 |
-
from langchain_community.llms import HuggingFaceEndpoint
|
10 |
-
from langchain.memory import ConversationBufferMemory
|
11 |
-
from unidecode import unidecode
|
12 |
-
import chromadb
|
13 |
-
import re
|
14 |
-
|
15 |
-
list_llm = [
|
16 |
-
"mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
17 |
-
"mistralai/Mistral-7B-Instruct-v0.1", "google/gemma-7b-it", "google/gemma-2b-it",
|
18 |
-
"HuggingFaceH4/zephyr-7b-beta", "HuggingFaceH4/zephyr-7b-gemma-v0.1",
|
19 |
-
"meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2",
|
20 |
-
"TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct",
|
21 |
-
"google/flan-t5-xxl"
|
22 |
-
]
|
23 |
-
|
24 |
-
def load_doc(list_file_path, chunk_size, chunk_overlap):
|
25 |
-
loaders = [PyPDFLoader(x) for x in list_file_path]
|
26 |
-
pages = []
|
27 |
-
for loader in loaders:
|
28 |
-
pages.extend(loader.load())
|
29 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
30 |
-
doc_splits = text_splitter.split_documents(pages)
|
31 |
-
return doc_splits
|
32 |
-
|
33 |
-
def create_db(splits, collection_name):
|
34 |
-
embedding = HuggingFaceEmbeddings()
|
35 |
-
new_client = chromadb.EphemeralClient()
|
36 |
-
vectordb = Chroma.from_documents(
|
37 |
-
documents=splits,
|
38 |
-
embedding=embedding,
|
39 |
-
client=new_client,
|
40 |
-
collection_name=collection_name
|
41 |
-
)
|
42 |
-
return vectordb
|
43 |
-
|
44 |
-
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db):
|
45 |
-
llm = HuggingFaceEndpoint(repo_id=llm_model, temperature=temperature, max_new_tokens=max_tokens, top_k=top_k)
|
46 |
-
memory = ConversationBufferMemory(memory_key="chat_history", output_key='answer', return_messages=True)
|
47 |
-
retriever = vector_db.as_retriever()
|
48 |
-
qa_chain = ConversationalRetrievalChain.from_llm(
|
49 |
-
llm,
|
50 |
-
retriever=retriever,
|
51 |
-
chain_type="stuff",
|
52 |
-
memory=memory,
|
53 |
-
return_source_documents=True,
|
54 |
-
verbose=False
|
55 |
-
)
|
56 |
-
return qa_chain
|
57 |
-
|
58 |
-
def create_collection_name(file_path):
|
59 |
-
collection_name = Path(file_path).stem
|
60 |
-
collection_name = unidecode(collection_name)
|
61 |
-
collection_name = re.sub('[^A-Za-z0-9]+', '-', collection_name)
|
62 |
-
collection_name = collection_name[:50]
|
63 |
-
if len(collection_name) < 3:
|
64 |
-
collection_name = collection_name + 'xyz'
|
65 |
-
if not collection_name[0].isalnum():
|
66 |
-
collection_name = 'A' + collection_name[1:]
|
67 |
-
if not collection_name[-1].isalnum():
|
68 |
-
collection_name = collection_name[:-1] + 'Z'
|
69 |
-
return collection_name
|
70 |
-
|
71 |
-
def main():
|
72 |
-
st.title("PDF-based Chatbot")
|
73 |
-
|
74 |
-
uploaded_files = st.file_uploader("Upload PDF documents (single or multiple)", type="pdf", accept_multiple_files=True)
|
75 |
-
|
76 |
-
if uploaded_files:
|
77 |
-
chunk_size = st.slider("Chunk size", min_value=100, max_value=1000, value=600, step=20)
|
78 |
-
chunk_overlap = st.slider("Chunk overlap", min_value=10, max_value=200, value=40, step=10)
|
79 |
-
|
80 |
-
list_file_path = [file.name for file in uploaded_files]
|
81 |
-
|
82 |
-
if st.button("Generate Vector Database"):
|
83 |
-
st.text("Loading documents...")
|
84 |
-
doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)
|
85 |
-
st.text("Creating vector database...")
|
86 |
-
collection_name = create_collection_name(list_file_path[0])
|
87 |
-
vector_db = create_db(doc_splits, collection_name)
|
88 |
-
|
89 |
-
llm_model = st.selectbox("Choose LLM Model", list_llm)
|
90 |
-
temperature = st.slider("Temperature", min_value=0.01, max_value=1.0, value=0.7, step=0.1)
|
91 |
-
max_tokens = st.slider("Max Tokens", min_value=224, max_value=4096, value=1024, step=32)
|
92 |
-
top_k = st.slider("Top-K Samples", min_value=1, max_value=10, value=3, step=1)
|
93 |
-
|
94 |
-
if st.button("Initialize QA Chain"):
|
95 |
-
st.text("Initializing QA chain...")
|
96 |
-
qa_chain = initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db)
|
97 |
-
|
98 |
-
st.header("Chatbot")
|
99 |
-
message = st.text_input("Type your message")
|
100 |
-
if st.button("Submit"):
|
101 |
-
st.text("Generating response...")
|
102 |
-
response = qa_chain({"question": message, "chat_history": []})
|
103 |
-
st.write("Assistant:", response["answer"])
|
104 |
-
|
105 |
-
if __name__ == "__main__":
|
106 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|