Spaces:
Runtime error
Runtime error
Update GUI.py
Browse files
GUI.py
CHANGED
@@ -1,12 +1,53 @@
|
|
1 |
import asyncio
|
2 |
import json
|
3 |
import logging
|
4 |
-
import
|
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__()
|
@@ -14,23 +55,18 @@ class AIApp(tk.Tk):
|
|
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 |
|
@@ -65,21 +101,20 @@ class AIApp(tk.Tk):
|
|
65 |
"""Periodically check system health"""
|
66 |
def update_health():
|
67 |
if self._running:
|
68 |
-
health = asyncio.run(self.ai_core.
|
69 |
self.status_bar.config(
|
70 |
text=f"System Health - Memory: {health['memory_usage']}% | "
|
71 |
-
|
|
|
72 |
)
|
73 |
-
|
|
|
74 |
update_health()
|
75 |
|
76 |
async def main():
|
77 |
-
"""The main function initializes the AI system
|
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()
|
|
|
1 |
import asyncio
|
2 |
import json
|
3 |
import logging
|
4 |
+
import torch
|
5 |
import tkinter as tk
|
6 |
from tkinter import messagebox
|
7 |
from threading import Thread
|
8 |
from typing import Dict, Any
|
9 |
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO)
|
12 |
+
|
13 |
+
class AICore:
|
14 |
+
def __init__(self):
|
15 |
+
if not torch.cuda.is_available():
|
16 |
+
raise RuntimeError("GPU not available. Ensure CUDA is installed and a compatible GPU is present.")
|
17 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
18 |
+
logging.info(f"Using device: {self.device}")
|
19 |
+
|
20 |
+
async def generate_response(self, query: str) -> Dict[str, Any]:
|
21 |
+
# Simulate AI response generation
|
22 |
+
await asyncio.sleep(1) # Simulate processing time
|
23 |
+
return {
|
24 |
+
"query": query,
|
25 |
+
"response": f"AI response to: {query}",
|
26 |
+
"insights": ["Insight 1", "Insight 2"],
|
27 |
+
"security_level": 2,
|
28 |
+
"safety_analysis": {"toxicity": 0.1, "bias": 0.05, "privacy": []}
|
29 |
+
}
|
30 |
+
|
31 |
+
async def check_health(self) -> Dict[str, Any]:
|
32 |
+
# Simulate health check
|
33 |
+
await asyncio.sleep(1) # Simulate processing time
|
34 |
+
return {
|
35 |
+
"memory_usage": 30, # Example memory usage percentage
|
36 |
+
"cpu_load": 20, # Example CPU load percentage
|
37 |
+
"gpu_memory": self.get_gpu_memory(), # Get GPU memory usage
|
38 |
+
"response_time": 0.5 # Example response time in seconds
|
39 |
+
}
|
40 |
+
|
41 |
+
def get_gpu_memory(self) -> float:
|
42 |
+
if torch.cuda.is_available():
|
43 |
+
return torch.cuda.memory_allocated() / 1e9 # Convert bytes to GB
|
44 |
+
return 0.0
|
45 |
+
|
46 |
+
async def shutdown(self):
|
47 |
+
# Simulate shutdown process
|
48 |
+
await asyncio.sleep(1) # Simulate cleanup time
|
49 |
+
logging.info("AI Core shutdown complete.")
|
50 |
+
|
51 |
class AIApp(tk.Tk):
|
52 |
def __init__(self, ai_core):
|
53 |
super().__init__()
|
|
|
55 |
self.title("AI System Interface")
|
56 |
self.geometry("800x600")
|
57 |
self._running = True
|
|
|
58 |
self.create_widgets()
|
59 |
self._start_health_monitoring()
|
60 |
|
61 |
def create_widgets(self):
|
62 |
self.query_label = tk.Label(self, text="Enter your query:")
|
63 |
self.query_label.pack(pady=10)
|
|
|
64 |
self.query_entry = tk.Entry(self, width=100)
|
65 |
self.query_entry.pack(pady=10)
|
|
|
66 |
self.submit_button = tk.Button(self, text="Submit", command=self.submit_query)
|
67 |
self.submit_button.pack(pady=10)
|
|
|
68 |
self.response_area = tk.Text(self, height=20, width=100)
|
69 |
self.response_area.pack(pady=10)
|
|
|
70 |
self.status_bar = tk.Label(self, text="Ready", bd=1, relief=tk.SUNKEN, anchor=tk.W)
|
71 |
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
|
72 |
|
|
|
101 |
"""Periodically check system health"""
|
102 |
def update_health():
|
103 |
if self._running:
|
104 |
+
health = asyncio.run(self.ai_core.check_health())
|
105 |
self.status_bar.config(
|
106 |
text=f"System Health - Memory: {health['memory_usage']}% | "
|
107 |
+
f"CPU: {health['cpu_load']}% | GPU: {health['gpu_memory']}GB | "
|
108 |
+
f"Response Time: {health['response_time']:.2f}s"
|
109 |
)
|
110 |
+
self.after(5000, update_health)
|
111 |
+
|
112 |
update_health()
|
113 |
|
114 |
async def main():
|
115 |
+
"""The main function initializes the AI system and starts the GUI."""
|
|
|
|
|
|
|
116 |
print("🧠 Hybrid AI System Initializing (Local Models)")
|
117 |
+
ai = AICore() # Initialize the AI core
|
118 |
app = AIApp(ai)
|
119 |
app.mainloop()
|
120 |
await ai.shutdown()
|