Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Nymisha
Created July 30, 2024

Virtuza:Your Personalised Companion

The idea incorporates a new type of virtual voice assistant that can recognize mixed speech in languages such as Telugu and English.

14
Virtuza:Your Personalised Companion

Things used in this project

Software apps and online services

Open AI

Story

Read more

Schematics

Basic workflow

It tells you about the general process of flow of the code.

Code

Source file

Python
Import the necessary libraries and buy an Open API key and use it.
import speech_recognition as sr
from gtts import gTTS
import os
import datetime as dt
import pywhatkit as pk
import openai
import pygame
import sys

# Set up OpenAI API key
openai.api_key = " "  # Replace with your actual API key

# Initialize Pygame mixer
pygame.mixer.init()

def speak(cmd):
    print("Speaking...")
    print('')
    tts = gTTS(cmd, lang="te")
    tts.save('response.mp3')

    # Play audio file
    try:
        pygame.mixer.music.load('response.mp3')
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():  # Wait for the sound to finish playing
            pygame.time.Clock().tick(10)
    except Exception as e:
        print(f"Error in speak: {e}")
    finally:
        pygame.mixer.music.stop()  # Ensure playback is stopped
        pygame.mixer.music.unload()  # Unload the file
        os.remove('response.mp3')  # Remove the file after playing

listener = sr.Recognizer()
va_name = ""

speak("!   ,    ")

def take_cmd(check):
    command = ""
    try:
        with sr.Microphone() as source:
            print("Hearing...")
            print('')
            audio = listener.listen(source)
            if check:
                command = listener.recognize_google(audio, language='te')
                if va_name in command:
                    command = command.replace(" ", "")
                else:
                    command = ""
            else:
                command = listener.recognize_google(audio, language='en-US')
    except sr.UnknownValueError:
        print("Can't hear..please check your microphone")
    except sr.RequestError as e:
        print(f"Error with the speech recognition service: {e}")
    return command

def handle_command(command):
    if "" in command:
        current_time = dt.datetime.now().strftime("%I:%M %p")
        speak(current_time)
    elif "" in command:
        speak("     ")
        video_cmd = take_cmd(False)
        pk.playonyt(video_cmd)
        speak("  ")
        return True
    elif "" in command:
        speak("     ")
        search_cmd = take_cmd(False)
        pk.search(search_cmd)
    else:
        # Use OpenAI to respond to other commands
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "You are a helpful assistant that can respond accurately to various questions in English and Telugu."},
                {"role": "user", "content": command}
            ],
            temperature=0.7,
            max_tokens=150
        )
        response_text = response.choices[0].message['content'].strip()
        print("Response:", response_text)
        speak(response_text)

while True:
    final_cmd = take_cmd(True)
    if final_cmd:
        if handle_command(final_cmd):
            break

input('Press Enter to Close')

Credits

Nymisha
1 project • 0 followers

Comments