Rajut commited on
Commit
9290216
·
verified ·
1 Parent(s): 56abf51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -22
app.py CHANGED
@@ -1,5 +1,3 @@
1
- import dotenv
2
- from dotenv import load_dotenv
3
  import os
4
  from PyPDF2 import PdfReader
5
  import docx
@@ -11,36 +9,22 @@ from langchain_openai import OpenAI
11
  from langchain.callbacks import get_openai_callback
12
  import gradio as gr
13
  from aiohttp import web
14
- import gradio as gr
15
-
16
- load_dotenv()
17
 
 
18
  os.environ["OPENAI_API_KEY"] = "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"
19
 
20
-
21
  def read_txt(file_path):
22
  with open(file_path, "r") as file:
23
  text = file.read()
24
  return text
25
 
26
- def read_documents_from_directory(directory):
27
- combined_text = ""
28
- for filename in os.listdir(directory):
29
- file_path = os.path.join(directory, filename)
30
- if filename.endswith(".pdf"):
31
- combined_text += read_pdf(file_path)
32
- elif filename.endswith(".docx"):
33
- combined_text += read_word(file_path)
34
- elif filename.endswith(".txt"):
35
- combined_text += read_txt(file_path)
36
- return combined_text
37
-
38
- text_file_path = '/content/lawsofpower.txt'
39
  user_query = read_txt(text_file_path)
40
 
41
- char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
42
- chunk_overlap=200, length_function=len)
43
-
44
  text_chunks = char_text_splitter.split_text(user_query)
45
 
46
  embeddings = OpenAIEmbeddings()
@@ -49,6 +33,7 @@ docsearch = FAISS.from_texts(text_chunks, embeddings)
49
  llm = OpenAI()
50
  chain = load_qa_chain(llm, chain_type="stuff")
51
 
 
52
  async def chatbot_interface(request):
53
  data = await request.post()
54
  input_text = data.get("input_text", "")
@@ -58,8 +43,10 @@ async def chatbot_interface(request):
58
 
59
  return web.Response(text=response)
60
 
 
61
  app = web.Application()
62
  app.router.add_post('/chatbot', chatbot_interface)
63
 
 
64
  if __name__ == "__main__":
65
  web.run_app(app, port=os.getenv("PORT", 8080))
 
 
 
1
  import os
2
  from PyPDF2 import PdfReader
3
  import docx
 
9
  from langchain.callbacks import get_openai_callback
10
  import gradio as gr
11
  from aiohttp import web
 
 
 
12
 
13
+ # Load OpenAI API key directly
14
  os.environ["OPENAI_API_KEY"] = "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"
15
 
16
+ # Function to read text from a file
17
  def read_txt(file_path):
18
  with open(file_path, "r") as file:
19
  text = file.read()
20
  return text
21
 
22
+ # Load text from the specified file
23
+ text_file_path = '/home/user/app/content/lawsofpower.txt'
 
 
 
 
 
 
 
 
 
 
 
24
  user_query = read_txt(text_file_path)
25
 
26
+ # Set up text processing components
27
+ char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
 
28
  text_chunks = char_text_splitter.split_text(user_query)
29
 
30
  embeddings = OpenAIEmbeddings()
 
33
  llm = OpenAI()
34
  chain = load_qa_chain(llm, chain_type="stuff")
35
 
36
+ # Define the chatbot interface
37
  async def chatbot_interface(request):
38
  data = await request.post()
39
  input_text = data.get("input_text", "")
 
43
 
44
  return web.Response(text=response)
45
 
46
+ # Set up the web application
47
  app = web.Application()
48
  app.router.add_post('/chatbot', chatbot_interface)
49
 
50
+ # Run the web server
51
  if __name__ == "__main__":
52
  web.run_app(app, port=os.getenv("PORT", 8080))