Spaces:
Runtime error
Runtime error
Create GUI.py
Browse files
GUI.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import json
|
3 |
+
import logging
|
4 |
+
import os
|
5 |
+
import tkinter as tk
|
6 |
+
from tkinter import messagebox
|
7 |
+
from threading import Thread
|
8 |
+
from typing import Dict, Any
|
9 |
+
|
10 |
+
class AIApp(tk.Tk):
|
11 |
+
def __init__(self, ai_core):
|
12 |
+
super().__init__()
|
13 |
+
self.ai_core = ai_core
|
14 |
+
self.title("AI System Interface")
|
15 |
+
self.geometry("800x600")
|
16 |
+
self._running = True
|
17 |
+
|
18 |
+
self.create_widgets()
|
19 |
+
self._start_health_monitoring()
|
20 |
+
|
21 |
+
def create_widgets(self):
|
22 |
+
self.query_label = tk.Label(self, text="Enter your query:")
|
23 |
+
self.query_label.pack(pady=10)
|
24 |
+
|
25 |
+
self.query_entry = tk.Entry(self, width=100)
|
26 |
+
self.query_entry.pack(pady=10)
|
27 |
+
|
28 |
+
self.submit_button = tk.Button(self, text="Submit", command=self.submit_query)
|
29 |
+
self.submit_button.pack(pady=10)
|
30 |
+
|
31 |
+
self.response_area = tk.Text(self, height=20, width=100)
|
32 |
+
self.response_area.pack(pady=10)
|
33 |
+
|
34 |
+
self.status_bar = tk.Label(self, text="Ready", bd=1, relief=tk.SUNKEN, anchor=tk.W)
|
35 |
+
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
|
36 |
+
|
37 |
+
def submit_query(self):
|
38 |
+
query = self.query_entry.get()
|
39 |
+
self.status_bar.config(text="Processing...")
|
40 |
+
Thread(target=self._run_async_task, args=(self.ai_core.generate_response(query),)).start()
|
41 |
+
|
42 |
+
def _run_async_task(self, coroutine):
|
43 |
+
"""Run async task in a separate thread"""
|
44 |
+
loop = asyncio.new_event_loop()
|
45 |
+
asyncio.set_event_loop(loop)
|
46 |
+
try:
|
47 |
+
result = loop.run_until_complete(coroutine)
|
48 |
+
self.after(0, self._display_result, result)
|
49 |
+
except Exception as e:
|
50 |
+
self.after(0, self._show_error, str(e))
|
51 |
+
finally:
|
52 |
+
loop.close()
|
53 |
+
|
54 |
+
def _display_result(self, result: Dict):
|
55 |
+
"""Display results in the GUI"""
|
56 |
+
self.response_area.insert(tk.END, json.dumps(result, indent=2) + "\n\n")
|
57 |
+
self.status_bar.config(text="Query processed successfully")
|
58 |
+
|
59 |
+
def _show_error(self, message: str):
|
60 |
+
"""Display error messages to the user"""
|
61 |
+
messagebox.showerror("Error", message)
|
62 |
+
self.status_bar.config(text=f"Error: {message}")
|
63 |
+
|
64 |
+
def _start_health_monitoring(self):
|
65 |
+
"""Periodically check system health"""
|
66 |
+
def update_health():
|
67 |
+
if self._running:
|
68 |
+
health = asyncio.run(self.ai_core.self_healing.check_health())
|
69 |
+
self.status_bar.config(
|
70 |
+
text=f"System Health - Memory: {health['memory_usage']}% | "
|
71 |
+
f"CPU: {health['cpu_load']}% | Response Time: {health['response_time']:.2f}s"
|
72 |
+
)
|
73 |
+
self.after(5000, update_health)
|
74 |
+
update_health()
|
75 |
+
|
76 |
+
async def main():
|
77 |
+
"""The main function initializes the AI system, handles user input in a loop,
|
78 |
+
generates responses using the AI system, and prints the insights, security level,
|
79 |
+
AI response, and safety analysis. It also ensures proper shutdown of the AI system
|
80 |
+
and its resources."""
|
81 |
+
print("🧠 Hybrid AI System Initializing (Local Models)")
|
82 |
+
ai = AICore()
|
83 |
+
app = AIApp(ai)
|
84 |
+
app.mainloop()
|
85 |
+
await ai.shutdown()
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
asyncio.run(main())
|