Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
import os
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
import docx
|
5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
6 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain.chains.question_answering import load_qa_chain
|
9 |
+
from langchain_openai import OpenAI
|
10 |
+
from langchain.callbacks import get_openai_callback
|
11 |
+
import gradio as gr
|
12 |
+
from aiohttp import web
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
17 |
+
|
18 |
+
|
19 |
+
def read_txt(file_path):
|
20 |
+
with open(file_path, "r") as file:
|
21 |
+
text = file.read()
|
22 |
+
return text
|
23 |
+
|
24 |
+
def read_documents_from_directory(directory):
|
25 |
+
combined_text = ""
|
26 |
+
for filename in os.listdir(directory):
|
27 |
+
file_path = os.path.join(directory, filename)
|
28 |
+
if filename.endswith(".pdf"):
|
29 |
+
combined_text += read_pdf(file_path)
|
30 |
+
elif filename.endswith(".docx"):
|
31 |
+
combined_text += read_word(file_path)
|
32 |
+
elif filename.endswith(".txt"):
|
33 |
+
combined_text += read_txt(file_path)
|
34 |
+
return combined_text
|
35 |
+
|
36 |
+
text_file_path = '/content/lawsofpower.txt'
|
37 |
+
user_query = read_txt(text_file_path)
|
38 |
+
|
39 |
+
char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
|
40 |
+
chunk_overlap=200, length_function=len)
|
41 |
+
|
42 |
+
text_chunks = char_text_splitter.split_text(user_query)
|
43 |
+
|
44 |
+
embeddings = OpenAIEmbeddings()
|
45 |
+
docsearch = FAISS.from_texts(text_chunks, embeddings)
|
46 |
+
|
47 |
+
llm = OpenAI()
|
48 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
49 |
+
|
50 |
+
async def chatbot_interface(request):
|
51 |
+
data = await request.post()
|
52 |
+
input_text = data.get("input_text", "")
|
53 |
+
|
54 |
+
docs = docsearch.similarity_search(input_text)
|
55 |
+
response = chain.run(input_documents=docs, question=input_text)
|
56 |
+
|
57 |
+
return web.Response(text=response)
|
58 |
+
|
59 |
+
app = web.Application()
|
60 |
+
app.router.add_post('/chatbot', chatbot_interface)
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
web.run_app(app, port=os.getenv("PORT", 8080))
|