Version mit angepasster Suche
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import os
|
||||
from openai import OpenAI
|
||||
import threading
|
||||
import itertools
|
||||
from tkinter import Tk, Label, Entry, Button, Text, filedialog
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from openai import OpenAI
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -15,25 +17,22 @@ def list_files(directory):
|
||||
if filename.endswith(('.docx', '.pdf')):
|
||||
file_path = os.path.join(root, filename)
|
||||
files.append(file_path)
|
||||
return files
|
||||
|
||||
return files # Hier wurde die Einrückung korrigiert
|
||||
def compare_with_openai(content, search_query):
|
||||
client = OpenAI(
|
||||
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
|
||||
)
|
||||
try:
|
||||
completion = client.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
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? Antworte mit 'Ja' oder 'Nein'."
|
||||
},
|
||||
"content": f"Vergleiche den folgenden Text mit der Suchanfrage '{search_query}':\n\n{content[:1000]}...\n\nIst der Text relevant für die Suchanfrage? Antworte mit 'Ja' oder 'Nein'.",
|
||||
}
|
||||
],
|
||||
max_tokens=5,
|
||||
temperature=0.0,
|
||||
model="gpt-4o-mini",
|
||||
)
|
||||
answer = completion.choices[0].message.content.strip().lower()
|
||||
answer = chat_completion.choices[0].message.content.strip().lower()
|
||||
return "ja" in answer
|
||||
except Exception as e:
|
||||
print(f"Fehler bei der OpenAI-API-Anfrage: {e}")
|
||||
@@ -64,23 +63,27 @@ def read_file_content(file_path):
|
||||
return text
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Lesen der PDF-Datei {file_path}: {e}")
|
||||
return ''
|
||||
return ''
|
||||
else:
|
||||
return ''
|
||||
|
||||
def search_files():
|
||||
def search_files_threaded():
|
||||
search_query = query_entry.get()
|
||||
directory = directory_entry.get()
|
||||
|
||||
if not search_query:
|
||||
print("Keine Suchanfrage eingegeben.")
|
||||
root.after(0, stop_animation)
|
||||
return
|
||||
|
||||
directory = filedialog.askdirectory()
|
||||
if not directory:
|
||||
print("Kein Verzeichnis ausgewählt.")
|
||||
print("Kein Verzeichnis eingegeben.")
|
||||
root.after(0, stop_animation)
|
||||
return
|
||||
|
||||
if not os.path.exists(directory):
|
||||
print("Verzeichnis existiert nicht.")
|
||||
root.after(0, stop_animation)
|
||||
return
|
||||
|
||||
files = list_files(directory)
|
||||
@@ -92,6 +95,15 @@ def search_files():
|
||||
if compare_with_openai(file_content, search_query):
|
||||
found_files.append(file_path)
|
||||
|
||||
# Nach Abschluss der Suche GUI aktualisieren
|
||||
root.after(0, update_result_text, found_files)
|
||||
|
||||
def update_result_text(found_files):
|
||||
# Animation stoppen
|
||||
global searching
|
||||
searching = False
|
||||
animation_label.config(text="")
|
||||
|
||||
result_text.delete(1.0, 'end')
|
||||
if found_files:
|
||||
result_text.insert('end', "Dateien relevant für die Suchanfrage:\n")
|
||||
@@ -100,10 +112,38 @@ def search_files():
|
||||
else:
|
||||
result_text.insert('end', "Keine relevanten Dateien gefunden.")
|
||||
|
||||
def start_search():
|
||||
global searching
|
||||
if searching:
|
||||
print("Die Suche läuft bereits.")
|
||||
return
|
||||
searching = True
|
||||
result_text.delete(1.0, 'end') # Ausgabefenster leeren
|
||||
animate() # Animation starten
|
||||
threading.Thread(target=search_files_threaded).start()
|
||||
|
||||
def animate():
|
||||
if not searching:
|
||||
return
|
||||
next_frame = next(animation_frames)
|
||||
animation_label.config(text=f"Suche läuft... {next_frame}")
|
||||
root.after(200, animate)
|
||||
|
||||
def stop_animation():
|
||||
global searching
|
||||
searching = False
|
||||
animation_label.config(text="")
|
||||
|
||||
def select_directory():
|
||||
directory = filedialog.askdirectory()
|
||||
if directory:
|
||||
directory_entry.delete(0, 'end')
|
||||
directory_entry.insert(0, directory)
|
||||
|
||||
# GUI erstellen
|
||||
root = Tk()
|
||||
root.title("Dateisuche mit OpenAI")
|
||||
root.geometry("600x400")
|
||||
root.geometry("600x500")
|
||||
|
||||
# Eingabefeld für die Suchanfrage
|
||||
query_label = Label(root, text="Suchanfrage:")
|
||||
@@ -111,12 +151,30 @@ query_label.pack(pady=5)
|
||||
query_entry = Entry(root, width=50)
|
||||
query_entry.pack(pady=5)
|
||||
|
||||
# Eingabefeld für das Verzeichnis
|
||||
directory_label = Label(root, text="Verzeichnis:")
|
||||
directory_label.pack(pady=5)
|
||||
directory_entry = Entry(root, width=50)
|
||||
directory_entry.pack(pady=5)
|
||||
|
||||
# Button zum Auswählen des Verzeichnisses
|
||||
select_button = Button(root, text="Durchsuchen...", command=select_directory)
|
||||
select_button.pack(pady=5)
|
||||
|
||||
# Suchbutton
|
||||
search_button = Button(root, text="Suchen", command=search_files)
|
||||
search_button = Button(root, text="Suchen", command=start_search)
|
||||
search_button.pack(pady=10)
|
||||
|
||||
# Animation Label
|
||||
animation_label = Label(root, text="")
|
||||
animation_label.pack(pady=5)
|
||||
|
||||
# Ergebnisanzeige
|
||||
result_text = Text(root, height=15, width=70)
|
||||
result_text.pack(pady=10)
|
||||
|
||||
# Animation initialisieren
|
||||
animation_frames = itertools.cycle(['|', '/', '-', '\\'])
|
||||
searching = False
|
||||
|
||||
root.mainloop()
|
||||
Reference in New Issue
Block a user