tdecae commited on
Commit
a895164
1 Parent(s): e4d6780

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -15
app.py CHANGED
@@ -1,13 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import sys
3
- import openai
4
- from langchain.chains import ConversationalRetrievalChain, RetrievalQA
5
- from langchain.chat_models import ChatOpenAI
6
  from langchain.document_loaders import DirectoryLoader, TextLoader
7
- from langchain.embeddings import OpenAIEmbeddings
8
  from langchain.indexes import VectorstoreIndexCreator
9
  from langchain.indexes.vectorstore import VectorStoreIndexWrapper
10
- from langchain.llms import OpenAI
11
  from langchain.text_splitter import CharacterTextSplitter
12
 
13
  __import__('pysqlite3')
@@ -16,8 +96,7 @@ sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
16
 
17
  from langchain.vectorstores import Chroma
18
  import gradio as gr
19
-
20
- os.environ["OPENAI_API_KEY"] = os.getenv("OPENAPIKEY")
21
 
22
  docs = []
23
 
@@ -39,11 +118,14 @@ splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
39
  docs = splitter.split_documents(docs)
40
 
41
  # Convert the document chunks to embedding and save them to the vector store
42
- vectorstore = Chroma.from_documents(docs, embedding=OpenAIEmbeddings(), persist_directory="./data")
43
  vectorstore.persist()
