88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
import os
|
|
from openai import OpenAI
|
|
from tkinter import simpledialog, Tk, Label, Entry, Button, Text, filedialog
|
|
from dotenv import load_dotenv
|
|
|
|
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):
|
|
client = OpenAI(
|
|
api_key=os.environ.get("OPENAI_API_KEY")
|
|
)
|
|
chat_completion = client.chat.completions.create(
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": f"Vergleiche den folgenden Text mit der Suchanfrage '{search_query}':\n\n{content[:1000]}...\n\nIst der Text relevant für die Suchanfrage?"},
|
|
],
|
|
model="gpt-4o-mini",
|
|
)
|
|
return "ja" in chat_completion.choices[0].message.content.lower()
|
|
def search_files():
|
|
search_query = query_entry.get() # Get the search query from the Entry widget
|
|
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() |