SOS CẦN GẤP: Code Trợ Lý Ảo Deadline 25/11 - Giúp Với Các Bro Ơi!!!

Chào hội anh em developer! Mình đang cần gấp lắm nha, deadline 25/11 phải nộp rồi mà mình còn loay hoay với em code trợ lý ảo này Ai rảnh thì giúp mình với, mình cảm ơn nhiều lắm ạ! (Video có full HD luôn đó nhen)

Code chính - Trợ lý ảo nghe và nói tiếng Việt

Đầu tiên là phần import các thư viện cần thiết nha:

```
import speech_recognition
from gtts import gTTS
import os
import playsound
from datetime import date, datetime
import webbrowser
import python_weather
import asyncio
```

Phần xử lý thời tiết (quan trọng luôn!) ☁️

```
async def getweather():
client = python_weather.Client(format=python_weather.IMPERIAL)
weather = await client.find("Washington DC")
await client.close()

if name == "main":
loop = asyncio.get_event_loop()
loop.run_until_complete(getweather())
```

Vòng lặp chính - Phần "não" của con bot

Giờ đến phần hót nhất nè - vòng lặp while True để bot có thể lắng nghe và phản hồi liên tục:

```
while True:
ai_brain = " "
ai_ear = speech_recognition.Recognizer()
you = ""

with speech_recognition.Microphone() as mic:
print("AI:Đang nghe")
audio = ai_ear.record(mic, duration = 5)
print("AI: ... ")

try:
you = ai_ear.recognize_google(audio, language = 'vi')
if you:
you = you.lower()
else:
you = "Xin chào"
you = you.lower()
print("Bạn: " + you)

except:
ai_brain = "Tôi không hiểu bạn nói gì cả ! ..."
print("AI: " + ai_brain)
tts = gTTS(text=ai_brain, lang='vi', slow=False)
tts.save("ai.mp3")
playsound.playsound("ai.mp3")
os.remove("ai.mp3")
continue
```

Các câu lệnh điều khiển

Phần này là phần xử lý các câu nói của người dùng đó:

```
if "xin chào" in you:
ai_brain = "Xin chào Bạn."

elif "mở youtube" in you:
ai_brain = webbrowser.open('http://youtube.com')

elif "chao xìn" in you:
ai_brain = "nói xin chào đi, đừng nói chao xìn như thế nữa"

elif "thời tiết" in you:
ai_brain = "Bạn xem ở ngoài được mà."

elif "ngày" in you:
today = date.today()
ai_brain = today.strftime("%d/%m/%Y")

elif "mấy giờ rồi" in you:
now = datetime.now()
ai_brain = now.strftime("%H:%M:%S")

elif "Chao xìn" in you:
ai_brain = "nói hẳn hoi tí đi"

elif "nhiệt độ" in you :
ai_brain = print(weather.current.temperature)

elif "hẹn gặp lại" in you:
ai_brain = "Chào tạm biệt và hẹn gặp lại."
print("AI: " + ai_brain)
tts = gTTS(text = ai_brain, lang = 'vi')
tts.save("ai.mp3")
playsound.playsound("ai.mp3")
os.remove("ai.mp3")
exit()

else:
ai_brain = "Tôi không nghe rõ. Bạn nói lại đi !!! "

print("AI: " + ai_brain)
tts = gTTS(text = ai_brain, lang = 'vi' , slow= False)
tts.save("ai.mp3")
playsound.playsound("ai.mp3")
os.remove("ai.mp3")
```

Code riêng cho phần thời tiết

Còn đây là đoạn code xử lý riêng cho chức năng thời tiết nè:

```
import python_weather
import asyncio

async def getweather():
client = python_weather.Client(format=python_weather.IMPERIAL)
weather = await client.find("Washington DC")
print(weather.current.temperature)
await client.close()

if name == "main":
loop = asyncio.get_event_loop()
```

Nguồn: tinhte.vn
 
Back
Top