pattonma commited on
Commit
a43235d
1 Parent(s): 1318cbe

forgot the chainlit changes

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  from typing import List
3
  from chainlit.types import AskFileResponse
4
- from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader
5
  from aimakerspace.openai_utils.prompts import (
6
  UserRolePrompt,
7
  SystemRolePrompt,
@@ -47,8 +47,21 @@ class RetrievalAugmentedQAPipeline:
47
 
48
  return {"response": generate_response(), "context": context_list}
49
 
50
- text_splitter = CharacterTextSplitter()
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  def process_text_file(file: AskFileResponse):
54
  import tempfile
@@ -72,7 +85,7 @@ async def on_chat_start():
72
  # Wait for the user to upload a file
73
  while files == None:
74
  files = await cl.AskFileMessage(
75
- content="Please upload a Text File file to begin!",
76
  accept=["text/plain"],
77
  max_size_mb=2,
78
  timeout=180,
@@ -86,7 +99,7 @@ async def on_chat_start():
86
  await msg.send()
87
 
88
  # load the file
89
- texts = process_text_file(file)
90
 
91
  print(f"Processing {len(texts)} text chunks")
92
 
 
1
  import os
2
  from typing import List
3
  from chainlit.types import AskFileResponse
4
+ from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader, PDFLoader, SentenceTextSplitter
5
  from aimakerspace.openai_utils.prompts import (
6
  UserRolePrompt,
7
  SystemRolePrompt,
 
47
 
48
  return {"response": generate_response(), "context": context_list}
49
 
50
+ text_splitter = SentenceTextSplitter()
51
 
52
+ def process_pdf_file(file: AskFileResponse):
53
+ import tempfile
54
+
55
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".pdf") as temp_file:
56
+ temp_file_path = temp_file.name
57
+
58
+ with open(temp_file_path, "wb") as f:
59
+ f.write(file.content)
60
+
61
+ pdf_loader = PDFLoader(temp_file_path)
62
+ documents = pdf_loader.load_documents()
63
+ texts = text_splitter.split_texts(documents)
64
+ return texts
65
 
66
  def process_text_file(file: AskFileResponse):
67
  import tempfile
 
85
  # Wait for the user to upload a file
86
  while files == None:
87
  files = await cl.AskFileMessage(
88
+ content="Please upload a PDF File file to begin!",
89
  accept=["text/plain"],
90
  max_size_mb=2,
91
  timeout=180,
 
99
  await msg.send()
100
 
101
  # load the file
102
+ texts = process_pdf_file(file)
103
 
104
  print(f"Processing {len(texts)} text chunks")
105