44
 
 
 
 
45
  chain = ConversationalRetrievalChain.from_llm(
46
- ChatOpenAI(temperature=0.7, model_name='gpt-3.5-turbo'),
47
  retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
48
  return_source_documents=True,
49
  verbose=False
@@ -52,15 +134,12 @@ chain = ConversationalRetrievalChain.from_llm(
52
  chat_history = []
53
 
54
  with gr.Blocks() as demo:
55
- chatbot = gr.Chatbot([("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my previous or most recent experience, where I'm eligible to work, when I can start work, what NLP skills I have, and much more! you can chat with me directly in multiple languages")],avatar_images=["./multiple_docs/Guest.jpg","./multiple_docs/Thierry Picture.jpg"])
56
  msg = gr.Textbox()
57
  clear = gr.Button("Clear")
58
  chat_history = []
59
 
60
  def user(query, chat_history):
61
- # print("User query:", query)
62
- # print("Chat history:", chat_history)
63
-
64
  # Convert chat history to list of tuples
65
  chat_history_tuples = []
66
  for message in chat_history:
@@ -71,11 +150,12 @@ with gr.Blocks() as demo:
71
 
72
  # Append user message and response to chat history
73
  chat_history.append((query, result["answer"]))
74
- # print("Updated chat history:", chat_history)
75
 
76
  return gr.update(value=""), chat_history
77
 
78
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
79
  clear.click(lambda: None, None, chatbot, queue=False)
80
 
81
- demo.launch(debug=True)
 
 
 
1
+ # import os
2
+ # import sys
3
+ # import openai
4
+ # from langchain.chains import ConversationalRetrievalChain, RetrievalQA
5
+ # from langchain.chat_models import ChatOpenAI
6
+ # from langchain.document_loaders import DirectoryLoader, TextLoader
7
+ # from langchain.embeddings import OpenAIEmbeddings
8
+ # from langchain.indexes import VectorstoreIndexCreator
9
+ # from langchain.indexes.vectorstore import VectorStoreIndexWrapper
10
+ # from langchain.llms import OpenAI
11
+ # from langchain.text_splitter import CharacterTextSplitter
12
+
13
+ # __import__('pysqlite3')
14
+ # import sys
15
+ # sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
16
+
17
+ # from langchain.vectorstores import Chroma
18
+ # import gradio as gr
19
+
20
+ # os.environ["OPENAI_API_KEY"] = os.getenv("OPENAPIKEY")
21
+
22
+ # docs = []
23
+
24
+ # for f in os.listdir("multiple_docs"):
25
+ # if f.endswith(".pdf"):
26
+ # pdf_path = "./multiple_docs/" + f
27
+ # loader = PyPDFLoader(pdf_path)
28
+ # docs.extend(loader.load())
29
+ # elif f.endswith('.docx') or f.endswith('.doc'):
30
+ # doc_path = "./multiple_docs/" + f
31
+ # loader = Docx2txtLoader(doc_path)
32
+ # docs.extend(loader.load())
33
+ # elif f.endswith('.txt'):
34
+ # text_path = "./multiple_docs/" + f
35
+ # loader = TextLoader(text_path)
36
+ # docs.extend(loader.load())
37
+
38
+ # splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
39
+ # docs = splitter.split_documents(docs)
40
+
41
+ # # Convert the document chunks to embedding and save them to the vector store
42
+ # vectorstore = Chroma.from_documents(docs, embedding=OpenAIEmbeddings(), persist_directory="./data")
43
+ # vectorstore.persist()
44
+
45
+ # chain = ConversationalRetrievalChain.from_llm(
46
+ # ChatOpenAI(temperature=0.7, model_name='gpt-3.5-turbo'),
47
+ # retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
48
+ # return_source_documents=True,
49
+ # verbose=False
50
+ # )
51
+
52
+ # chat_history = []
53
+
54
+ # with gr.Blocks() as demo:
55
+ # chatbot = gr.Chatbot([("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my previous or most recent experience, where I'm eligible to work, when I can start work, what NLP skills I have, and much more! you can chat with me directly in multiple languages")],avatar_images=["./multiple_docs/Guest.jpg","./multiple_docs/Thierry Picture.jpg"])
56
+ # msg = gr.Textbox()
57
+ # clear = gr.Button("Clear")
58
+ # chat_history = []
59
+
60
+ # def user(query, chat_history):
61
+ # # print("User query:", query)
62
+ # # print("Chat history:", chat_history)
63
+
64
+ # # Convert chat history to list of tuples
65
+ # chat_history_tuples = []
66
+ # for message in chat_history:
67
+ # chat_history_tuples.append((message[0], message[1]))
68
+
69
+ # # Get result from QA chain
70
+ # result = chain({"question": query, "chat_history": chat_history_tuples})
71
+
72
+ # # Append user message and response to chat history
73
+ # chat_history.append((query, result["answer"]))
74
+ # # print("Updated chat history:", chat_history)
75
+
76
+ # return gr.update(value=""), chat_history
77
+
78
+ # msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
79
+ # clear.click(lambda: None, None, chatbot, queue=False)
80
+
81
+ # demo.launch(debug=True)
82
+
83
  import os
84
  import sys
85
+ from langchain.chains import ConversationalRetrievalChain
 
 
86
  from langchain.document_loaders import DirectoryLoader, TextLoader
87
+ from langchain.embeddings import HuggingFaceEmbeddings
88
  from langchain.indexes import VectorstoreIndexCreator
89
  from langchain.indexes.vectorstore import VectorStoreIndexWrapper
90
+ from langchain.llms import HuggingFaceLLM
91
  from langchain.text_splitter import CharacterTextSplitter
92
 
93
  __import__('pysqlite3')
 
96
 
97
  from langchain.vectorstores import Chroma
98
  import gradio as gr
99
+ from transformers import pipeline
 
100
 
101
  docs = []
102
 
 
118
  docs = splitter.split_documents(docs)
119
 
120
  # Convert the document chunks to embedding and save them to the vector store
121
+ vectorstore = Chroma.from_documents(docs, embedding=HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2"), persist_directory="./data")
122
  vectorstore.persist()
123
 
124
+ # Load the Hugging Face model
125
+ llm = HuggingFaceLLM(pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B"))
126
+
127
  chain = ConversationalRetrievalChain.from_llm(
128
+ llm,
129
  retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
130
  return_source_documents=True,
131
  verbose=False
 
134
  chat_history = []
135
 
136
  with gr.Blocks() as demo:
137
+ chatbot = gr.Chatbot([("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my previous or most recent experience, where I'm eligible to work, when I can start work, what NLP skills I have, and much more! you can chat with me directly in multiple languages")], avatar_images=["./multiple_docs/Guest.jpg","./multiple_docs/Thierry Picture.jpg"])
138
  msg = gr.Textbox()
139
  clear = gr.Button("Clear")
140
  chat_history = []
141
 
142
  def user(query, chat_history):
 
 
 
143
  # Convert chat history to list of tuples
144
  chat_history_tuples = []
145
  for message in chat_history:
 
150
 
151
  # Append user message and response to chat history
152
  chat_history.append((query, result["answer"]))
 
153
 
154
  return gr.update(value=""), chat_history
155
 
156
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
157
  clear.click(lambda: None, None, chatbot, queue=False)
158
 
159
+ demo.launch(debug=True)
160
+
161
+