From ba9256034c9c521c5d1474d3520a0236f1de1a72 Mon Sep 17 00:00:00 2001 From: Martin Rattensberger Date: Fri, 22 Nov 2024 23:33:08 +0100 Subject: [PATCH] initialer commit --- filesearch.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 filesearch.py diff --git a/filesearch.py b/filesearch.py new file mode 100644 index 0000000..9ea02ce --- /dev/null +++ b/filesearch.py @@ -0,0 +1,88 @@ +import os +import openai +from tkinter import simpledialog, Tk, Label, Entry, Button, Text, filedialog + +# Set your OpenAI API key here +openai.api_key = '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' + +def get_user_input(): + root = None # No parent window for simplicity + search_query = simpledialog.askstring("Input", "Enter search query:", parent=root) + return search_query + +def list_files(directory): + files = [] + for root, dirs, filenames in os.walk(directory): + if not any(d.startswith('.') for d in dirs): + for filename in filenames: + file_path = os.path.join(root, filename) + if filename.endswith(('.docx', '.pdf')): + files.append(file_path) + return files + +def compare_with_openai(content, search_query): + response = openai.Completion.create( + engine="text-davinci-003", + prompt=f"Vergleiche den folgenden Text mit der Suchanfrage '{search_query}':\n\n{content[:1000]}...\n\nIst der Text relevant für die Suchanfrage?", + max_tokens=50, + n=1, + stop=None, + temperature=0.5, + ) + return "ja" in response.choices[0].text.lower() + +def search_files(): + search_query = get_user_input() + if not search_query: + print("No search query provided.") + return + + directory = filedialog.askdirectory() # Auswahldialog für das Verzeichnis + if not directory: + print("No directory selected.") + return + + if not os.path.exists(directory): + print("Directory does not exist.") + return + + files = list_files(directory) + found_files = [] + + for file_path in files: + try: + with open(file_path, 'r', encoding='utf-8', errors='ignore') as file: + file_content = file.read() + if compare_with_openai(file_content, search_query): + found_files.append(file_path) + except Exception as e: + print(f"Error reading {file_path}: {e}") + + result_text.delete(1.0, 'end') + if found_files: + result_text.insert('end', "Files containing the search query:\n") + for file in found_files: + result_text.insert('end', file + '\n') + else: + result_text.insert('end', "No files containing the search query found.") + +# GUI erstellen +root = Tk() +root.title("Dateisuche mit OpenAI") +root.geometry("600x400") + +# Eingabefeld für die Suchanfrage +query_label = Label(root, text="Suchanfrage:") +query_label.pack(pady=5) +query_entry = Entry(root, width=50) +query_entry.pack(pady=5) + +# Suchbutton +search_button = Button(root, text="Suchen", command=search_files) +search_button.pack(pady=10) + +# Ergebnisanzeige +result_text = Text(root, height=15, width=70) +result_text.pack(pady=10) + +root.mainloop() \ No newline at end of file