|
import os, shutil, json, random, inspect |
|
import tkinter as tk |
|
import openai |
|
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader |
|
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter |
|
|
|
class DocProcessing: |
|
@classmethod |
|
def load_directory(self, file_path): |
|
loader = DirectoryLoader(file_path) |
|
documents = loader.load() |
|
return documents |
|
|
|
|
|
@classmethod |
|
def split_docs(self, documents,chunk_size=1000,chunk_overlap=20): |
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) |
|
docs = text_splitter.split_documents(documents) |
|
return docs |
|
|
|
@classmethod |
|
def embeddings_chroma(): |
|
pass |
|
|
|
@classmethod |
|
def get_random_line(self): |
|
with open(self.file_path, "r", encoding="utf-8") as file: |
|
lines = file.readlines() |
|
return random.choice(lines).strip() |
|
|
|
|
|
class ObjPrinter: |
|
@staticmethod |
|
def print_obj(obj): |
|
|
|
|
|
|
|
print("Object:", obj) |
|
print("Type:", type(obj)) |
|
print("Class Hierarchy:") |
|
for cls in type(obj).__mro__: |
|
print(f" - {cls.__name__} ({cls.__module__})") |
|
|
|
print("\nInstance Variables:") |
|
for key, value in vars(obj).items(): |
|
print(f" - {key}: {value}") |
|
|
|
class ShowJson: |
|
@staticmethod |
|
def show_json(obj): |
|
print(json.loads(obj.model_dump_json())) |
|
|
|
class KeyGood: |
|
@staticmethod |
|
|
|
def check_key(apikey="None"): |
|
print(apikey) |
|
windows_key = os.environ.get("OPENAI_API_KEY") |
|
if windows_key is not None: |
|
try: |
|
client=OpenAI() |
|
client.models.list() |
|
return client |
|
except Exception as e: |
|
client = OpenAI(api_key = apikey) |
|
try: |
|
client.models.list() |
|
print('good') |
|
return client |
|
except Exception as e: |
|
return None |
|
else: |
|
client = OpenAI(api_key = apikey) |
|
try: |
|
client.models.list() |
|
print('good') |
|
return client |
|
except Exception as e: |
|
return None |
|
|
|
class CheckKeyInComputer: |
|
@staticmethod |
|
def check_key_in_computer(): |
|
windows_key = os.environ.get("OPENAI_API_KEY") |
|
if windows_key is None: |
|
return False |
|
print(f'key in env is good: {client.api_key[:9]}') |
|
return True |
|
|
|
|
|
|
|
class CopyResume: |
|
@staticmethod |
|
def copy_and_rename_file(): |
|
data_path = Path_.DATA_PATH |
|
myfile_ = data_path / "MyFile.pdf" |
|
|
|
if myfile_.exists(): |
|
print(f"{myfile_} existed.") |
|
return "existed" |
|
else: |
|
|
|
original_file_path = filedialog.askopenfilename(title="Locate and click your resume: ") |
|
|
|
if original_file_path: |
|
try: |
|
|
|
if not data_path: |
|
os.makedirs(data_path) |
|
|
|
|
|
shutil.copy2(original_file_path, myfile_) |
|
print(f"File copied to {myfile_}") |
|
return "succeed" |
|
except Exception as e: |
|
print(f"Error copying file: {e}") |
|
return "failed" |
|
|
|
class HyperlinkManager: |
|
|
|
def __init__(self, text): |
|
|
|
self.text = text |
|
|
|
self.text.tag_config("hyper", foreground="#5E95FF", underline=1) |
|
|
|
self.text.tag_bind("hyper", "<Enter>", self._enter) |
|
self.text.tag_bind("hyper", "<Leave>", self._leave) |
|
self.text.tag_bind("hyper", "<Button-1>", self._click) |
|
|
|
self.reset() |
|
|
|
def reset(self): |
|
self.links = {} |
|
|
|
def add(self, action): |
|
|
|
|
|
tag = "hyper-%d" % len(self.links) |
|
self.links[tag] = action |
|
return "hyper", tag |
|
|
|
def _enter(self, event): |
|
self.text.config(cursor="hand2") |
|
|
|
def _leave(self, event): |
|
self.text.config(cursor="") |
|
|
|
def _click(self, event): |
|
for tag in self.text.tag_names(CURRENT): |
|
if tag[:6] == "hyper-": |
|
self.links[tag]() |
|
return |
|
|
|
|
|
class ExceptionHandler: |
|
@staticmethod |
|
def handle_exception(exception, additional_info=None): |
|
frame = inspect.currentframe() |
|
module_name = inspect.getmodule(frame).__name__ |
|
function_name = frame.f_code.co_name |
|
line_number = frame.f_lineno |
|
globals_dict = frame.f_globals |
|
|
|
|
|
print(f"Exception occurred:") |
|
if additional_info: |
|
print(f" - Additional Info: {additional_info}") |
|
print(f" - Module: {module_name}") |
|
print(f" - Function: {function_name}") |
|
print(f" - Line: {line_number}") |
|
|
|
print(f" - Exception Type: {type(exception).__name__}") |
|
print(f" - Exception Details: {exception}") |
|
|
|
class ChangeText: |
|
@staticmethod |
|
def bold_text(text_widget, keyword): |
|
text_widget.tag_configure("bold", font=("Montserrat", 14 * -1, "bold")) |
|
|
|
start_index = "1.0" |
|
while True: |
|
start_index = text_widget.search(keyword, start_index, stopindex=tk.END) |
|
if not start_index: break |
|
|
|
|
|
end_index = f"{start_index}+{len(keyword)}c" |
|
|
|
|
|
text_widget.tag_add("bold", start_index, end_index) |
|
|
|
|
|
start_index = end_index |
|
|
|
|
|
@staticmethod |
|
def color_line(text_widget, line_number, color): |
|
|
|
text_widget.tag_configure(color, foreground=color) |
|
|
|
|
|
start_index = f"{line_number}.0" |
|
end_index = f"{line_number + 1}.0" |
|
|
|
|
|
text_widget.tag_add(color, start_index, end_index) |