kristada673 commited on
Commit
f698ede
1 Parent(s): f21b080

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, gradio
2
+ from langchain.document_loaders import UnstructuredPDFLoader
3
+ from langchain.indexes import VectorstoreIndexCreator
4
+ from detectron2.config import get_cfg
5
+ import nltk
6
+
7
+ try:
8
+ nltk.data.find('tokenizers/punkt')
9
+ except LookupError:
10
+ nltk.download('punkt')
11
+
12
+ os.environ['OPENAI_API_KEY'] = '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'
13
+
14
+ cfg = get_cfg()
15
+ cfg.MODEL.DEVICE = 'gpu'
16
+
17
+ text_folder = 'AsomBarta-NewsLetter'
18
+ loaders = [UnstructuredPDFLoader(os.path.join(text_folder, fn)) for fn in os.listdir(text_folder)]
19
+
20
+ index = VectorstoreIndexCreator().from_loaders(loaders)
21
+
22
+ 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.
23
+ 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
24
+ newspapers, it will simply reply with "I don't know". Enjoy!'''
25
+
26
+ def chat_response(query):
27
+ return index.query(query)
28
+
29
+ interface = gradio.Interface(fn=chat_response, inputs="text", outputs="text", title='Asom Barta Q&A Bot', description=description)
30
+
31
+ interface.launch()