Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ from langchain.document_loaders import PyPDFLoader, UnstructuredWordDocumentLoad
|
|
| 3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
from langchain.embeddings import HuggingFaceInferenceAPIEmbeddings
|
| 5 |
from langchain.vectorstores import Chroma
|
|
|
|
| 6 |
import os
|
| 7 |
-
from io import BytesIO
|
| 8 |
from groq import Groq
|
| 9 |
|
| 10 |
# Initialize the Groq API client
|
|
@@ -18,21 +18,27 @@ def get_groq_response(prompt, model="llama3-8b-8192"):
|
|
| 18 |
return chat_completion.choices[0].message.content
|
| 19 |
|
| 20 |
def process_file(uploaded_file):
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
documents = pdf_loader.load()
|
| 26 |
-
elif
|
| 27 |
-
word_loader = UnstructuredWordDocumentLoader(
|
| 28 |
documents = word_loader.load()
|
| 29 |
-
elif
|
| 30 |
-
text_loader = TextLoader(
|
| 31 |
documents = text_loader.load()
|
| 32 |
else:
|
| 33 |
st.error("Unsupported file type.")
|
| 34 |
return None
|
| 35 |
|
|
|
|
|
|
|
| 36 |
return documents
|
| 37 |
|
| 38 |
def answer_with_retrieval(prompt, retriever):
|
|
|
|
| 3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
from langchain.embeddings import HuggingFaceInferenceAPIEmbeddings
|
| 5 |
from langchain.vectorstores import Chroma
|
| 6 |
+
import tempfile
|
| 7 |
import os
|
|
|
|
| 8 |
from groq import Groq
|
| 9 |
|
| 10 |
# Initialize the Groq API client
|
|
|
|
| 18 |
return chat_completion.choices[0].message.content
|
| 19 |
|
| 20 |
def process_file(uploaded_file):
|
| 21 |
+
# Save the uploaded file to a temporary file
|
| 22 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
| 23 |
+
temp_file.write(uploaded_file.getvalue())
|
| 24 |
+
temp_file_path = temp_file.name
|
| 25 |
|
| 26 |
+
# Process the file based on its type
|
| 27 |
+
if uploaded_file.type == "application/pdf":
|
| 28 |
+
pdf_loader = PyPDFLoader(temp_file_path)
|
| 29 |
documents = pdf_loader.load()
|
| 30 |
+
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| 31 |
+
word_loader = UnstructuredWordDocumentLoader(temp_file_path)
|
| 32 |
documents = word_loader.load()
|
| 33 |
+
elif uploaded_file.type == "text/plain":
|
| 34 |
+
text_loader = TextLoader(temp_file_path)
|
| 35 |
documents = text_loader.load()
|
| 36 |
else:
|
| 37 |
st.error("Unsupported file type.")
|
| 38 |
return None
|
| 39 |
|
| 40 |
+
# Clean up the temporary file
|
| 41 |
+
os.remove(temp_file_path)
|
| 42 |
return documents
|
| 43 |
|
| 44 |
def answer_with_retrieval(prompt, retriever):
|