Spaces:
Runtime error
Runtime error
Rename pages/jarvis.py to pages/descriptionGen.py
Browse filesFiles changed (2)
- pages/descriptionGen.pyAdded+50
- pages/jarvis.pyDeleted-106
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import streamlit as stfrom langchain.prompts import PromptTemplatefrom langchain.chat_models import ChatOpenAIfrom langchain.base_language import BaseLanguageModelfrom langchain.chains.llm import LLMChainimport openaiimport 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."""
@classmethod 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.")
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import streamlit as stimport osfrom pathlib import Pathfrom langchain_community.document_loaders import PyPDFLoaderfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain_community.vectorstores import Chromafrom langchain.chains import ConversationalRetrievalChainfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.llms import HuggingFaceEndpointfrom langchain.memory import ConversationBufferMemoryfrom unidecode import unidecodeimport chromadbimport re
list_llm = [ "mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", "google/gemma-7b-it", "google/gemma-2b-it", "HuggingFaceH4/zephyr-7b-beta", "HuggingFaceH4/zephyr-7b-gemma-v0.1", "meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2", "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct", "google/flan-t5-xxl"]
def load_doc(list_file_path, chunk_size, chunk_overlap): loaders = [PyPDFLoader(x) for x in list_file_path] pages = [] for loader in loaders: pages.extend(loader.load()) text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) doc_splits = text_splitter.split_documents(pages) return doc_splits
def create_db(splits, collection_name): embedding = HuggingFaceEmbeddings() new_client = chromadb.EphemeralClient() vectordb = Chroma.from_documents( documents=splits, embedding=embedding, client=new_client, collection_name=collection_name ) return vectordb
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db): llm = HuggingFaceEndpoint(repo_id=llm_model, temperature=temperature, max_new_tokens=max_tokens, top_k=top_k) memory = ConversationBufferMemory(memory_key="chat_history", output_key='answer', return_messages=True) retriever = vector_db.as_retriever() qa_chain = ConversationalRetrievalChain.from_llm( llm, retriever=retriever, chain_type="stuff", memory=memory, return_source_documents=True, verbose=False ) return qa_chain
def create_collection_name(file_path): collection_name = Path(file_path).stem collection_name = unidecode(collection_name) collection_name = re.sub('[^A-Za-z0-9]+', '-', collection_name) collection_name = collection_name[:50] if len(collection_name) < 3: collection_name = collection_name + 'xyz' if not collection_name[0].isalnum(): collection_name = 'A' + collection_name[1:] if not collection_name[-1].isalnum(): collection_name = collection_name[:-1] + 'Z' return collection_name
def main(): st.title("PDF-based Chatbot")
uploaded_files = st.file_uploader("Upload PDF documents (single or multiple)", type="pdf", accept_multiple_files=True)
if uploaded_files: chunk_size = st.slider("Chunk size", min_value=100, max_value=1000, value=600, step=20) chunk_overlap = st.slider("Chunk overlap", min_value=10, max_value=200, value=40, step=10)
list_file_path = [file.name for file in uploaded_files]
if st.button("Generate Vector Database"): st.text("Loading documents...") doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap) st.text("Creating vector database...") collection_name = create_collection_name(list_file_path[0]) vector_db = create_db(doc_splits, collection_name)
llm_model = st.selectbox("Choose LLM Model", list_llm) temperature = st.slider("Temperature", min_value=0.01, max_value=1.0, value=0.7, step=0.1) max_tokens = st.slider("Max Tokens", min_value=224, max_value=4096, value=1024, step=32) top_k = st.slider("Top-K Samples", min_value=1, max_value=10, value=3, step=1)
if st.button("Initialize QA Chain"): st.text("Initializing QA chain...") qa_chain = initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db)
st.header("Chatbot") message = st.text_input("Type your message") if st.button("Submit"): st.text("Generating response...") response = qa_chain({"question": message, "chat_history": []}) st.write("Assistant:", response["answer"])
if __name__ == "__main__": main()pages/descriptionGen.pyAdded+50
pages/jarvis.pyDeleted-106