Spaces:
Sleeping
Sleeping
File size: 1,804 Bytes
85c4fe0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# created custom class for WhatsAppChatLoader - because original langchain one isnt working
import re
from pathlib import Path
from typing import List
from langchain.docstore.document import Document
from langchain.document_loaders.base import BaseLoader
def concatenate_rows(date: str, sender: str, text: str) -> str:
"""Combine message information in a readable format ready to be used."""
return f"{sender} on {date}: {text}\n\n"
# def concatenate_rows(date: str, sender: str, text: str) -> str:
# """Combine message information in a readable format ready to be used."""
# return f"{text}\n"
class WhatsAppChatLoader(BaseLoader):
"""Load `WhatsApp` messages text file."""
def __init__(self, path: str):
"""Initialize with path."""
self.file_path = path
def load(self) -> List[Document]:
"""Load documents."""
p = Path(self.file_path)
text_content = ""
ignore_lines = ["This message was deleted", "<Media omitted>"]
#########################################################################################
# original code from langchain replaced with this code
#########################################################################################
# use https://whatstk.streamlit.app/ to get CSV
import pandas as pd
df = pd.read_csv(p)[['date', 'username', 'message']]
for i,row in df.iterrows():
date = row['date']
sender = row['username']
text = row['message']
if not any(x in text for x in ignore_lines):
text_content += concatenate_rows(date, sender, text)
metadata = {"source": str(p)}
return [Document(page_content=text_content.strip(), metadata=metadata)] |