Shakir60 commited on
Commit
d053647
·
verified ·
1 Parent(s): 84b6073

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -23
app.py CHANGED
@@ -5,8 +5,6 @@ from gtts import gTTS
5
  import os
6
  import sqlite3
7
  from sklearn.ensemble import IsolationForest
8
- import sounddevice as sd
9
- import numpy as np
10
  import speech_recognition as sr
11
 
12
  # Initialize Database
@@ -16,23 +14,6 @@ cursor.execute('''CREATE TABLE IF NOT EXISTS preferences (id INTEGER PRIMARY KEY
16
  cursor.execute('''CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY, command TEXT, response TEXT)''')
17
  conn.commit()
18
 
19
- def record_audio():
20
- samplerate = 16000
21
- duration = 5 # seconds
22
- recording = sd.rec(int(samplerate * duration), samplerate=samplerate, channels=1, dtype='float64')
23
- sd.wait() # Wait until recording is finished
24
- return np.squeeze(recording)
25
-
26
- def recognize_audio():
27
- r = sr.Recognizer()
28
- audio = record_audio()
29
- with sr.AudioData(audio.tobytes(), 16000, 2) as source:
30
- try:
31
- command = r.recognize_google(source)
32
- return command
33
- except sr.UnknownValueError:
34
- return "Sorry, I could not understand the audio."
35
-
36
  # Anomaly Detection Model
37
  anomaly_model = IsolationForest(contamination=0.1)
38
  data = []
@@ -81,16 +62,42 @@ def detect_anomalies(command):
81
  return True
82
  return False
83
 
84
- # Gradio Interface
85
- def assistant(command):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  if detect_anomalies(command):
87
  return "Warning: Anomalous behavior detected!", ""
 
 
88
  response = process_command(command)
89
  log_history(command, response)
 
 
90
  tts = gTTS(text=response, lang='en')
91
  tts.save("response.mp3")
92
  return response, "response.mp3"
93
 
94
- # Launch App
95
- demo = gr.Interface(fn=assistant, inputs="text", outputs=["text", "audio"])
 
 
 
 
 
 
 
 
96
  demo.launch()
 
5
  import os
6
  import sqlite3
7
  from sklearn.ensemble import IsolationForest
 
 
8
  import speech_recognition as sr
9
 
10
  # Initialize Database
 
14
  cursor.execute('''CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY, command TEXT, response TEXT)''')
15
  conn.commit()
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Anomaly Detection Model
18
  anomaly_model = IsolationForest(contamination=0.1)
19
  data = []
 
62
  return True
63
  return False
64
 
65
+ # Gradio Interface for Audio and Text Input
66
+ def assistant(input_audio=None, input_text=""):
67
+ if input_audio:
68
+ # Process audio input
69
+ recognizer = sr.Recognizer()
70
+ with sr.AudioFile(input_audio) as source:
71
+ audio = recognizer.record(source)
72
+ try:
73
+ command = recognizer.recognize_google(audio)
74
+ except sr.UnknownValueError:
75
+ return "Sorry, I could not understand the audio.", ""
76
+ else:
77
+ # Process text input
78
+ command = input_text
79
+
80
+ # Check anomalies
81
  if detect_anomalies(command):
82
  return "Warning: Anomalous behavior detected!", ""
83
+
84
+ # Process command
85
  response = process_command(command)
86
  log_history(command, response)
87
+
88
+ # Text-to-Speech Output
89
  tts = gTTS(text=response, lang='en')
90
  tts.save("response.mp3")
91
  return response, "response.mp3"
92
 
93
+ # Gradio Interface with Audio and Text Input Options
94
+ demo = gr.Interface(
95
+ fn=assistant,
96
+ inputs=[
97
+ gr.Audio(source="microphone", type="filepath", optional=True), # Audio input
98
+ gr.Textbox(label="Text Command", placeholder="Type a command...") # Text input
99
+ ],
100
+ outputs=["text", "audio"]
101
+ )
102
+
103
  demo.launch()