Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, RagTokenizer, RagRetriever, RagSequenceForGeneration
|
3 |
+
import paho.mqtt.client as mqtt
|
4 |
+
from gtts import gTTS
|
5 |
+
import os
|
6 |
+
import sqlite3
|
7 |
+
from sklearn.ensemble import IsolationForest
|
8 |
+
|
9 |
+
# Initialize Database
|
10 |
+
conn = sqlite3.connect('preferences.db')
|
11 |
+
cursor = conn.cursor()
|
12 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS preferences (id INTEGER PRIMARY KEY, setting TEXT, value TEXT)''')
|
13 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY, command TEXT, response TEXT)''')
|
14 |
+
conn.commit()
|
15 |
+
|
16 |
+
# Anomaly Detection Model
|
17 |
+
anomaly_model = IsolationForest(contamination=0.1)
|
18 |
+
data = []
|
19 |
+
|
20 |
+
# Initialize Models
|
21 |
+
retriever = RagRetriever.from_pretrained("facebook/rag-sequence-base")
|
22 |
+
tokenizer = RagTokenizer.from_pretrained("facebook/rag-sequence-base")
|
23 |
+
model = RagSequenceForGeneration.from_pretrained("facebook/rag-sequence-base")
|
24 |
+
nlp = pipeline("conversational")
|
25 |
+
|
26 |
+
# IoT Device Control
|
27 |
+
def control_device(command):
|
28 |
+
client = mqtt.Client()
|
29 |
+
client.connect("broker.hivemq.com", 1883, 60)
|
30 |
+
if "light" in command and "on" in command:
|
31 |
+
client.publish("home/light", "ON")
|
32 |
+
return "Light turned on."
|
33 |
+
elif "light" in command and "off" in command:
|
34 |
+
client.publish("home/light", "OFF")
|
35 |
+
return "Light turned off."
|
36 |
+
else:
|
37 |
+
return "Command not recognized."
|
38 |
+
|
39 |
+
# Process Command
|
40 |
+
def process_command(command):
|
41 |
+
if "light" in command:
|
42 |
+
return control_device(command)
|
43 |
+
else:
|
44 |
+
inputs = tokenizer(command, return_tensors="pt")
|
45 |
+
retrieved_docs = retriever(command, return_tensors="pt")
|
46 |
+
outputs = model.generate(input_ids=inputs['input_ids'], context_input_ids=retrieved_docs['context_input_ids'])
|
47 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
48 |
+
|
49 |
+
# Log History
|
50 |
+
def log_history(command, response):
|
51 |
+
cursor.execute("INSERT INTO history (command, response) VALUES (?, ?)", (command, response))
|
52 |
+
conn.commit()
|
53 |
+
|
54 |
+
# Anomaly Detection
|
55 |
+
def detect_anomalies(command):
|
56 |
+
global data
|
57 |
+
data.append(len(command))
|
58 |
+
if len(data) > 10:
|
59 |
+
anomaly_model.fit([[x] for x in data])
|
60 |
+
if anomaly_model.predict([[len(command)]])[0] == -1:
|
61 |
+
return True
|
62 |
+
return False
|
63 |
+
|
64 |
+
# Gradio Interface
|
65 |
+
def assistant(command):
|
66 |
+
if detect_anomalies(command):
|
67 |
+
return "Warning: Anomalous behavior detected!", ""
|
68 |
+
response = process_command(command)
|
69 |
+
log_history(command, response)
|
70 |
+
tts = gTTS(text=response, lang='en')
|
71 |
+
tts.save("response.mp3")
|
72 |
+
return response, "response.mp3"
|
73 |
+
|
74 |
+
# Launch App
|
75 |
+
demo = gr.Interface(fn=assistant, inputs="text", outputs=["text", "audio"])
|
76 |
+
demo.launch()
|