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