kristada673 commited on
Commit
8f0f959
·
1 Parent(s): 57de172

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -1,37 +1,27 @@
1
  import os, gradio
2
  from langchain.document_loaders import UnstructuredPDFLoader
3
  from langchain.indexes import VectorstoreIndexCreator
4
- import nltk
5
 
6
- os.system("pip3 install 'git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI'")
7
- try:
8
- import detectron2
9
- except:
10
- os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
11
-
12
- from detectron2.config import get_cfg
13
-
14
- try:
15
- nltk.data.find('tokenizers/punkt')
16
- except LookupError:
17
- nltk.download('punkt')
18
-
19
- os.environ['OPENAI_API_KEY'] = 'OPEN-API-KEY'
20
-
21
- cfg = get_cfg()
22
- cfg.MODEL.DEVICE = 'gpu'
23
 
24
  text_folder = '.'
25
  loaders = [UnstructuredPDFLoader(os.path.join(text_folder, fn)) for fn in os.listdir(text_folder)]
26
 
27
- index = VectorstoreIndexCreator().from_loaders(loaders)
 
 
 
 
 
 
 
28
 
29
- description = '''This is an AI conversational agent who you can ask questions to. The ONLY documents it has access to are the Asom Barta newspapers from July 2022 to May 2023.
30
- So, the bot can only frame its answers based on its worldview attained from the Asom Barta newspapers. If you ask it about anything not pertaining to the content of the
31
  newspapers, it will simply reply with "I don't know". Enjoy!'''
32
 
33
  def chat_response(query):
34
- return index.query(query)
35
 
36
  interface = gradio.Interface(fn=chat_response, inputs="text", outputs="text", title='Asom Barta Q&A Bot', description=description)
37
 
 
1
  import os, gradio
2
  from langchain.document_loaders import UnstructuredPDFLoader
3
  from langchain.indexes import VectorstoreIndexCreator
 
4
 
5
+ os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  text_folder = '.'
8
  loaders = [UnstructuredPDFLoader(os.path.join(text_folder, fn)) for fn in os.listdir(text_folder)]
9
 
10
+ # Create the index, if it does not exist, and save it
11
+ if not os.path.isfile('VectorStoreIndex/chroma-embeddings.parquet'):
12
+ from langchain.vectorstores import Chroma
13
+ index = VectorstoreIndexCreator(vectorstore_cls=Chroma, vectorstore_kwargs={ "persist_directory": "VectorStoreIndex/"}).from_loaders(loaders)
14
+ index.vectorstore.persist()
15
+
16
+ # Load the saved index
17
+ index_saved = VectorstoreIndexCreator().from_persistent_index("VectorStoreIndex/")
18
 
19
+ description = '''This is an AI conversational agent that has studied the Asom Barta newspapers over the last 1 year, from June 2022 to May 2023. You can ask it any question pertaining to this period, and it will answer it.
20
+ \n\nThe AI can only frame its answers based on its worldview attained from the Asom Barta newspapers. If you ask it about anything not pertaining to the content of the
21
  newspapers, it will simply reply with "I don't know". Enjoy!'''
22
 
23
  def chat_response(query):
24
+ return index_saved.query(query)
25
 
26
  interface = gradio.Interface(fn=chat_response, inputs="text", outputs="text", title='Asom Barta Q&A Bot', description=description)
27