# Trigger Aryan

## Wake up Aryan from Sleep

Once AI device is powered on, Aryan gets started in Sleeping mode and waits for Human to Trigger it. A Infinite while loop runs awaiting Human to trigger the Aryan to wake up from Sleep in the **AryanStartStopCommand** flow.&#x20;

**RawSpeechRecognizationTrigger** flow is used to recognize the Human speech and output is stored in **ListenSpeech** for every 3 sec in loop.

Below code snippet demonstrates this flow and as soon as ListenSpeech matches Aryan or Arya respective flows are triggered by **startAI** flow.

It is from here that **aifriend** is identified as Aryan or Arya, they remain with you until Aryan or Arya goes back to sleep.

### Terminate Aryan Master

{% hint style="warning" %}
If the Human say "Terminate" then Aryan Master process are shutdown and have to be started again to bring them back to sleep.
{% endhint %}

```python
while True:
    PrintText("Aryan/Arya", "Sleeping...Say Aryan or Arya to Wake me up")

    ListenSpeech = RawSpeechRecognizationTrigger().lower().lstrip().rstrip()

    if ListenSpeech != "NA":

        if subStrCheck(ListenSpeech,"aryan"):
            if not (subStrCheck(ListenSpeech,"exit") or subStrCheck(ListenSpeech,"stop")):
                aifriend = "Aryan"

                startAI(aifriend,ledproc)

        elif subStrCheck(ListenSpeech,"arya"):
            if not (subStrCheck(ListenSpeech,"exit") or subStrCheck(ListenSpeech,"stop")):
                aifriend = "Arya"

                startAI(aifriend,ledproc)

        elif subStrCheck(ListenSpeech,"terminate"): 

            kill_led(ledproc,"Aryan/Arya")
            PrintText("Internal", "Shutting down Aryan and Arya..")
            DisplayText("Shutting down..")
            exit()
```

## Listening to User Speech in Loop

Below snippet is **RawSpeechRecognizationTrigger** which keeps listening for Human phrase for time limit of 3 sec in loop.&#x20;

Google Speech Recognition is used to recognize the speech, In case no speech or input then exception is raised but no action is taken and control fall backs to listen again as we want to listen to Human in loop awaiting trigger.

```python
r = sr.Recognizer()

with sr.Microphone() as source:  
    audio = r.listen(source, phrase_time_limit=3)

try:
    speech = r.recognize_google(audio)
    PrintText(humanname,speech)
    return speech

# Handling Exceptions if speech is not understood.
except sr.UnknownValueError:
    speech = "NA"
    return speech

except sr.RequestError as e:  
    speech = "NA"
    return speech
```

## Trigger AI Worker to Start Aryan

Once AI Master get the wakeup command from the Human with AI Friend identified as Aryan or Arya, thereafter AI Worker is triggered which starts the worker script to Start Aryan or Arya using **startAI flow**

It is accomplished by starting a operating system sub process to start the AIWorker.py script and pass the AI Friend (Aryan or Arya) as parameter.

Variable **proc** is used to capture the sub process and further monitoring of the child worker by Master.

```python
workername = os.path.join(os.path.dirname(__file__) or '.','AIWorker.py')

proc = subprocess.Popen([sys.executable,workername,"-a",aifriend], stderr=subprocess.PIPE)
```

## Monitor Aryan Worker

For the entire Duration when Aryan is in conversation with Human using AI Worker process, AI Master process keeps

### Listening for explicit command to Stop AI Worker

* **Listening** to the Human in case Human wants to exit the AI worker explicitly
  * In this case as soon as the stop trigger words such as "stop", "exit", "no thank you" are identified&#x20;
    * the AI Worker process is killed by AI Master using the PID captured while starting the child instance.
  * and control goes back to starting point i.e. AryanStartStopCommand and AI Master goes back to sleep.

### **Monitoring for Graceful Exit of AI Worker**

* **Monitoring** if AI Worker process exits naturally after completion of the command given by Human.
  * AI Worker PID is being checked using an Infinite while loop and if poll() for the sub process is None,&#x20;
    * that is either the process is killed (PID is removed when exited from the Process table)
    * &#x20;or is not functional (Zombie process in Linux)&#x20;
    * Thereafter AI Master kills the process&#x20;
  * and control goes back to starting point i.e. AryanStartStopCommand and AI Master goes back to sleep.

```python
while True:

    if psutil.pid_exists(proc.pid) and proc.poll() is not "None":

        PrintText("Aryan", "Listening...Say " + aifriend + " Stop to Stop me")

        ListenSpeech = RawSpeechRecognizationTrigger().lower().lstrip().rstrip()

        if (subStrCheck(ListenSpeech,"exit") or subStrCheck(ListenSpeech,"stop") or subStrCheck(ListenSpeech,"no thank you")):
            PrintText("Internal","About to Exit")
            kill_led(ledproc1,aifriend)
            kill_process(proc,aifriend)
            AryanStartStopCommand()

    else:
        kill_led(ledproc1,aifriend)
        kill_process(proc,aifriend)
        AryanStartStopCommand()   
```

{% hint style="success" %}
Phew, Aryan is sleeping again, waiting for Human trigger to wake up again.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aryanai.gitbook.io/docs.aryanandarya/overview/trigger-arya-or-aryan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
