| 1 | import re |
| 2 | import requests |
| 3 | from bs4 import BeautifulSoup |
| 4 | from abc import ABC, abstractmethod |
| 5 | |
| 6 | def Source(source:str, instruction:str|None=None) ->'AbstractSource': |
| 7 | """ |
| 8 | Creates a source object |
| 9 | |
| 10 | Parameters: |
| 11 | source (str): path to source |
| 12 | instruction (str): source specific instruction for the LLM |
| 13 | |
| 14 | Returns: |
| 15 | AbstractSource: source |
| 16 | """ |
| 17 | |
| 18 | if not isinstance(source, str) or source == "": |
| 19 | raise Exception("Invalid argument, source should be a none empty string.") |
| 20 | |
| 21 | s = None |
| 22 | |
| 23 | if ".pdf" in source.lower(): |
| 24 | s = PDF(source) |
| 25 | elif ".html" in source.lower() or "http" in source.lower(): |
| 26 | s = HTML(source) |
| 27 | else: |
| 28 | s = TEXT(source) |
| 29 | |
| 30 | # Set source instructions |
| 31 | s.instruction = instruction |
| 32 | |
| 33 | return s |
| 34 | |
| 35 | class AbstractSource(ABC): |
| 36 | """ |
| 37 | Abstract class representing a source file |
| 38 | """ |
| 39 | |
| 40 | def __init__(self, source:str): |
| 41 | self.source = source |
| 42 | self.content = None |
| 43 | |
| 44 | @abstractmethod |
| 45 | def load(self) -> str: |
| 46 | pass |
| 47 | |
| 48 | def __eq__(self, other) -> bool: |
| 49 | if not isinstance(other, AbstractSource): |
| 50 | return False |
| 51 | |
| 52 | return self.source == other.source |
| 53 | |
| 54 | def __hash__(self): |
| 55 | return hash(self.source) |
| 56 | |
| 57 | class PDF(AbstractSource): |
| 58 | """ |
| 59 | PDF resource |
| 60 | """ |
| 61 | |
| 62 | def __init__(self, source): |
| 63 | super().__init__(source) |
| 64 | |
| 65 | def load(self): |
| 66 | raise Exception("Not implemented") |
| 67 | # extract text from PDF, populate self.content |
| 68 | self.content = None |
| 69 | |
| 70 | class TEXT(AbstractSource): |
| 71 | """ |
| 72 | TEXT resource |
| 73 | """ |
| 74 | |
| 75 | def __init__(self, source): |
| 76 | super().__init__(source) |
| 77 | |
| 78 | def load(self): |
| 79 | if self.content is None: |
| 80 | # populate self.content |
| 81 | with open(self.source, 'r') as f: |
| 82 | self.content = f.read() |
| 83 | |
| 84 | return self.content |
| 85 | |
| 86 | class HTML(AbstractSource): |
| 87 | """ |
| 88 | HTML resource |
| 89 | """ |
| 90 | |
| 91 | def __init__(self, source): |
| 92 | super().__init__(source) |
| 93 | |
| 94 | def _download(self): |
| 95 | try: |
| 96 | response = requests.get(self.source) |
| 97 | response.raise_for_status() # Raise an HTTPError for bad responses (4xx and 5xx) |
| 98 | self.content = response.text |
| 99 | except requests.exceptions.RequestException as e: |
| 100 | print(f"An error occurred: {e}") |
| 101 | |
| 102 | def load(self): |
| 103 | # Download URL |
| 104 | self._download() |
| 105 | |
| 106 | # extract text from HTML, populate self.content |
| 107 | soup = BeautifulSoup(self.content, 'html.parser') |
| 108 | |
| 109 | # Extract text from the HTML |
| 110 | self.content = soup.get_text() |
| 111 | |
| 112 | # Remove extra newlines |
| 113 | self.content = re.sub(r'\n{2,}', '\n', self.content) |
| 114 | |
| 115 | return f"{self.source}\n{self.content}" |