kdembla commited on
Commit
0118b41
·
verified ·
1 Parent(s): f875214

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -114
app.py CHANGED
@@ -1,114 +1,114 @@
1
- import gradio as gr
2
- from langchain_openai import ChatOpenAI
3
- from langchain.memory import ConversationBufferMemory
4
- from langchain.chains import ConversationalRetrievalChain
5
- from langchain_community.document_loaders import UnstructuredPDFLoader
6
- from langchain_openai import OpenAIEmbeddings
7
- from langchain.text_splitter import RecursiveCharacterTextSplitter
8
- from langchain_community.vectorstores import DocArrayInMemorySearch
9
- from langchain_core.runnables import RunnableLambda, RunnablePassthrough
10
- from langchain.prompts import PromptTemplate
11
- from langfuse.callback import CallbackHandler
12
-
13
- import os
14
- from dotenv import load_dotenv
15
-
16
- load_dotenv()
17
- API_KEY = os.getenv("OPENAI_API_KEY")
18
- LANGFUSE_SK = os.getenv("LANGFUSE_SECRET_KEY")
19
- LANGFUSE_PK = os.getenv("LANGFUSE_PUBLIC_KEY")
20
- HOST = os.getenv("LANGFUSE_HOST")
21
-
22
- langfuse_handler = CallbackHandler(
23
- secret_key=LANGFUSE_SK,
24
- public_key=LANGFUSE_PK,
25
- host=HOST,
26
- debug=True,
27
- session_id="1505",
28
- release="1.0.0",
29
- user_id="Krish",
30
- trace_name="WarehouseAst"
31
- )
32
-
33
- # Function to create the chat chain
34
- def create_chat_chain(file_path, openai_model):
35
- docs = []
36
- for filepath in file_path:
37
- loader = UnstructuredPDFLoader(filepath)
38
- docs.extend(loader.load())
39
-
40
- # Splitting txt
41
- text_splitter = RecursiveCharacterTextSplitter(
42
- chunk_size=500,
43
- chunk_overlap=100
44
- )
45
- split_documents = text_splitter.split_documents(docs)
46
-
47
- #Creating embeddings
48
- embeddings = OpenAIEmbeddings(openai_api_key=API_KEY)
49
- vectordb = DocArrayInMemorySearch.from_documents(split_documents, embeddings)
50
-
51
- #Retriever setip
52
- retriever = vectordb.as_retriever(
53
- search_type='mmr'
54
- )
55
-
56
- #memory setup
57
- memory = ConversationBufferMemory(
58
- memory_key='chat_history',
59
- return_messages=True,
60
- output_key='answer' # Explicitly set the output key to avoid ambiguity
61
- )
62
-
63
- llm_def = ChatOpenAI(model=openai_model, temperature=0, api_key=API_KEY, max_tokens=1100)
64
- chain = ConversationalRetrievalChain.from_llm(
65
- llm=llm_def,
66
- retriever=retriever,
67
- memory=memory,
68
- return_source_documents=True,
69
- verbose=True,
70
- callbacks=[langfuse_handler]
71
- )
72
- return chain
73
-
74
- # Function to process user input
75
- def process_input(chain, user_message):
76
- inputs = {"question": user_message}
77
- outputs = chain(inputs)
78
- return outputs['answer']
79
-
80
- if __name__ == "__main__":
81
- openai_model = "gpt-3.5-turbo"
82
- file_path = [
83
- r"C:\WarehouseAsst_PoC\data\Advantage Dashboard Functional Description.pdf",
84
- r"data\user_guide.pdf"
85
- ]
86
- chain = create_chat_chain(file_path, openai_model)
87
-
88
- # Gradio UI
89
- with gr.Blocks() as demo:
90
- with gr.Row():
91
- gr.Markdown("<h1 style='text-align: center; width: 100%;'>RAG with Memory (Warehouse Assistant)</h1>")
92
- with gr.Row():
93
- with gr.Column(scale=1):
94
- gr.Markdown("**Here's how to use this demo:**")
95
- gr.Markdown("""This a conversational agent responsible for summarizing, and giving key highlights about the High Jump Warehouse System Guide .\n
96
- Feel free to experiment with your own different queries and usage or accesibility requests!
97
- """)
98
- with gr.Column(scale=3):
99
- ex = ["Where can I find the Inbound pallets menu?", "How can I navigate the Pallet Question Master?", "Where can I find the A-Frame Items Configurations menu?"]
100
- chatbot = gr.Chatbot()
101
- msg = gr.Textbox()
102
- gr.Examples(ex, msg)
103
- clear = gr.Button("Clear Chat History")
104
-
105
- def handle_message(user_message, history):
106
- answer = process_input(chain, user_message)
107
- history.append([user_message, answer])
108
- return "", history
109
-
110
- msg.submit(handle_message, [msg, chatbot], [msg, chatbot])
111
- clear.click(lambda: chain.memory.clear(), None, chatbot)
112
-
113
- demo.queue()
114
- demo.launch()
 
