Version mit angepasster Suche
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import os
|
import os
|
||||||
from openai import OpenAI
|
import threading
|
||||||
|
import itertools
|
||||||
from tkinter import Tk, Label, Entry, Button, Text, filedialog
|
from tkinter import Tk, Label, Entry, Button, Text, filedialog
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@@ -15,25 +17,22 @@ def list_files(directory):
|
|||||||
if filename.endswith(('.docx', '.pdf')):
|
if filename.endswith(('.docx', '.pdf')):
|
||||||
file_path = os.path.join(root, filename)
|
file_path = os.path.join(root, filename)
|
||||||
files.append(file_path)
|
files.append(file_path)
|
||||||
return files
|
return files # Hier wurde die Einrückung korrigiert
|
||||||
|
|
||||||
def compare_with_openai(content, search_query):
|
def compare_with_openai(content, search_query):
|
||||||
client = OpenAI(
|
client = OpenAI(
|
||||||
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
|
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
completion = client.chat.completions.create(
|
chat_completion = client.chat.completions.create(
|
||||||
model="gpt-4o-mini",
|
|
||||||
messages=[
|
messages=[
|
||||||
{
|
{
|
||||||
"role": "user",
|
"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,
|
model="gpt-4o-mini",
|
||||||
temperature=0.0,
|
|
||||||
)
|
)
|
||||||
answer = completion.choices[0].message.content.strip().lower()
|
answer = chat_completion.choices[0].message.content.strip().lower()
|
||||||
return "ja" in answer
|
return "ja" in answer
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Fehler bei der OpenAI-API-Anfrage: {e}")
|
print(f"Fehler bei der OpenAI-API-Anfrage: {e}")
|
||||||
@@ -68,19 +67,23 @@ def read_file_content(file_path):
|
|||||||
else:
|
else:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def search_files():
|
def search_files_threaded():
|
||||||
search_query = query_entry.get()
|
search_query = query_entry.get()
|
||||||
|
directory = directory_entry.get()
|
||||||
|
|
||||||
if not search_query:
|
if not search_query:
|
||||||
print("Keine Suchanfrage eingegeben.")
|
print("Keine Suchanfrage eingegeben.")
|
||||||
|
root.after(0, stop_animation)
|
||||||
return
|
return
|
||||||
|
|
||||||
directory = filedialog.askdirectory()
|
|
||||||
if not directory:
|
if not directory:
|
||||||
print("Kein Verzeichnis ausgewählt.")
|
print("Kein Verzeichnis eingegeben.")
|
||||||
|
root.after(0, stop_animation)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not os.path.exists(directory):
|
if not os.path.exists(directory):
|
||||||
print("Verzeichnis existiert nicht.")
|
print("Verzeichnis existiert nicht.")
|
||||||
|
root.after(0, stop_animation)
|
||||||
return
|
return
|
||||||
|
|
||||||
files = list_files(directory)
|
files = list_files(directory)
|
||||||
@@ -92,6 +95,15 @@ def search_files():
|
|||||||
if compare_with_openai(file_content, search_query):
|
if compare_with_openai(file_content, search_query):
|
||||||
found_files.append(file_path)
|
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')
|
result_text.delete(1.0, 'end')
|
||||||
if found_files:
|
if found_files:
|
||||||
result_text.insert('end', "Dateien relevant für die Suchanfrage:\n")
|
result_text.insert('end', "Dateien relevant für die Suchanfrage:\n")
|
||||||
@@ -100,10 +112,38 @@ def search_files():
|
|||||||
else:
|
else:
|
||||||
result_text.insert('end', "Keine relevanten Dateien gefunden.")
|
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
|
# GUI erstellen
|
||||||
root = Tk()
|
root = Tk()
|
||||||
root.title("Dateisuche mit OpenAI")
|
root.title("Dateisuche mit OpenAI")
|
||||||
root.geometry("600x400")
|
root.geometry("600x500")
|
||||||
|
|
||||||
# Eingabefeld für die Suchanfrage
|
# Eingabefeld für die Suchanfrage
|
||||||
query_label = Label(root, text="Suchanfrage:")
|
query_label = Label(root, text="Suchanfrage:")
|
||||||
@@ -111,12 +151,30 @@ query_label.pack(pady=5)
|
|||||||
query_entry = Entry(root, width=50)
|
query_entry = Entry(root, width=50)
|
||||||
query_entry.pack(pady=5)
|
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
|
# Suchbutton
|
||||||
search_button = Button(root, text="Suchen", command=search_files)
|
search_button = Button(root, text="Suchen", command=start_search)
|
||||||
search_button.pack(pady=10)
|
search_button.pack(pady=10)
|
||||||
|
|
||||||
|
# Animation Label
|
||||||
|
animation_label = Label(root, text="")
|
||||||
|
animation_label.pack(pady=5)
|
||||||
|
|
||||||
# Ergebnisanzeige
|
# Ergebnisanzeige
|
||||||
result_text = Text(root, height=15, width=70)
|
result_text = Text(root, height=15, width=70)
|
||||||
result_text.pack(pady=10)
|
result_text.pack(pady=10)
|
||||||
|
|
||||||
|
# Animation initialisieren
|
||||||
|
animation_frames = itertools.cycle(['|', '/', '-', '\\'])
|
||||||
|
searching = False
|
||||||
|
|
||||||
root.mainloop()
|
root.mainloop()
|
||||||
Reference in New Issue
Block a user