Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,7 @@ import os
|
|
| 2 |
from typing import Iterable
|
| 3 |
from tempfile import TemporaryDirectory
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
-
from typing import Iterator
|
| 6 |
-
from docling.document_converter import DocumentConverter
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
|
| 9 |
from langchain_milvus import Milvus
|
|
@@ -17,28 +16,28 @@ from fastapi import FastAPI, HTTPException
|
|
| 17 |
from pydantic import BaseModel
|
| 18 |
import gradio as gr
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
self._file_paths = file_paths if isinstance(file_paths, list) else [file_paths]
|
| 23 |
-
self._converter = DocumentConverter()
|
| 24 |
|
| 25 |
def lazy_load(self) -> Iterator[LCDocument]:
|
| 26 |
-
for
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def load(self) ->
|
| 32 |
return list(self.lazy_load())
|
| 33 |
|
| 34 |
# Load environment variables
|
| 35 |
load_dotenv()
|
| 36 |
-
|
| 37 |
# File paths configuration
|
| 38 |
-
FILE_PATHS = ["vol1.
|
| 39 |
|
| 40 |
# Load and split documents
|
| 41 |
-
loader =
|
| 42 |
docs = loader.load()
|
| 43 |
|
| 44 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
@@ -107,7 +106,6 @@ async def query_documents(request: QueryRequest):
|
|
| 107 |
except Exception as e:
|
| 108 |
raise HTTPException(status_code=500, detail=str(e))
|
| 109 |
|
| 110 |
-
|
| 111 |
# Gradio interface
|
| 112 |
def ask_question(question):
|
| 113 |
return rag_chain.invoke(question)
|
|
|
|
| 2 |
from typing import Iterable
|
| 3 |
from tempfile import TemporaryDirectory
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from typing import Iterator, List, Union
|
|
|
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
|
| 8 |
from langchain_milvus import Milvus
|
|
|
|
| 16 |
from pydantic import BaseModel
|
| 17 |
import gradio as gr
|
| 18 |
|
| 19 |
+
# Custom Text File Loader
|
| 20 |
+
class TextFileLoader(BaseLoader):
|
| 21 |
+
def __init__(self, file_paths: Union[str, List[str]]) -> None:
|
| 22 |
self._file_paths = file_paths if isinstance(file_paths, list) else [file_paths]
|
|
|
|
| 23 |
|
| 24 |
def lazy_load(self) -> Iterator[LCDocument]:
|
| 25 |
+
for file_path in self._file_paths:
|
| 26 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
| 27 |
+
text = file.read()
|
| 28 |
+
yield LCDocument(page_content=text)
|
| 29 |
+
|
| 30 |
+
def load(self) -> List[LCDocument]:
|
| 31 |
return list(self.lazy_load())
|
| 32 |
|
| 33 |
# Load environment variables
|
| 34 |
load_dotenv()
|
| 35 |
+
|
| 36 |
# File paths configuration
|
| 37 |
+
FILE_PATHS = ["vol1.txt", "vol2.txt", "vol3.txt", "vol4.txt", "vol5.txt"]
|
| 38 |
|
| 39 |
# Load and split documents
|
| 40 |
+
loader = TextFileLoader(file_paths=FILE_PATHS)
|
| 41 |
docs = loader.load()
|
| 42 |
|
| 43 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
raise HTTPException(status_code=500, detail=str(e))
|
| 108 |
|
|
|
|
| 109 |
# Gradio interface
|
| 110 |
def ask_question(question):
|
| 111 |
return rag_chain.invoke(question)
|