import asyncio
import tkinter as tk
from tkinter import scrolledtext
from g4f.client import Client
client = Client()
def get_response(prompt):
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": prompt
}]
)
return response.choices[0].message.content
except Exception as e:
return f"Произошла ошибка: {str(e)}"
def send_message():
user_input = input_box.get("1.0", tk.END).strip()
if user_input.lower() == 'exit':
root.destroy()
return
chat_history.insert(tk.END, "Вы: " + user_input + "\n")
response = get_response(user_input)
chat_history.insert(tk.END, "Модель: " + response + "\n\n")
input_box.delete("1.0", tk.END)
chat_history.yview(tk.END)
# Настройка основного окна
root = tk.Tk()
root.title("Диалоговая система с GPT-4")
chat_history = scrolledtext.ScrolledText(root, wrap=tk.WORD, state='normal', width=80, height=20, font=("Helvetica", 12))
chat_history.pack(padx=10, pady=10)
input_frame = tk.Frame(root)
input_frame.pack(pady=5)
input_box = tk.Text(input_frame, height=3, width=60, font=("Helvetica", 12))
input_box.pack(side=tk.LEFT, padx=5)
send_button = tk.Button(input_frame, text="Отправить", command=send_message, font=("Helvetica", 12))
send_button.pack(side=tk.RIGHT, padx=5)
root.mainloop()