1
+ import gradio as gr
2
+ from langchain_openai import ChatOpenAI
3
+ from langchain.memory import ConversationBufferMemory
4
+ from langchain.chains import ConversationalRetrievalChain
5
+ from langchain_community.document_loaders import UnstructuredPDFLoader
6
+ from langchain_openai import OpenAIEmbeddings
7
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
+ from langchain_community.vectorstores import DocArrayInMemorySearch
9
+ from langchain_core.runnables import RunnableLambda, RunnablePassthrough
10
+ from langchain.prompts import PromptTemplate
11
+ from langfuse.callback import CallbackHandler
12
+
13
+ import os
14
+ from dotenv import load_dotenv
15
+
16
+ load_dotenv()
17
+ API_KEY = os.getenv("OPENAI_API_KEY")
18
+ LANGFUSE_SK = os.getenv("LANGFUSE_SECRET_KEY")
19
+ LANGFUSE_PK = os.getenv("LANGFUSE_PUBLIC_KEY")
20
+ HOST = os.getenv("LANGFUSE_HOST")
21
+
22
+ langfuse_handler = CallbackHandler(
23
+ secret_key=LANGFUSE_SK,
24
+ public_key=LANGFUSE_PK,
25
+ host=HOST,
26
+ debug=True,
27
+ session_id="1505",
28
+ release="1.0.0",
29
+ user_id="Krish",
30
+ trace_name="WarehouseAst"
31
+ )
32
+
33
+ # Function to create the chat chain
34
+ def create_chat_chain(file_path, openai_model):
35
+ docs = []
36
+ for filepath in file_path:
37
+ loader = UnstructuredPDFLoader(filepath)
38
+ docs.extend(loader.load())
39
+
40
+ # Splitting txt
41
+ text_splitter = RecursiveCharacterTextSplitter(
42
+ chunk_size=500,
43
+ chunk_overlap=100
44
+ )
45
+ split_documents = text_splitter.split_documents(docs)
46
+
47
+ #Creating embeddings
48
+ embeddings = OpenAIEmbeddings(openai_api_key=API_KEY)
49
+ vectordb = DocArrayInMemorySearch.from_documents(split_documents, embeddings)
50
+
51
+ #Retriever setip
52
+ retriever = vectordb.as_retriever(
53
+ search_type='mmr'
54
+ )
55
+
56
+ #memory setup
57
+ memory = ConversationBufferMemory(
58
+ memory_key='chat_history',
59
+ return_messages=True,
60
+ output_key='answer' # Explicitly set the output key to avoid ambiguity
61
+ )
62
+
63
+ llm_def = ChatOpenAI(model=openai_model, temperature=0, api_key=API_KEY, max_tokens=1100)
64
+ chain = ConversationalRetrievalChain.from_llm(
65
+ llm=llm_def,
66
+ retriever=retriever,
67
+ memory=memory,
68
+ return_source_documents=True,
69
+ verbose=True,
70
+ callbacks=[langfuse_handler]
71
+ )
72
+ return chain
73
+
74
+ # Function to process user input
75
+ def process_input(chain, user_message):
76
+ inputs = {"question": user_message}
77
+ outputs = chain(inputs)
78
+ return outputs['answer']
79
+
80
+ if __name__ == "__main__":
81
+ openai_model = "gpt-3.5-turbo"
82
+ file_path = [
83
+ r"Advantage Dashboard Functional Description.pdf",
84
+ r"data\user_guide.pdf"
85
+ ]
86
+ chain = create_chat_chain(file_path, openai_model)
87
+
88
+ # Gradio UI
89
+ with gr.Blocks() as demo:
90
+ with gr.Row():
91
+ gr.Markdown("<h1 style='text-align: center; width: 100%;'>RAG with Memory (Warehouse Assistant)</h1>")
92
+ with gr.Row():
93
+ with gr.Column(scale=1):
94
+ gr.Markdown("**Here's how to use this demo:**")
95
+ gr.Markdown("""This a conversational agent responsible for summarizing, and giving key highlights about the High Jump Warehouse System Guide .\n
96
+ Feel free to experiment with your own different queries and usage or accesibility requests!
97
+ """)
98
+ with gr.Column(scale=3):
99
+ ex = ["Where can I find the Inbound pallets menu?", "How can I navigate the Pallet Question Master?", "Where can I find the A-Frame Items Configurations menu?"]
100
+ chatbot = gr.Chatbot()
101
+ msg = gr.Textbox()
102
+ gr.Examples(ex, msg)
103
+ clear = gr.Button("Clear Chat History")
104
+
105
+ def handle_message(user_message, history):
106
+ answer = process_input(chain, user_message)
107
+ history.append([user_message, answer])
108
+ return "", history
109
+
110
+ msg.submit(handle_message, [msg, chatbot], [msg, chatbot])
111
+ clear.click(lambda: chain.memory.clear(), None, chatbot)
112
+
113
+ demo.queue()
114
+ demo.launch()