main
Thamognya Kodi 2022-12-30 13:50:36 +07:00
parent 7c858b2005
commit 7da9bad2af
38 changed files with 127 additions and 33 deletions

View File

@ -23,10 +23,12 @@ import config
import internet
def answer(query: str) -> tuple[Any, list[str]]:
def answer(
query: str, GOOGLE_SEARCH_API_KEY: str, GOOGLE_SEARCH_ENGINE_ID: str
) -> tuple[Any, list[str]]:
QA_MODEL: Any = pipeline("question-answering")
GOOGLE_SEARCH_API_KEY = str(os.environ["INTERNET_ML_GOOGLE_API"])
GOOGLE_SEARCH_ENGINE_ID = str(os.environ["INTERNET_ML_GOOGLE_SEARCH_ENGINE_ID"])
# GOOGLE_SEARCH_API_KEY = str(os.getenv("INTERNET_ML_GOOGLE_API"))
# GOOGLE_SEARCH_ENGINE_ID = str(os.getenv("INTERNET_ML_GOOGLE_SEARCH_ENGINE_ID"))
results: tuple[list[str], list[str]] = internet.Google(
query, GOOGLE_SEARCH_API_KEY, GOOGLE_SEARCH_ENGINE_ID
).google()

View File

@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "internet-ml"
version = "1.0.1"
version = "1.0.6"
description = "Internet-ML: Allowing ML to connect to the internet"
readme = "./.github/README.md"
authors = ["Thamognya Kodi <contact@thamognya.com>"]

View File

@ -1,6 +1,6 @@
from django.apps import AppConfig
class QuestionAnswerConfig(AppConfig):
class NlpConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api.question_answer"
name = "api.nlp"

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ContextConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api.nlp.context"

View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class NocontextConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api.nlp.nocontext"

View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,25 @@
from typing import Any
import json
import internet_ml.NLP.no_context.QA
def QA(
query: str, INTERNET_ML_GOOGLE_API: str, INTERNET_ML_GOOGLE_SEARCH_ENGINE_ID: str
) -> Any:
try:
answer = internet_ml.NLP.no_context.QA.answer(
query, INTERNET_ML_GOOGLE_API, INTERNET_ML_GOOGLE_SEARCH_ENGINE_ID
)
content = json.dumps(
{"error": "", "response": answer[0], "resources": answer[1]}
)
except:
content = json.dumps(
{"error": "Google API key not present in .env or environment"}
)
return content
# print(QA("Who is Elon Musk?"))

View File

@ -6,5 +6,5 @@ from django.urls import include, path
from . import views
urlpatterns: list[Any] = [
path("", views.QAView.as_view()),
path("question-answering/", views.QAView.as_view(), name="nlp"),
]

View File

