twimbit-ai commited on
Commit
3432c0b
·
1 Parent(s): 2b129b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -7
app.py CHANGED
@@ -1,15 +1,17 @@
1
  import os
 
2
  from typing import Optional, Tuple
3
 
4
  import gradio as gr
5
  from query_data import get_chain
6
  from threading import Lock
7
  import pinecone
8
- from langchain.vectorstores import Chroma, Pinecone
9
  from langchain.embeddings.openai import OpenAIEmbeddings
10
 
 
11
 
12
- embeddings = OpenAIEmbeddings()
13
 
14
  PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
15
  PINECONE_API_ENV = os.getenv("PINECONE_API_ENV")
@@ -22,6 +24,12 @@ pinecone.init(
22
  )
23
 
24
  index_name = PINECONE_INDEX
 
 
 
 
 
 
25
  vectorstore = Pinecone.from_existing_index(index_name=index_name, embedding=embeddings)
26
 
27
  chain = get_chain(vectorstore)
@@ -39,18 +47,35 @@ class ChatWrapper:
39
  self.lock.acquire()
40
  try:
41
  history = history or []
 
42
  # If chain is None, that is because no API key was provided.
43
  # if chain is None:
44
- # history.append((inp, "Please paste your OpenAI key to use"))
45
  # return history, history
46
  # Set OpenAI key
47
 
48
  import openai
49
  openai.api_key = os.getenv("OPENAI_API_KEY")
50
- # Run chain and append input.
51
- output = chain({"question": inp, "chat_history": history})["answer"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  history.append((inp, output))
53
- print(history)
54
  except Exception as e:
55
  raise e
56
  finally:
@@ -104,4 +129,4 @@ with block:
104
  # )
105
 
106
  # block.launch(debug=True)
107
- block.launch(debug=True, auth=('admin', 'Twimbit@2019'), auth_message='enter username password to proceed further')
 
1
  import os
2
+ import time
3
  from typing import Optional, Tuple
4
 
5
  import gradio as gr
6
  from query_data import get_chain
7
  from threading import Lock
8
  import pinecone
9
+ from langchain.vectorstores import Pinecone
10
  from langchain.embeddings.openai import OpenAIEmbeddings
11
 
12
+ from dotenv import load_dotenv
13
 
14
+ load_dotenv()
15
 
16
  PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
17
  PINECONE_API_ENV = os.getenv("PINECONE_API_ENV")
 
24
  )
25
 
26
  index_name = PINECONE_INDEX
27
+
28
+ embeddings = OpenAIEmbeddings()
29
+
30
+ # with open("posts.pkl", "rb") as f:
31
+ # vectorstore = pickle.load(f)
32
+
33
  vectorstore = Pinecone.from_existing_index(index_name=index_name, embedding=embeddings)
34
 
35
  chain = get_chain(vectorstore)
 
47
  self.lock.acquire()
48
  try:
49
  history = history or []
50
+
51
  # If chain is None, that is because no API key was provided.
52
  # if chain is None:
53
+ # history.append((inp, "Please paste your OpenAI key to use"))
54
  # return history, history
55
  # Set OpenAI key
56
 
57
  import openai
58
  openai.api_key = os.getenv("OPENAI_API_KEY")
59
+ start_time = time.time()
60
+ chain_obj = chain({"question": inp, "chat_history": history})
61
+ print('=======time===== : ' + str(time.time() - start_time))
62
+
63
+ output = chain_obj["answer"]
64
+
65
+ sources = chain_obj['source_documents']
66
+
67
+ source_link = []
68
+ sources_str = 'Sources:'
69
+ for source in sources:
70
+ if source.metadata['source'] not in source_link:
71
+ source_link.append(source.metadata['source'])
72
+ sources_str += '\n' + source.metadata['source']
73
+
74
+ if 'Unfortunately, our current pool of insights does not have an answer to this.' not in output:
75
+ output += '\n\n' + sources_str
76
+
77
  history.append((inp, output))
78
+ # print(chain_obj)
79
  except Exception as e:
80
  raise e
81
  finally:
 
129
  # )
130
 
131
  # block.launch(debug=True)
132
+ # block.launch(debug=True, auth=('admin', 'Twimbit@2019'), auth_message='enter username password to proceed further')