From b686487bf1922151d14e9261ed51e1da9d2b0041 Mon Sep 17 00:00:00 2001 From: Jonas Braus Date: Tue, 10 Dec 2024 23:13:59 +0100 Subject: [PATCH] puuuuush --- lib.py | 19 ++++++++++++++++++ main2.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 main2.py diff --git a/lib.py b/lib.py index 42b46e8..489d195 100644 --- a/lib.py +++ b/lib.py @@ -1,7 +1,9 @@ import os, pymupdf, whisper, json import cv2 +import psycopg2 import pytesseract +import ollama def read_files(path, output, filetypes=None): @@ -40,3 +42,20 @@ def extract_image_content(path): pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' img = cv2.imread(path) return pytesseract.image_to_string(img) + +def prompt_embedding(prompt): + embedding = ollama.embeddings(model="mxbai-embed-large", prompt=prompt) + + conn = psycopg2.connect( + dbname="embeddings", + user="python", + password="PasswordPassword123", + host="localhost", + port="5555" + ) + cur = conn.cursor() + + cur.execute(f"select id, filepath, embedding, embedding <-> {embedding} as distance from dbtable order by distance limit 1;") + + result = cur.fetchall() + print(result) \ No newline at end of file diff --git a/main2.py b/main2.py new file mode 100644 index 0000000..60f948e --- /dev/null +++ b/main2.py @@ -0,0 +1,60 @@ +import pymupdf +import os +import lib +import json + + +def retrieve_file_contents(path): + file_extraction_functions = { + "pdf": lambda path: lib.extract_pdf_content(path), + "jpg": lambda path: lib.extract_image_content(path), + "png": lambda path: lib.extract_image_content(path), + "txt": lambda path: lib.extract_pdf_content(path), + "mp3": lambda path: lib.extract_mp3_content(path), + } + + lib.read_files(path, files := []) + + contents = [] + + for file in files: + content = file_extraction_functions[file[0]](file[1]) + contents.append({ + "type": file[0], + "path": file[1], + "filename": file[2], + "content": content + }) + + return contents + + +def reload_files(): + path = input("Please provider path to folder: ") + pass + + +def add_files(): + path = input("Please provider path to folder: ") + pass + + +def prompt_cycle(): + while True: + prompt = input("Please enter prompt: ") + lib.prompt_embedding(prompt) + + +def get_user_action(): + user_action = input("Reload Files (r), Add Files (a), Prompt (p): ") + + { + "r": lambda: reload_files(), + "a": lambda: add_files(), + "p": lambda: prompt_cycle() + }[user_action]() + +# contents = retrieve_file_contents("C:\\SoftwareEng") +# print(json.dumps(contents, indent="\t")) + +get_user_action() \ No newline at end of file