@ -1,14 +1,15 @@
import json
import os
from django.views.generic.base import TemplateView
from dotenv import load_dotenv
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from internet_ml.NLP.no_context import QA
from .tools import question_answer
load_dotenv()
import internet_ml.NLP.no_context.QA
class QAView(APIView):
@ -35,14 +36,12 @@ class QAView(APIView):
}
so check error if it exists first and then for other stuff
"""
try:
answer = QA.answer(request.POST.get("question"))
content = json.dumps(
{"error": "", "response": answer[0], "resources": answer[1]}
)
return Response(content, status=status.HTTP_200_OK)
except:
content = json.dumps(
{"error": "Google API key not present in .env or environment"}
)
return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
answer = internet_ml.NLP.no_context.QA.answer(
request.POST.get("question"),
str(os.getenv("INTERNET_ML_GOOGLE_API")),
str(os.getenv("INTERNET_ML_GOOGLE_SEARCH_ENGINE_ID")),
)
content = json.dumps(
{"error": "", "response": answer[0], "resources": answer[1]}
)
return content

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,11 @@
from typing import Any, List
from django.contrib import admin
from django.urls import include, path
from . import views
urlpatterns: list[Any] = [
path("", views.NLPView.as_view(), name="nlp"),
path("no-context/", include("api.nlp.nocontext.urls"), name="nlp no context api"),
]

View File

@ -0,0 +1,5 @@
from django.views.generic.base import TemplateView
class NLPView(TemplateView):
template_name = "index.api.nlp.dj.html"

View File

@ -1 +1 @@
This is my api list
api

View File

@ -6,6 +6,6 @@ from django.urls import include, path
from . import views
urlpatterns: list[Any] = [
path("", views.ApiView.as_view()),
path("question-answer/", include("api.question_answer.urls")),
path("", views.APIView.as_view(), name="api"),
path("nlp/", include("api.nlp.urls"), name="nlp api"),
]

View File

@ -1,7 +1,5 @@
from django.views.generic.base import TemplateView
# Create your views here.
class ApiView(TemplateView):
class APIView(TemplateView):
template_name = "index.api.dj.html"

View File

@ -42,7 +42,9 @@ INSTALLED_APPS: list[str] = [
"rest_framework",
"internet_ml",
"api",
"api.question_answer",
"api.nlp",
"api.nlp.context",
"api.nlp.nocontext",
]
REST_FRAMEWORK = {"DEFAULT_METADATA_CLASS": "rest_framework.metadata.SimpleMetadata"}
@ -64,9 +66,11 @@ TEMPLATES: list[Any] = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
str(BASE_DIR) + "/api/templates",
str(BASE_DIR) + "/internet_ml_server/templates",
str(BASE_DIR) + "/api/question_answer/templates",
str(BASE_DIR) + "/api/templates",
str(BASE_DIR) + "/api/nlp/templates",
str(BASE_DIR) + "/api/nlp/context/templates",
str(BASE_DIR) + "/api/nlp/nocontext/templates",
],
"APP_DIRS": True,
"OPTIONS": {

View File

@ -22,6 +22,6 @@ from . import views
urlpatterns: list[Any] = [
# path("admin/", admin.site.urls),
path("", views.IndexView.as_view(), name="api"),
path("", views.IndexView.as_view(), name="index"),
path("api/", include("api.urls"), name="api"),
]

View File

@ -1,7 +1,5 @@
from django.views.generic.base import TemplateView
# Create your views here.
class IndexView(TemplateView):
template_name = "index.internet_ml_server.dj.html"

View File

@ -36,6 +36,23 @@ tzdata = {version = "*", markers = "sys_platform == \"win32\""}
argon2 = ["argon2-cffi (>=19.1.0)"]
bcrypt = ["bcrypt"]
[[package]]
name = "django-environ"
version = "0.9.0"
description = "A package that allows you to utilize 12factor inspired environment variables to configure your Django application."
category = "main"
optional = false
python-versions = ">=3.4,<4"
files = [
{file = "django-environ-0.9.0.tar.gz", hash = "sha256:bff5381533056328c9ac02f71790bd5bf1cea81b1beeb648f28b81c9e83e0a21"},
{file = "django_environ-0.9.0-py2.py3-none-any.whl", hash = "sha256:f21a5ef8cc603da1870bbf9a09b7e5577ab5f6da451b843dbcc721a7bca6b3d9"},
]
[package.extras]
develop = ["coverage[toml] (>=5.0a4)", "furo (>=2021.8.17b43,<2021.9.0)", "pytest (>=4.6.11)", "sphinx (>=3.5.0)", "sphinx-notfound-page"]
docs = ["furo (>=2021.8.17b43,<2021.9.0)", "sphinx (>=3.5.0)", "sphinx-notfound-page"]
testing = ["coverage[toml] (>=5.0a4)", "pytest (>=4.6.11)"]
[[package]]
name = "django-stubs"
version = "1.13.1"
@ -257,4 +274,4 @@ files = [
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "58f95fa8d5dbf7be85c7a08f3dd2a5ddcc8dba0f233caec4e941898ce2f867fc"
content-hash = "650eb364a84df8d6cc70b2c19d177a09378245ef42efc0b95477f55bdc59f673"

View File

@ -16,6 +16,7 @@ django = "^4.1.4"
django-stubs = "^1.13.1"
djangorestframework = "^3.14.0"
python-dotenv = "^0.21.0"
django-environ = "^0.9.0"
[tool.mypy]
plugins = ["mypy_django_plugin.main"]