internet_ml/internet_ml/NLP/no_context/QA.py

43 lines
1.0 KiB
Python
Raw Normal View History

2022-12-27 06:38:47 +00:00
from typing import Any, List, Tuple
2022-12-26 15:43:10 +00:00
2022-12-27 06:38:47 +00:00
import logging
2022-12-30 05:28:26 +00:00
import os
2022-12-25 17:15:24 +00:00
import sys
from pathlib import Path
2022-12-30 05:28:26 +00:00
import dotenv
2022-12-25 17:15:24 +00:00
from transformers import pipeline
2022-12-27 06:38:47 +00:00
logging.basicConfig(
filename="QA.log",
filemode="w",
level=logging.INFO,
format="%(name)s - %(levelname)s - %(message)s",
)
2022-12-25 17:15:24 +00:00
sys.path.append(str(Path(__file__).parent.parent.parent) + "/tools/NLP/data")
2022-12-27 06:38:47 +00:00
sys.path.append(str(Path(__file__).parent.parent.parent) + "/utils")
import config
2022-12-25 17:15:24 +00:00
import internet
2022-12-26 15:43:10 +00:00
2022-12-30 06:50:36 +00:00
def answer(
query: str, GOOGLE_SEARCH_API_KEY: str, GOOGLE_SEARCH_ENGINE_ID: str
) -> tuple[Any, list[str]]:
2022-12-30 05:40:40 +00:00
QA_MODEL: Any = pipeline("question-answering")
2022-12-30 05:28:26 +00:00
results: tuple[list[str], list[str]] = internet.Google(
query, GOOGLE_SEARCH_API_KEY, GOOGLE_SEARCH_ENGINE_ID
).google()
2022-12-27 06:38:47 +00:00
answer: tuple[Any, list[str]] = (
QA_MODEL(question=query, context=str(results[0])),
results[1],
)
if config.CONF_DEBUG:
logging.info(f"Answer: {answer}")
return answer
2023-01-01 13:01:12 +00:00
# print(os.environ)
# print(answer("Who is Elon Musk?"))
2022-12-27 06:38:47 +00:00
# def custom_answer