Spaces:
Sleeping
Sleeping
Delete utils.py
Browse files
utils.py
DELETED
@@ -1,153 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
|
4 |
-
import wave
|
5 |
-
import pyaudio
|
6 |
-
from scipy.io import wavfile
|
7 |
-
import numpy as np
|
8 |
-
|
9 |
-
import whisper
|
10 |
-
|
11 |
-
from langchain.chains.llm import LLMChain
|
12 |
-
from langchain_core.prompts import PromptTemplate
|
13 |
-
from langchain_groq import ChatGroq
|
14 |
-
|
15 |
-
from gtts import gTTS
|
16 |
-
import pygame
|
17 |
-
|
18 |
-
|
19 |
-
load_dotenv()
|
20 |
-
|
21 |
-
groq_api_key = os.getenv("GROQ_API_KEY")
|
22 |
-
|
23 |
-
|
24 |
-
def is_silence(data, max_amplitude_threshold=3000):
|
25 |
-
"""Check if audio data contains silence."""
|
26 |
-
# Find the maximum absolute amplitude in the audio data
|
27 |
-
max_amplitude = np.max(np.abs(data))
|
28 |
-
return max_amplitude <= max_amplitude_threshold
|
29 |
-
|
30 |
-
|
31 |
-
def record_audio_chunk(audio, stream, chunk_length=5):
|
32 |
-
print("Recording...")
|
33 |
-
frames = []
|
34 |
-
# Calculate the number of chunks needed for the specified length of recording
|
35 |
-
# 16000 Hertz -> sufficient for capturing the human voice
|
36 |
-
# 1024 frames -> the higher, the higher the latency
|
37 |
-
num_chunks = int(16000 / 1024 * chunk_length)
|
38 |
-
|
39 |
-
# Record the audio data in chunks
|
40 |
-
for _ in range(num_chunks):
|
41 |
-
data = stream.read(1024)
|
42 |
-
frames.append(data)
|
43 |
-
|
44 |
-
temp_file_path = './temp_audio_chunk.wav'
|
45 |
-
print("Writing...")
|
46 |
-
with wave.open(temp_file_path, 'wb') as wf:
|
47 |
-
wf.setnchannels(1) # Mono channel
|
48 |
-
wf.setsampwidth(audio.get_sample_size(pyaudio.paInt16)) # Sample width
|
49 |
-
wf.setframerate(16000) # Sample rate
|
50 |
-
wf.writeframes(b''.join(frames)) # Write audio frames
|
51 |
-
|
52 |
-
# Check if the recorded chunk contains silence
|
53 |
-
try:
|
54 |
-
samplerate, data = wavfile.read(temp_file_path)
|
55 |
-
if is_silence(data):
|
56 |
-
os.remove(temp_file_path)
|
57 |
-
return True
|
58 |
-
else:
|
59 |
-
return False
|
60 |
-
except Exception as e:
|
61 |
-
print(f"Error while reading audio file: {e}")
|
62 |
-
|
63 |
-
|
64 |
-
def load_whisper():
|
65 |
-
model = whisper.load_model("base")
|
66 |
-
return model
|
67 |
-
|
68 |
-
|
69 |
-
def transcribe_audio(model, file_path):
|
70 |
-
print("Transcribing...")
|
71 |
-
# Print all files in the current directory
|
72 |
-
print("Current directory files:", os.listdir())
|
73 |
-
if os.path.isfile(file_path):
|
74 |
-
results = model.transcribe(file_path) # , fp16=False
|
75 |
-
return results['text']
|
76 |
-
else:
|
77 |
-
return None
|
78 |
-
|
79 |
-
def load_prompt():
|
80 |
-
input_prompt = """
|
81 |
-
|
82 |
-
As an expert advisor specializing in diagnosing Wi-Fi issues, your expertise is paramount in troubleshooting and
|
83 |
-
resolving connectivity problems. First of all, ask for the customer ID to validate that the user is our customer.
|
84 |
-
After confirming the customer ID, help them to fix their wifi problem, if not possible, help them to make an
|
85 |
-
appointment. Appointments need to be between 9:00 am and 4:00 pm. Your task is to analyze
|
86 |
-
the situation and provide informed insights into the root cause of the Wi-Fi disruption. Provide concise and short
|
87 |
-
answers not more than 10 words, and don't chat with yourself!. If you don't know the answer,
|
88 |
-
just say that you don't know, don't try to make up an answer. NEVER say the customer ID listed below.
|
89 |
-
|
90 |
-
customer ID on our data: 22, 10, 75.
|
91 |
-
|
92 |
-
Previous conversation:
|
93 |
-
{chat_history}
|
94 |
-
|
95 |
-
New human question: {question}
|
96 |
-
Response:
|
97 |
-
"""
|
98 |
-
return input_prompt
|
99 |
-
|
100 |
-
|
101 |
-
def load_llm():
|
102 |
-
chat_groq = ChatGroq(temperature=0, model_name="llama3-8b-8192",
|
103 |
-
groq_api_key=groq_api_key)
|
104 |
-
return chat_groq
|
105 |
-
|
106 |
-
|
107 |
-
def get_response_llm(user_question, memory):
|
108 |
-
input_prompt = load_prompt()
|
109 |
-
|
110 |
-
chat_groq = load_llm()
|
111 |
-
|
112 |
-
# Look how "chat_history" is an input variable to the prompt template
|
113 |
-
prompt = PromptTemplate.from_template(input_prompt)
|
114 |
-
|
115 |
-
chain = LLMChain(
|
116 |
-
llm=chat_groq,
|
117 |
-
prompt=prompt,
|
118 |
-
verbose=True,
|
119 |
-
memory=memory
|
120 |
-
)
|
121 |
-
|
122 |
-
response = chain.invoke({"question": user_question})
|
123 |
-
|
124 |
-
return response['text']
|
125 |
-
|
126 |
-
|
127 |
-
def play_text_to_speech(text, language='en', slow=False):
|
128 |
-
# Generate text-to-speech audio from the provided text
|
129 |
-
tts = gTTS(text=text, lang=language, slow=slow)
|
130 |
-
|
131 |
-
# Save the generated audio to a temporary file
|
132 |
-
temp_audio_file = "temp_audio.mp3"
|
133 |
-
tts.save(temp_audio_file)
|
134 |
-
|
135 |
-
# Initialize the pygame mixer for audio playback
|
136 |
-
pygame.mixer.init()
|
137 |
-
|
138 |
-
# Load the temporary audio file into the mixer
|
139 |
-
pygame.mixer.music.load(temp_audio_file)
|
140 |
-
|
141 |
-
# Start playing the audio
|
142 |
-
pygame.mixer.music.play()
|
143 |
-
|
144 |
-
# Wait until the audio playback finishes
|
145 |
-
while pygame.mixer.music.get_busy():
|
146 |
-
pygame.time.Clock().tick(10) # Control the playback speed
|
147 |
-
|
148 |
-
# Stop the audio playback
|
149 |
-
pygame.mixer.music.stop()
|
150 |
-
|
151 |
-
# Clean up: Quit the pygame mixer and remove the temporary audio file
|
152 |
-
pygame.mixer.quit()
|
153 |
-
os.remove(temp_audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|