-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (52 loc) · 1.6 KB
/
app.py
File metadata and controls
57 lines (52 loc) · 1.6 KB
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
import speech_recognition as sr
from gtts import gTTS
import playsound
import requests
import json
url = "http://localhost:11434/api/generate"
recognizer = sr.Recognizer()
headers = {
'Content-Type': 'application/json',
}
data = {
"model": "mistral",
"prompt": "",
"stream": False,}
def SpeakText(text_):
speech = gTTS(text=text_, lang="en", slow=False, tld="com.au")
speech.save("prompt.mp3")
playsound.playsound("prompt.mp3")
def AudioToText():
while (1):
try:
with sr.Microphone() as source2:
recognizer.adjust_for_ambient_noise(source2, duration=0.2)
print("I am Listening")
audio2 = recognizer.listen(source2)
MyAudioText = recognizer.recognize_google(audio2)
print(MyAudioText)
return MyAudioText
except sr.RequestError as e:
print(f"Could not request results; {e}")
except sr.UnknownValueError:
print("unkown error occured")
def TextToAi(data_):
print("AI is working its magic")
response = requests.post(url, headers=headers, data=json.dumps(data_))
if response.status_code == 200:
response_text = response.text
data = json.loads(response_text)
actual_response = data["response"]
print(actual_response)
return actual_response
else:
print("Error:", response.status_code, response.text)
while (1):
text = AudioToText()
data = {
"model": "mistral",
"prompt": text,
"stream": False,}
response = TextToAi(data)
SpeakText(response)
print(response)