From 3228402c95d8a840136e3f3d2be318da4182a5a6 Mon Sep 17 00:00:00 2001 From: Mallikarjunarao Kosuri Date: Sun, 15 Oct 2017 05:58:02 +0000 Subject: [PATCH] API.AI now Dialogflow * apai.ai is now dialogflow * introduce dialogflow * base examples * cros headers * instalation procedures and tests * travis ci * repo updates * dialogflow version update * tests path * package version and build status * update setup classifier * summary update --- .travis.yml | 16 +++ README.md | 59 ++++++++--- django_dialogflow/__init__.py | 7 ++ .../django_dialogflow}/__init__.py | 0 .../django_dialogflow}/admin.py | 0 django_dialogflow/django_dialogflow/apps.py | 8 ++ .../migrations/0001_initial.py | 5 +- .../django_dialogflow/migrations}/__init__.py | 0 .../django_dialogflow}/models.py | 0 .../django_dialogflow}/settings.py | 33 +++--- .../static/css/bootstrap.css | 0 .../django_dialogflow}/static/css/custom.css | 0 .../django_dialogflow}/static/js/bootstrap.js | 0 .../django_dialogflow}/static/js/jquery.js | 0 .../django_dialogflow}/static/js/js.cookie.js | 0 .../django_dialogflow}/templates/app.html | 32 ++++-- django_dialogflow/django_dialogflow/tests.py | 98 ++++++++++++++++++ .../django_dialogflow}/urls.py | 4 +- django_dialogflow/django_dialogflow/views.py | 62 +++++++++++ .../django_dialogflow}/wsgi.py | 5 +- .../manage.py | 2 +- examples/base_examples.sh | 2 + examples/get_response.py | 66 ------------ examples/microphone_recognition.py | 62 ----------- examples/pyaudio_example.py | 36 ------- requirements.txt | 6 +- setup.py | 57 ++++++++++ src/__init__.py | 9 -- src/apiai_chat/apiai_chat/urls.py | 7 -- src/apiai_chat/apiai_chat/views.py | 2 - src/apiai_chat/chat/apps.py | 5 - .../migrations/0002_auto_20170925_1226.py | 24 ----- .../migrations/0003_auto_20170925_1521.py | 20 ---- src/apiai_chat/chat/migrations/__init__.py | 0 src/apiai_chat/chat/tests.py | 3 - src/apiai_chat/chat/views.py | 83 --------------- src/apiai_chat/db.sqlite3 | Bin 44032 -> 0 bytes 37 files changed, 348 insertions(+), 365 deletions(-) create mode 100644 .travis.yml create mode 100644 django_dialogflow/__init__.py rename {src/apiai_chat/apiai_chat => django_dialogflow/django_dialogflow}/__init__.py (100%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/admin.py (100%) create mode 100644 django_dialogflow/django_dialogflow/apps.py rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/migrations/0001_initial.py (87%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow/migrations}/__init__.py (100%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/models.py (100%) rename {src/apiai_chat/apiai_chat => django_dialogflow/django_dialogflow}/settings.py (81%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/static/css/bootstrap.css (100%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/static/css/custom.css (100%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/static/js/bootstrap.js (100%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/static/js/jquery.js (100%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/static/js/js.cookie.js (100%) rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/templates/app.html (89%) create mode 100644 django_dialogflow/django_dialogflow/tests.py rename {src/apiai_chat/chat => django_dialogflow/django_dialogflow}/urls.py (54%) create mode 100644 django_dialogflow/django_dialogflow/views.py rename {src/apiai_chat/apiai_chat => django_dialogflow/django_dialogflow}/wsgi.py (80%) rename {src/apiai_chat => django_dialogflow}/manage.py (68%) create mode 100644 examples/base_examples.sh delete mode 100644 examples/get_response.py delete mode 100644 examples/microphone_recognition.py delete mode 100644 examples/pyaudio_example.py create mode 100644 setup.py delete mode 100644 src/__init__.py delete mode 100644 src/apiai_chat/apiai_chat/urls.py delete mode 100644 src/apiai_chat/apiai_chat/views.py delete mode 100644 src/apiai_chat/chat/apps.py delete mode 100644 src/apiai_chat/chat/migrations/0002_auto_20170925_1226.py delete mode 100644 src/apiai_chat/chat/migrations/0003_auto_20170925_1521.py delete mode 100644 src/apiai_chat/chat/migrations/__init__.py delete mode 100644 src/apiai_chat/chat/tests.py delete mode 100644 src/apiai_chat/chat/views.py delete mode 100644 src/apiai_chat/db.sqlite3 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..88c2e5f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: python + +python: + - '2.7' + - '3.4' + - '3.5' + +install: + - pip install coveralls + - pip install -r requirements.txt + +script: + - python django_dialogflow/manage.py test django_dialogflow.tests + +after_success: + - coveralls diff --git a/README.md b/README.md index 99f9d95..14ada1d 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,16 @@ -# django-apiai +# Django Dialogflow -Django [API.AI](https://api.ai/) is a web client to chat. +[![Package Version](https://img.shields.io/pypi/v/django-dialogflow.svg)](https://pypi.python.org/pypi/django-dialogflow/) +[![Build Status](https://travis-ci.org/vkosuri/django-dialogflow.svg?branch=master)](https://travis-ci.org/vkosuri/django-dialogflow) -## How to train -In progress +Django [Dialogflow](https://dialogflow.com) is a web client to chat. -## Development -To make development environment clone this repository +## Using the development version -git clone https://github.com/vkosuri/django-apiai.git +You can clone the git repository by doing the following: ``` Bash -virtualenv venv -source venv/bin/activate -pip install --upgrade pip -pip install --upgrade -r requirements.txt +$ git clone git://github.com/vkosuri/django-dialogflow.git ``` ## Production your Bot @@ -47,10 +43,47 @@ CORS_ORIGIN_WHITELIST = ( ) ``` +## Installation + +If you are trying ``django_dialogflow`` as app, + +Then you could install django-dialogflow either via the Python Package Index (PyPI) or from source. + +To install using pip : + +``` Bash +$ pip install django-dialogflow +``` + +and then add it to your installed apps: + +``` Bash +INSTALLED_APPS = ( + ... + 'django_dialogflow', + ... +) +``` + +You will then want to create the necessary tables. If you generating schema migrations, you'll want to run: + +``` Bash +$ python manage.py migrate django_dialogflow +``` + ## Examples -All examples are located here https://github.com/vkosuri/django-apiai/tree/master/examples + +All [examples](./examples) are located here Github repo + ## Motivation + https://github.com/gunthercox/django_chatterbot ## LICENSE -Licensed under MIT https://github.com/vkosuri/django-apiai/blob/master/LICENSE.md \ No newline at end of file +Licensed under [MIT](./LICENSE.md) + +## Contributing + +Development of django_dialogflow happens at Github: http://github.com/vkosuri/django-dialogflow + +You are highly encouraged to participate in the development. If you don't like Github (for some reason) you're welcome to send regular patches. \ No newline at end of file diff --git a/django_dialogflow/__init__.py b/django_dialogflow/__init__.py new file mode 100644 index 0000000..8f48a26 --- /dev/null +++ b/django_dialogflow/__init__.py @@ -0,0 +1,7 @@ +""" +Django Dialogflow chat agent +""" +__version__ = '0.0.1' +__author__ = 'Mallikarjunarao Kosuri' +__email__ = 'malli.kv2@gmail.com' +__url__ = 'https://github.com/vkosuri/django-dialogflow' \ No newline at end of file diff --git a/src/apiai_chat/apiai_chat/__init__.py b/django_dialogflow/django_dialogflow/__init__.py similarity index 100% rename from src/apiai_chat/apiai_chat/__init__.py rename to django_dialogflow/django_dialogflow/__init__.py diff --git a/src/apiai_chat/chat/admin.py b/django_dialogflow/django_dialogflow/admin.py similarity index 100% rename from src/apiai_chat/chat/admin.py rename to django_dialogflow/django_dialogflow/admin.py diff --git a/django_dialogflow/django_dialogflow/apps.py b/django_dialogflow/django_dialogflow/apps.py new file mode 100644 index 0000000..d395ead --- /dev/null +++ b/django_dialogflow/django_dialogflow/apps.py @@ -0,0 +1,8 @@ +from django.apps import AppConfig + + +class DjangoDialogflowConfig(AppConfig): + + name = 'django_dialogflow' + label = 'django_dialogflow' + verbose_name = 'Django Dialogflow' diff --git a/src/apiai_chat/chat/migrations/0001_initial.py b/django_dialogflow/django_dialogflow/migrations/0001_initial.py similarity index 87% rename from src/apiai_chat/chat/migrations/0001_initial.py rename to django_dialogflow/django_dialogflow/migrations/0001_initial.py index f870c8b..38afa91 100644 --- a/src/apiai_chat/chat/migrations/0001_initial.py +++ b/django_dialogflow/django_dialogflow/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.5 on 2017-09-25 12:20 +# Generated by Django 1.11.5 on 2017-10-15 02:23 from __future__ import unicode_literals from django.db import migrations, models @@ -28,12 +28,11 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('text', models.CharField(max_length=400)), - ('pub_date', models.DateTimeField(verbose_name='date published')), ], ), migrations.AddField( model_name='response', name='statement', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='chat.Statement'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_dialogflow.Statement'), ), ] diff --git a/src/apiai_chat/chat/__init__.py b/django_dialogflow/django_dialogflow/migrations/__init__.py similarity index 100% rename from src/apiai_chat/chat/__init__.py rename to django_dialogflow/django_dialogflow/migrations/__init__.py diff --git a/src/apiai_chat/chat/models.py b/django_dialogflow/django_dialogflow/models.py similarity index 100% rename from src/apiai_chat/chat/models.py rename to django_dialogflow/django_dialogflow/models.py diff --git a/src/apiai_chat/apiai_chat/settings.py b/django_dialogflow/django_dialogflow/settings.py similarity index 81% rename from src/apiai_chat/apiai_chat/settings.py rename to django_dialogflow/django_dialogflow/settings.py index 44e1ca9..dd2541e 100644 --- a/src/apiai_chat/apiai_chat/settings.py +++ b/django_dialogflow/django_dialogflow/settings.py @@ -1,5 +1,5 @@ """ -Django settings for apiai_chat project. +Django settings for django_dialogflow project. For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/ @@ -22,17 +22,13 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['localhost'] -# API.AI Settings -URL = 'https://api.api.ai/v1/query' +# Dialogflow settings -# demo agent access token: e5dc21cab6df451c866bf5efacb40178 -CLIENT_ACCESS_TOKEN = '86773b26ca554576971f45baff906bce' -DEVELOPER_ACCESS_TOKEN = '7f3918105982484c866c5891ae31972f' - -# https://api.ai/docs/reference/agent/#versioning -API_VERSION = '20150910' +DIALOGFLOW = { + 'client_access_token': 'e5dc21cab6df451c866bf5efacb40178', +} # Application definition @@ -43,13 +39,13 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'django_bootstrap_base_template', - 'apiai_chat', - 'chat', + 'corsheaders', + 'django_dialogflow', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', + 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', @@ -57,7 +53,7 @@ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) -ROOT_URLCONF = 'apiai_chat.urls' +ROOT_URLCONF = 'django_dialogflow.urls' TEMPLATES = [ { @@ -75,7 +71,7 @@ }, ] -WSGI_APPLICATION = 'apiai_chat.wsgi.application' +WSGI_APPLICATION = 'django_dialogflow.wsgi.application' # Database @@ -106,3 +102,10 @@ # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATIC_URL = '/static/' + +CORS_ORIGIN_WHITELIST = ( + 'google.com', + 'hostname.example.com', + 'localhost:8000', + '127.0.0.1:9000' +) \ No newline at end of file diff --git a/src/apiai_chat/chat/static/css/bootstrap.css b/django_dialogflow/django_dialogflow/static/css/bootstrap.css similarity index 100% rename from src/apiai_chat/chat/static/css/bootstrap.css rename to django_dialogflow/django_dialogflow/static/css/bootstrap.css diff --git a/src/apiai_chat/chat/static/css/custom.css b/django_dialogflow/django_dialogflow/static/css/custom.css similarity index 100% rename from src/apiai_chat/chat/static/css/custom.css rename to django_dialogflow/django_dialogflow/static/css/custom.css diff --git a/src/apiai_chat/chat/static/js/bootstrap.js b/django_dialogflow/django_dialogflow/static/js/bootstrap.js similarity index 100% rename from src/apiai_chat/chat/static/js/bootstrap.js rename to django_dialogflow/django_dialogflow/static/js/bootstrap.js diff --git a/src/apiai_chat/chat/static/js/jquery.js b/django_dialogflow/django_dialogflow/static/js/jquery.js similarity index 100% rename from src/apiai_chat/chat/static/js/jquery.js rename to django_dialogflow/django_dialogflow/static/js/jquery.js diff --git a/src/apiai_chat/chat/static/js/js.cookie.js b/django_dialogflow/django_dialogflow/static/js/js.cookie.js similarity index 100% rename from src/apiai_chat/chat/static/js/js.cookie.js rename to django_dialogflow/django_dialogflow/static/js/js.cookie.js diff --git a/src/apiai_chat/chat/templates/app.html b/django_dialogflow/django_dialogflow/templates/app.html similarity index 89% rename from src/apiai_chat/chat/templates/app.html rename to django_dialogflow/django_dialogflow/templates/app.html index 8a7f017..5bbccf2 100644 --- a/src/apiai_chat/chat/templates/app.html +++ b/django_dialogflow/django_dialogflow/templates/app.html @@ -1,7 +1,7 @@ {% load staticfiles %} - API AI + Django Dialogflow @@ -9,7 +9,7 @@
@@ -32,12 +32,17 @@

API.AI

diff --git a/django_dialogflow/django_dialogflow/tests.py b/django_dialogflow/django_dialogflow/tests.py new file mode 100644 index 0000000..96fb31e --- /dev/null +++ b/django_dialogflow/django_dialogflow/tests.py @@ -0,0 +1,98 @@ +import json +from django.test import TestCase +from dialogflow import dialogflow +from mock import Mock +from django.core.urlresolvers import reverse + + +class MockResponse(object): + def __init__(self, status_code, data): + self.status_code = status_code + self.data = data + + def json(self): + return self.data + + +def mock_get_response(*args, **kwargs): + url = kwargs['url'] + endpoints = { + 'https://api.dialogflow.com/v1/query': MockResponse(200, { + "id": "b340a1f7-abee-4e13-9bdd-5e8938a48b7d", + "timestamp": "2017-02-09T15:38:26.548Z", + "lang": "en", + "result": { + "source": "agent", + "resolvedQuery": "my name is Sam and I live in Paris", + "action": "greetings", + "actionIncomplete": "false", + "parameters": {}, + "contexts": [], + "metadata": { + "intentId": "9f41ef7c-82fa-42a7-9a30-49a93e2c14d0", + "webhookUsed": "false", + "webhookForSlotFillingUsed": "false", + "intentName": "greetings" + }, + "fulfillment": { + "speech": "Hi Sam! Nice to meet you!", + "messages": [ + { + "type": 0, + "speech": "Hi Sam! Nice to meet you!" + } + ] + }, + "score": 1 + }, + "status": { + "code": 200, + "errorType": "success" + }, + "sessionId": "4b6a6779-b8ea-4094-b2ed-a302ba201815" + }) + } + + return endpoints[url] + + +class ViewTestCase(TestCase): + + def setUp(self): + super(ViewTestCase, self).setUp() + self.url = reverse('index') + + def test_get_main_page(self): + """ + Test that the index page can be loaded. + """ + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + + +class ApiTestCase(TestCase): + """ + Tests to make sure that the django_dialogflow API working fine + """ + + def setUp(self): + super(ApiTestCase, self).setUp() + dialogflow.requests.Session.get = Mock(side_effect=mock_get_response) + self.api_url = reverse('chat') + + def test_post(self): + """ + Test that a response is returned. + """ + data = { + 'text': 'How are you?' + } + response = self.client.post( + self.api_url, + data=json.dumps(data), + content_type='application/json', + format='json' + ) + + self.assertEqual(response.status_code, 200) + self.assertIn('text', str(response.content)) diff --git a/src/apiai_chat/chat/urls.py b/django_dialogflow/django_dialogflow/urls.py similarity index 54% rename from src/apiai_chat/chat/urls.py rename to django_dialogflow/django_dialogflow/urls.py index d251f15..71dfc2c 100644 --- a/src/apiai_chat/chat/urls.py +++ b/django_dialogflow/django_dialogflow/urls.py @@ -1,7 +1,9 @@ from django.conf.urls import url +from django.contrib import admin from .views import chat_view, index_view urlpatterns = [ url(r'chat/$', chat_view, name='chat'), - url(r'index/$', index_view, name='index'), + url(r'^$', index_view, name='index'), + url(r'^admin/', admin.site.urls), ] diff --git a/django_dialogflow/django_dialogflow/views.py b/django_dialogflow/django_dialogflow/views.py new file mode 100644 index 0000000..48de899 --- /dev/null +++ b/django_dialogflow/django_dialogflow/views.py @@ -0,0 +1,62 @@ + +from django.http import JsonResponse +from django.views.decorators.http import require_http_methods +from django.shortcuts import render +from django.conf import settings +from dialogflow import Dialogflow + +import json + + +def convert(data): + if isinstance(data, bytes): + return data.decode('ascii') + if isinstance(data, dict): + return dict(map(convert, data.items())) + if isinstance(data, tuple): + return map(convert, data) + + return data + + +@require_http_methods(['GET']) +def index_view(request): + return render(request, 'app.html') + + +@require_http_methods(['POST']) +def chat_view(request): + dialogflow = Dialogflow(**settings.DIALOGFLOW) + input_dict = convert(request.body) + input_text = json.loads(input_dict)['text'] + responses = dialogflow.text_request(str(input_text)) + + if request.method == "GET": + # Return a method not allowed response + data = { + 'detail': 'You should make a POST request to this endpoint.', + 'name': '/chat' + } + return JsonResponse(data, status=405) + elif request.method == "POST": + data = { + 'text': responses[0], + } + return JsonResponse(data, status=200) + elif request.method == "PATCH": + data = { + 'detail': 'You should make a POST request to this endpoint.', + 'name': '/chat' + } + + # Return a method not allowed response + return JsonResponse(data, status=405) + + elif request.method == "DELETE": + data = { + 'detail': 'You should make a POST request to this endpoint.', + 'name': '/chat' + } + + # Return a method not allowed response + return JsonResponse(data, status=405) diff --git a/src/apiai_chat/apiai_chat/wsgi.py b/django_dialogflow/django_dialogflow/wsgi.py similarity index 80% rename from src/apiai_chat/apiai_chat/wsgi.py rename to django_dialogflow/django_dialogflow/wsgi.py index f3d9e1b..88b2a05 100644 --- a/src/apiai_chat/apiai_chat/wsgi.py +++ b/django_dialogflow/django_dialogflow/wsgi.py @@ -8,7 +8,8 @@ """ import os -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apiai_chat.settings") - from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_dialogflow.settings") + application = get_wsgi_application() diff --git a/src/apiai_chat/manage.py b/django_dialogflow/manage.py similarity index 68% rename from src/apiai_chat/manage.py rename to django_dialogflow/manage.py index 952f4e8..3568c79 100644 --- a/src/apiai_chat/manage.py +++ b/django_dialogflow/manage.py @@ -3,7 +3,7 @@ import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apiai_chat.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_dialogflow.settings") from django.core.management import execute_from_command_line diff --git a/examples/base_examples.sh b/examples/base_examples.sh new file mode 100644 index 0000000..b605643 --- /dev/null +++ b/examples/base_examples.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +curl -H "Content-Type: application/json" "https://localhost:8008/chat?{'text':'hello'}" \ No newline at end of file diff --git a/examples/get_response.py b/examples/get_response.py deleted file mode 100644 index 67599ff..0000000 --- a/examples/get_response.py +++ /dev/null @@ -1,66 +0,0 @@ - -import requests -import uuid - - -class ApiAi(object): - - # create session - s = requests.Session() - - # API.AI query url - url = 'https://api.api.ai/v1/query' - - # Headers - headers = {} - - # https://api.ai/docs/reference/agent/#versioning - api_versioning = '20150910' - - # https://api.ai/docs/reference/agent/#languages - language = 'en' - session_id = uuid.uuid4().hex - timezone = 'Asia/Kolkata' - - def __init__(self, client_access_token): - """ Create Session: create a HTTP session to a server - """ - self.headers = { - 'Authorization': 'Bearer ' + client_access_token, - } - self.s.headers.update(self.headers) - - self.s.mount('http://', requests.adapters.HTTPAdapter()) - self.s.mount('https://', requests.adapters.HTTPAdapter()) - - self.url = self.url - - def get_response(self, text): - params = ( - ('v', self.api_versioning), - ('query', text), - ('lang', self.language), - ('sessionId', self.session_id), - ('timezone', self.timezone), - ) - responses = [] - result = self.s.get(url=self.url, params=params).json() - if result['status']['errorType'] == 'success': - for msg in result['result']['fulfillment']['messages']: - responses.append(msg['speech']) - else: - print('Failed to response from: ' + api_ai.url) - - return responses - - -if __name__ == '__main__': - # demo agent access token: e5dc21cab6df451c866bf5efacb40178 - client_access_token = '86773b26ca554576971f45baff906bce' - - api_ai = ApiAi(client_access_token); - response = api_ai.get_response('how are you') - print(response) - response = api_ai.get_response('howz the weather today') - print(response) - diff --git a/examples/microphone_recognition.py b/examples/microphone_recognition.py deleted file mode 100644 index 44c0ca0..0000000 --- a/examples/microphone_recognition.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python - -# NOTE: this example requires PyAudio because it uses the Microphone class -# If you stuck with any erros try to install these pacakges -# sudo apt-get install libjack-jackd2-dev portaudio19-dev -# sudo apt-get install jackd2 -# First you need to kill the existing pulseaudio process, -# start the jack_control process and re-start the pulseaudio process. -# Please use the below commands: -# pulseaudio --kill -# jack_control start -# jack_control exit -# pulseaudio --start -# For more information See https://pypi.python.org/pypi/SpeechRecognition/ - -import speech_recognition - - -def attempt_jack_control_start(): - """ - Jack is a program that can be used to get audio - input from your system. This command will try - to start it when your program runs. - """ - import subprocess - import warnings - try: - subprocess.call(['jack_control', 'start']) - except Exception: - # Note: jack_control is not a valid command in Windows - warnings.warn( - 'Unable to start jack control.', - RuntimeWarning - ) - -recognizer_function = 'recognize_sphinx' - -# Start jack control -# print("Jack control about to start") -# # attempt_jack_control_start() -# print("Jack control started") -# -# print("Lising default devices") -# for index, name in enumerate(speech_recognition.Microphone.list_microphone_names()): -# print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name)) - -recognizer = speech_recognition.Recognizer() -print("Starting Microphone") -with speech_recognition.Microphone() as source: - recognizer.adjust_for_ambient_noise(source) - print("Speak something") - audio = recognizer.listen(source) - print("Speech recognized") - -# recognize speech using Sphinx -try: - print("Sphinx thinks you said " + recognizer.recognize_sphinx(audio)) -except sr.UnknownValueError: - print("Sphinx could not understand audio") -except sr.RequestError as e: - print("Sphinx error; {0}".format(e)) - diff --git a/examples/pyaudio_example.py b/examples/pyaudio_example.py deleted file mode 100644 index 7230e4e..0000000 --- a/examples/pyaudio_example.py +++ /dev/null @@ -1,36 +0,0 @@ -import pyaudio -import wave - -FORMAT = pyaudio.paInt16 -CHANNELS = 2 -RATE = 44100 -CHUNK = 1024 -RECORD_SECONDS = 5 -WAVE_OUTPUT_FILENAME = "file.wav" - -audio = pyaudio.PyAudio() - -# start Recording -stream = audio.open(format=FORMAT, channels=CHANNELS, - rate=RATE, input=True, - frames_per_buffer=CHUNK) -print("recording...") -frames = [] - -for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): - data = stream.read(CHUNK) - frames.append(data) -print("finished recording") - - -# stop Recording -stream.stop_stream() -stream.close() -audio.terminate() - -waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb') -waveFile.setnchannels(CHANNELS) -waveFile.setsampwidth(audio.get_sample_size(FORMAT)) -waveFile.setframerate(RATE) -waveFile.writeframes(b''.join(frames)) -waveFile.close() diff --git a/requirements.txt b/requirements.txt index fa72579..a0e9811 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ Django>=1.11.5,<1.11.6 -future>=0.16.0,<0.16.1 -SpeechRecognition>=3.7.1,<3.7.2 -requests>=2.7.0,<2.7.1 +dialogflow>=0.0.4,<0.0.5 +mock +django-cors-headers diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7a2f132 --- /dev/null +++ b/setup.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +""" +Django Dialogflow setup file. +""" + +from setuptools import setup + + +# Dynamically retrieve the version information from the django_dialogflow module +DJANGO_DIALOGFLOW = __import__('django_dialogflow') +VERSION = DJANGO_DIALOGFLOW.__version__ +AUTHOR = DJANGO_DIALOGFLOW.__author__ +AUTHOR_EMAIL = DJANGO_DIALOGFLOW.__email__ +URL = DJANGO_DIALOGFLOW.__url__ +DESCRIPTION = 'Django Dialogflow chat agent' +LONG_DESCRIPTION = 'Django Dialogflow chat agent' + +with open('requirements.txt') as requirements: + REQUIREMENTS = requirements.readlines() + +setup( + name='django-dialogflow', + version=VERSION, + url=URL, + download_url='{}/tarball/{}'.format(URL, VERSION), + description=DESCRIPTION, + long_description=LONG_DESCRIPTION, + author=AUTHOR, + author_email=AUTHOR_EMAIL, + packages=[ + 'django_dialogflow', + ], + package_dir={'django_dialogflow': 'django_dialogflow'}, + include_package_data=True, + install_requires=REQUIREMENTS, + license='BSD', + zip_safe=True, + platforms=['any'], + keywords=['Django', 'Dialogflow', 'api.ai', 'chat', 'bot'], + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Environment :: Console', + 'Environment :: Web Environment', + 'Operating System :: OS Independent', + 'Topic :: Software Development :: Libraries :: Python Modules', + 'Topic :: Communications :: Chat', + 'Topic :: Internet', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + ], +) diff --git a/src/__init__.py b/src/__init__.py deleted file mode 100644 index 3b769d8..0000000 --- a/src/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -import sys - -if 'install' not in sys.argv and 'egg_info' not in sys.argv: - from .voice import VoiceInput - from .voice import VoiceOutput - -__version__ = '0.0.1' -__author__ = 'Mallikarjunarao Kosuri' -__email__ = 'venkatamallikarjunarao.kosuri@adtran.com' \ No newline at end of file diff --git a/src/apiai_chat/apiai_chat/urls.py b/src/apiai_chat/apiai_chat/urls.py deleted file mode 100644 index 520f665..0000000 --- a/src/apiai_chat/apiai_chat/urls.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.conf.urls import include, url -from django.contrib import admin - -urlpatterns = [ - url(r'^', include('chat.urls', namespace='apiai')), - url(r'^admin/', admin.site.urls), -] diff --git a/src/apiai_chat/apiai_chat/views.py b/src/apiai_chat/apiai_chat/views.py deleted file mode 100644 index 32dad7c..0000000 --- a/src/apiai_chat/apiai_chat/views.py +++ /dev/null @@ -1,2 +0,0 @@ - -from django.views.generic import View diff --git a/src/apiai_chat/chat/apps.py b/src/apiai_chat/chat/apps.py deleted file mode 100644 index 8ebb9f0..0000000 --- a/src/apiai_chat/chat/apps.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.apps import AppConfig - - -class ChatConfig(AppConfig): - name = 'chat' diff --git a/src/apiai_chat/chat/migrations/0002_auto_20170925_1226.py b/src/apiai_chat/chat/migrations/0002_auto_20170925_1226.py deleted file mode 100644 index bd34162..0000000 --- a/src/apiai_chat/chat/migrations/0002_auto_20170925_1226.py +++ /dev/null @@ -1,24 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11.5 on 2017-09-25 12:26 -from __future__ import unicode_literals - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('chat', '0001_initial'), - ] - - operations = [ - migrations.RenameField( - model_name='response', - old_name='response', - new_name='text', - ), - migrations.RemoveField( - model_name='statement', - name='pub_date', - ), - ] diff --git a/src/apiai_chat/chat/migrations/0003_auto_20170925_1521.py b/src/apiai_chat/chat/migrations/0003_auto_20170925_1521.py deleted file mode 100644 index 0aab905..0000000 --- a/src/apiai_chat/chat/migrations/0003_auto_20170925_1521.py +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11.5 on 2017-09-25 15:21 -from __future__ import unicode_literals - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('chat', '0002_auto_20170925_1226'), - ] - - operations = [ - migrations.RenameField( - model_name='response', - old_name='text', - new_name='response', - ), - ] diff --git a/src/apiai_chat/chat/migrations/__init__.py b/src/apiai_chat/chat/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/apiai_chat/chat/tests.py b/src/apiai_chat/chat/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/src/apiai_chat/chat/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/src/apiai_chat/chat/views.py b/src/apiai_chat/chat/views.py deleted file mode 100644 index f5d229f..0000000 --- a/src/apiai_chat/chat/views.py +++ /dev/null @@ -1,83 +0,0 @@ - -from django.http import JsonResponse -from django.views.decorators.http import require_http_methods -from django.shortcuts import render -import requests -import uuid - - -@require_http_methods(['GET']) -def index_view(request): - return render(request, 'app.html') - - -@require_http_methods(['GET', 'POST']) -def chat_view(request): - # create session - s = requests.Session() - - # API.AI query url - url = 'https://api.api.ai/v1/query' - - # Headers - headers = {} - - # https://api.ai/docs/reference/agent/#versioning - api_versioning = '20150910' - - # https://api.ai/docs/reference/agent/#languages - language = 'en' - session_id = uuid.uuid4().hex - timezone = 'Asia/Kolkata' - - headers = { - 'Authorization': 'Bearer 86773b26ca554576971f45baff906bce' - } - - s.headers.update(headers) - - s.mount('http://', requests.adapters.HTTPAdapter()) - s.mount('https://', requests.adapters.HTTPAdapter()) - - params = ( - ('v', api_versioning), - ('query', request.POST.text), - ('lang', language), - ('sessionId', session_id), - ('timezone', timezone), - ) - responses = [] - result = s.get(url=url, params=params).json() - if result['status']['errorType'] == 'success': - for msg in result['result']['fulfillment']['messages']: - responses.append(msg['speech']) - else: - print('Failed to response from: ' + url) - - if request.method == "GET": - # Return a method not allowed response - data = { - 'detail': 'You should make a POST request to this endpoint.', - 'name': 'API.AI' - } - return JsonResponse(data, status=405) - elif request.method == "POST": - data = { - 'text': responses, - } - return JsonResponse(data, status=200) - elif request.method == "PATCH": - data = { - 'detail': 'You should make a POST request to this endpoint.' - } - - # Return a method not allowed response - return JsonResponse(data, status=405) - - elif request.method == "DELETE": - data = { - 'detail': 'You should make a POST request to this endpoint.' - } - - # Return a method not allowed response - return JsonResponse(data, status=405) diff --git a/src/apiai_chat/db.sqlite3 b/src/apiai_chat/db.sqlite3 deleted file mode 100644 index a008bb39b343192ad812b9d7d8b92ec6257ee694..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 44032 zcmeHQU2Gf4ec#!kMA5daPkP<>RIMrNB%UmZyX2Ch4%a)zmTjIb>MY5TrQ$#=$(2Nk zA2LbVk`I@Lc5n#V21U}Q=u?{jc_>=o(Dorni{|CpA_ZEsKvUp=AVG_JxE_k;+7w9c zA${n7W_P(minMw1wXo$`$Ybuz|7(7;Gqd}jua%oOvQ+(wpM712A1?a_{zuI3c0Yr{^oL>>q`L8s3C)Z6r}zW>ANb$!FYwRJsCKM8o+H2! zcv1w$HIU%$hhSKO;QSB-T_+$Iy#T@IWKuT~{sn}>l=&a|$uEuoN8tD)@KRqd{wnm% zD?Rvocm;nOXYm*D*YNxJ{o|i^oX1IQ`5G4SG|J@k;#MiCXA0S3GGE#f zhq~|-()X&{$z7vd$W|)ZQt|Am6L{1^Bljvs8RMS_u7T6o?@4d#)nuirSB-*EtV%8% zv<0S1#VSN4s|UMAj~C%tJE4%>D(h7cS|Oc%e;=MOrId}zE<_rZm8Ab?FY+qTgEs~I zF|+`GkN*`P;7$BZ{2MF{|IHE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