Subbu1304 commited on
Commit
7702f01
·
verified ·
1 Parent(s): 2e15b10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -43
app.py CHANGED
@@ -1,48 +1,20 @@
1
- import torch
2
- import torchaudio
3
- import numpy as np
4
- import sounddevice as sd
5
- import queue
6
- import time
7
- import requests # To trigger Flask endpoint
8
 
9
- # Flask Server URL
10
- FLASK_URL = "http://127.0.0.1:5000/trigger_wake_word"
11
 
12
- # Load a pre-trained speech recognition model (Torch Hub)
13
- model = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H.get_model()
14
- waveform_queue = queue.Queue()
15
 
16
- # Capture audio stream
17
- def callback(indata, frames, time, status):
18
- if status:
19
- print(status)
20
- waveform_queue.put(indata.copy())
21
 
22
- # Function to process audio and detect wake word
23
- def detect_wake_word():
24
- while True:
25
- waveform = waveform_queue.get()
26
- waveform = torch.tensor(waveform).mean(dim=1) # Convert stereo to mono
27
- waveform = waveform.unsqueeze(0) # Add batch dimension
28
 
29
- # Convert waveform to text
30
- with torch.inference_mode():
31
- transcript = model(waveform)[0]
32
-
33
- transcript = transcript.lower()
34
- print(f"Detected: {transcript}")
35
-
36
- if "hi biriyani hub" in transcript:
37
- print("Wake word detected!")
38
- requests.get(FLASK_URL) # Trigger Flask route
39
- break
40
-
41
- # Start capturing audio in a background thread
42
- def start_listening():
43
- with sd.InputStream(callback=callback, channels=1, samplerate=16000):
44
- detect_wake_word()
45
-
46
- # Run Wake Word Detection
47
- if __name__ == "__main__":
48
- start_listening()
 
1
+ import os
2
+ from flask import Flask, render_template
 
 
 
 
 
3
 
4
+ app = Flask(__name__)
 
5
 
6
+ # Get Hugging Face assigned port, default to 7860 if running locally
7
+ PORT = int(os.getenv("PORT", 7860))
 
8
 
9
+ @app.route('/')
10
+ def index():
11
+ """Serve the main page with voice recognition."""
12
+ return render_template('index.html')
 
13
 
14
+ @app.route('/landing')
15
+ def landing():
16
+ """Landing page that loads after wake-word detection."""
17
+ return render_template('landing.html')
 
 
18
 
19
+ if __name__ == '__main__':
20
+ app.run(host="0.0.0.0", port=PORT, debug=True)