-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from fractalego/hybrid-interfaces
Hybrid interfaces
- Loading branch information
Showing
13 changed files
with
214 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
from typing import List | ||
|
||
from wafl.interface.base_interface import BaseInterface | ||
|
||
|
||
class ListInterface(BaseInterface): | ||
def __init__(self, interfaces_list: List[BaseInterface]): | ||
super().__init__() | ||
self._interfaces_list = interfaces_list | ||
self._synchronize_interfaces() | ||
|
||
async def output(self, text: str, silent: bool = False): | ||
await asyncio.wait( | ||
[interface.output(text, silent) for interface in self._interfaces_list], | ||
return_when=asyncio.ALL_COMPLETED | ||
) | ||
|
||
async def input(self) -> str: | ||
done, pending = await asyncio.wait( | ||
[interface.input() for interface in self._interfaces_list], | ||
return_when=asyncio.FIRST_COMPLETED | ||
) | ||
return done.pop().result() | ||
|
||
async def insert_input(self, text: str): | ||
await asyncio.wait( | ||
[interface.insert_input(text) for interface in self._interfaces_list], | ||
return_when=asyncio.ALL_COMPLETED | ||
) | ||
|
||
def bot_has_spoken(self, to_set: bool = None): | ||
for interface in self._interfaces_list: | ||
interface.bot_has_spoken(to_set) | ||
|
||
def activate(self): | ||
for interface in self._interfaces_list: | ||
interface.activate() | ||
super().activate() | ||
|
||
def deactivate(self): | ||
for interface in self._interfaces_list: | ||
interface.deactivate() | ||
super().deactivate() | ||
self._synchronize_interfaces() | ||
|
||
|
||
def add_hotwords(self, hotwords): | ||
for interface in self._interfaces_list: | ||
interface.add_hotwords(hotwords) | ||
|
||
def _synchronize_interfaces(self): | ||
for interface in self._interfaces_list: | ||
interface._utterances = self._utterances |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import asyncio | ||
import random | ||
import sys | ||
import threading | ||
|
||
from flask import render_template, redirect, url_for | ||
|
||
from wafl.interface.list_interface import ListInterface | ||
from wafl.interface.voice_interface import VoiceInterface | ||
from wafl.scheduler.scheduler import Scheduler | ||
from wafl.scheduler.web_loop import WebLoop | ||
from wafl.scheduler.conversation_loop import ConversationLoop | ||
from wafl.logger.local_file_logger import LocalFileLogger | ||
from wafl.events.conversation_events import ConversationEvents | ||
from wafl.interface.queue_interface import QueueInterface | ||
from wafl.config import Configuration | ||
from wafl.runners.routes import get_app, add_new_rules | ||
|
||
|
||
app = get_app() | ||
_logger = LocalFileLogger() | ||
|
||
|
||
def run_app(): | ||
@app.route("/create_new_instance", methods=["POST"]) | ||
def create_new_instance(): | ||
conversation_id = random.randint(0, sys.maxsize) | ||
result = create_scheduler_and_webserver_loop(conversation_id) | ||
add_new_rules(app, conversation_id, result["web_server_loop"]) | ||
thread = threading.Thread(target=result["scheduler"].run) | ||
thread.start() | ||
return redirect(url_for(f"index_{conversation_id}")) | ||
|
||
@app.route("/") | ||
async def index(): | ||
return render_template("selector.html") | ||
|
||
def create_scheduler_and_webserver_loop(conversation_id): | ||
config = Configuration.load_local_config() | ||
interface = ListInterface([VoiceInterface(config), QueueInterface()]) | ||
interface.activate() | ||
conversation_events = ConversationEvents( | ||
config=config, | ||
interface=interface, | ||
logger=_logger, | ||
) | ||
conversation_loop = ConversationLoop( | ||
interface, | ||
conversation_events, | ||
_logger, | ||
activation_word=config.get_value("waking_up_word"), | ||
) | ||
web_loop = WebLoop(interface, conversation_id, conversation_events) | ||
return { | ||
"scheduler": Scheduler([conversation_loop, web_loop]), | ||
"web_server_loop": web_loop, | ||
} | ||
|
||
app.run(host="0.0.0.0", port=8889) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_app() |
Oops, something went wrong.