import tkinter as tk from tkinter import messagebox, ttk import openai import threading import webbrowser import pickle class APIKeyWindow: def __init__(self, root, app): self.root = root self.root.title("Clé API OpenAI") self.app = app self.api_key_label = tk.Label(root, text="Pour commencer, veuillez obtenir une clé API sur le site d'OpenAI :") self.api_key_label.pack(pady=5) self.api_key_link = tk.Label(root, text="https://platform.openai.com/overview", fg="blue", cursor="hand2") self.api_key_link.pack() self.api_key_link.bind("", self.open_openai_link) self.api_key_instructions = tk.Label(root, text="Une fois que vous avez obtenu la clé API, veuillez la copier et la coller ci-dessous :") self.api_key_instructions.pack(pady=5) self.api_key_entry = tk.Entry(root) self.api_key_entry.pack(pady=10) self.continue_button = tk.Button(root, text="Continuer", command=self.continue_to_main) self.continue_button.pack(pady=10) def open_openai_link(self, event): openai_link = "https://platform.openai.com/overview" webbrowser.open_new(openai_link) def continue_to_main(self): api_key = self.api_key_entry.get().strip() if api_key: self.app.set_openai_api_key(api_key) self.root.destroy() self.app.create_main_window() class LolaApp: def __init__(self, root): self.root = root self.root.title("LOLA") self.root.geometry("1920x1080") # Définir la taille de la fenêtre # Fond en vert pâle self.canvas = tk.Canvas(self.root, bg="#d1eed1") self.canvas.pack(fill="both", expand=True) self.api_key = None self.root.after(1000, self.open_api_key_window) def open_api_key_window(self): api_key_root = tk.Toplevel() api_key_app = APIKeyWindow(api_key_root, self) def set_openai_api_key(self, api_key): self.api_key = api_key def create_main_window(self): self.title_label = tk.Label(self.canvas, text="LOLA", font=("Arial", 24), bg="#d1eed1", fg="black") self.title_label.pack(fill=tk.X) self.quit_button = tk.Button(self.canvas, text="Quitter", command=self.root.destroy, bg="red", fg="white") self.quit_button.pack(side=tk.TOP, anchor="ne", padx=10, pady=5) self.conversations_frame = tk.Frame(self.canvas, bg="#d1eed1") self.conversations_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) self.conversation_tabs = ttk.Notebook(self.conversations_frame) self.conversation_tabs.pack(fill=tk.BOTH, expand=True) self.create_new_conversation_button = tk.Button(self.conversations_frame, text="+ Nouvelle Conversation", command=self.create_new_conversation) self.create_new_conversation_button.pack(pady=10, side=tk.TOP) self.search_frame = tk.Frame(self.root, bg="#d1eed1") self.search_frame.pack(side=tk.BOTTOM, fill=tk.X, pady=20) # Ajuster la marge inférieure self.search_bar = tk.Entry(self.search_frame) # Déplacer la barre de saisie ici self.search_bar.pack(fill=tk.X, padx=10, pady=5, side=tk.LEFT, expand=True) # Ajuster la marge inférieure de la barre de saisie self.search_bar.bind("", self.send_message_with_loading) # Permettre d'appuyer sur Entrée pour envoyer self.search_button = tk.Button(self.search_frame, text="Envoyer", command=self.send_message_with_loading) self.search_button.pack(padx=10, pady=5, side=tk.RIGHT) # Placer le bouton Envoyer à droite self.history_button = tk.Button(self.root, text="Historique", command=self.show_history) self.history_button.pack(pady=10, side=tk.BOTTOM) # Placer le bouton Historique en bas self.loading_window = None self.loading_var = tk.DoubleVar() self.history = self.load_conversations_from_file() def create_new_conversation(self): conversation_frame = tk.Frame(self.conversation_tabs) self.conversation_tabs.add(conversation_frame, text=f"Conv {self.conversation_tabs.index('end')}") self.conversation_tabs.select(len(self.conversation_tabs.tabs()) - 1) conversation_text = tk.Text(conversation_frame, height=25, width=100, wrap=tk.WORD, state=tk.DISABLED, bg="white") # Changer la couleur de fond de la zone de réponse de l'IA conversation_text.pack(padx=10, pady=10) delete_button = tk.Button(conversation_frame, text="Supprimer", command=lambda: self.delete_conversation(conversation_frame)) delete_button.pack() def delete_conversation(self, conversation_frame): current_tab = self.conversation_tabs.nametowidget(self.conversation_tabs.select()) if current_tab == conversation_frame: conversation_index = self.conversation_tabs.index(current_tab) self.conversation_tabs.forget(conversation_index) def send_message_with_loading(self, event=None): self.loading_window = tk.Toplevel(self.root) self.loading_window.title("Chargement") loading_label = tk.Label(self.loading_window, text="Envoi de la requête en cours...") loading_label.pack(padx=20, pady=20) self.loading_bar = ttk.Progressbar(self.loading_window, mode='indeterminate', length=200) self.loading_bar.pack(padx=20, pady=20) self.loading_bar.start() threading.Thread(target=self.send_message).start() def send_message(self): user_query = self.search_bar.get() current_tab = self.conversation_tabs.nametowidget(self.conversation_tabs.select()) conversation_text = current_tab.winfo_children()[0] self.history.append(f"Utilisateur: {user_query}") # Appel à l'API OpenAI pour obtenir une réponse response = self.generate_openai_response(user_query) self.history.append(f"IA: {response['choices'][0]['message']['content']}") conversation_text.configure(state="normal") conversation_text.insert(tk.END, f"Vous: {user_query}\n", "blue") conversation_text.insert(tk.END, "-" * 50 + "\n", "blue") conversation_text.insert(tk.END, f"IA: {response['choices'][0]['message']['content']}\n", "red") conversation_text.insert(tk.END, "-" * 50 + "\n", "red") conversation_text.configure(state="disabled") self.loading_bar.stop() self.loading_window.destroy() self.save_conversations_to_file() # Save conversations after each message def generate_openai_response(self, user_input): openai.api_key = self.api_key response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": user_input} ] ) return response def show_history(self): history_text = "\n".join(self.history) history_window = tk.Toplevel(self.root) history_window.title("Historique") history_text_widget = tk.Text(history_window, height=20, width=60) history_text_widget.insert(tk.END, history_text) history_text_widget.pack(padx=10, pady=10) def save_conversations_to_file(self): with open("conversations.pkl", "wb") as f: pickle.dump(self.history, f) def load_conversations_from_file(self): try: with open("conversations.pkl", "rb") as f: conversations = pickle.load(f) return conversations except FileNotFoundError: return [] if __name__ == "__main__": root = tk.Tk() app = LolaApp(root) root.mainloop()