Spaces:
Runtime error
Runtime error
π Add a gradio UI
Browse files- Makefile +3 -0
- qnabot/QnABot.py +5 -3
- qnabot/__init__.py +1 -0
- qnabot/api.py +1 -1
- qnabot/tests/test_QnABot.py +3 -3
- qnabot/tests/test_ui.py +20 -0
- qnabot/ui.py +16 -0
- setup.py +1 -1
Makefile
CHANGED
@@ -20,6 +20,9 @@ build:
|
|
20 |
publish: clean build
|
21 |
$(PYTHON) -m twine upload dist/*
|
22 |
|
|
|
|
|
|
|
23 |
help:
|
24 |
@echo "install - install dependencies"
|
25 |
@echo "test - run tests"
|
|
|
20 |
publish: clean build
|
21 |
$(PYTHON) -m twine upload dist/*
|
22 |
|
23 |
+
trace:
|
24 |
+
langchain-server
|
25 |
+
|
26 |
help:
|
27 |
@echo "install - install dependencies"
|
28 |
@echo "test - run tests"
|
qnabot/QnABot.py
CHANGED
@@ -11,7 +11,7 @@ import os
|
|
11 |
class QnABot:
|
12 |
def __init__(
|
13 |
self,
|
14 |
-
directory: str,
|
15 |
index: str | None = None,
|
16 |
model: str | None = None,
|
17 |
verbose: bool = False,
|
@@ -36,7 +36,9 @@ class QnABot:
|
|
36 |
print("Using model: text-davinci-003")
|
37 |
self.llm = OpenAI(temperature=temperature)
|
38 |
|
39 |
-
def create_loader(self, directory: str):
|
|
|
|
|
40 |
# Create a loader based on the provided directory (either local or S3)
|
41 |
if directory.startswith("s3://"):
|
42 |
self.loader = S3DirectoryLoader(directory)
|
@@ -66,7 +68,7 @@ class QnABot:
|
|
66 |
a = self.chain.run(input_documents=input_documents, question=question)
|
67 |
print(a)
|
68 |
|
69 |
-
def
|
70 |
# Retrieve the answer to the given question and return it
|
71 |
input_documents = self.search_index.similarity_search(question, k=k)
|
72 |
answer = self.chain.run(input_documents=input_documents, question=question)
|
|
|
11 |
class QnABot:
|
12 |
def __init__(
|
13 |
self,
|
14 |
+
directory: str | None = None,
|
15 |
index: str | None = None,
|
16 |
model: str | None = None,
|
17 |
verbose: bool = False,
|
|
|
36 |
print("Using model: text-davinci-003")
|
37 |
self.llm = OpenAI(temperature=temperature)
|
38 |
|
39 |
+
def create_loader(self, directory: str | None):
|
40 |
+
if directory is None:
|
41 |
+
return
|
42 |
# Create a loader based on the provided directory (either local or S3)
|
43 |
if directory.startswith("s3://"):
|
44 |
self.loader = S3DirectoryLoader(directory)
|
|
|
68 |
a = self.chain.run(input_documents=input_documents, question=question)
|
69 |
print(a)
|
70 |
|
71 |
+
def ask(self, question: str, k=1) -> str:
|
72 |
# Retrieve the answer to the given question and return it
|
73 |
input_documents = self.search_index.similarity_search(question, k=k)
|
74 |
answer = self.chain.run(input_documents=input_documents, question=question)
|
qnabot/__init__.py
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
from .QnABot import QnABot
|
2 |
from .api import create_app
|
|
|
|
1 |
from .QnABot import QnABot
|
2 |
from .api import create_app
|
3 |
+
from .ui import create_interface
|
qnabot/api.py
CHANGED
@@ -7,7 +7,7 @@ def create_app(bot: QnABot):
|
|
7 |
|
8 |
@app.get("/v1/ask/{question}")
|
9 |
async def ask(question: str):
|
10 |
-
answer = bot.
|
11 |
return {"answer": answer}
|
12 |
|
13 |
return app
|
|
|
7 |
|
8 |
@app.get("/v1/ask/{question}")
|
9 |
async def ask(question: str):
|
10 |
+
answer = bot.ask(question)
|
11 |
return {"answer": answer}
|
12 |
|
13 |
return app
|
qnabot/tests/test_QnABot.py
CHANGED
@@ -14,9 +14,9 @@ correct_answer = "Iron Man, Thor, Hulk, Ant-Man"
|
|
14 |
sources = "SOURCES:"
|
15 |
|
16 |
|
17 |
-
def
|
18 |
bot = QnABot(directory=test_directory)
|
19 |
-
answer = bot.
|
20 |
|
21 |
# Assert that the answer contains the correct answer
|
22 |
assert correct_answer in answer
|
@@ -41,4 +41,4 @@ def test_save_load_index():
|
|
41 |
bot_with_predefined_index = QnABot(directory=test_directory, index=index_path)
|
42 |
|
43 |
# Assert that the bot returns the correct answer to the test question
|
44 |
-
assert correct_answer in bot_with_predefined_index.
|
|
|
14 |
sources = "SOURCES:"
|
15 |
|
16 |
|
17 |
+
def test_ask():
|
18 |
bot = QnABot(directory=test_directory)
|
19 |
+
answer = bot.ask(test_question)
|
20 |
|
21 |
# Assert that the answer contains the correct answer
|
22 |
assert correct_answer in answer
|
|
|
41 |
bot_with_predefined_index = QnABot(directory=test_directory, index=index_path)
|
42 |
|
43 |
# Assert that the bot returns the correct answer to the test question
|
44 |
+
assert correct_answer in bot_with_predefined_index.ask(test_question)
|
qnabot/tests/test_ui.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from qnabot import create_interface
|
3 |
+
|
4 |
+
|
5 |
+
def test_create_interface():
|
6 |
+
# create a mock QnABot object
|
7 |
+
class MockBot:
|
8 |
+
def ask(self, question: str):
|
9 |
+
return "Answer"
|
10 |
+
|
11 |
+
# create a mock example
|
12 |
+
example = [["What is your name?"], ["My name is Bot."]]
|
13 |
+
|
14 |
+
# call the function with the mock bot and example
|
15 |
+
interface = create_interface(MockBot(), examples=example)
|
16 |
+
|
17 |
+
# check if the interface has the correct properties
|
18 |
+
assert isinstance(interface, gr.Interface)
|
19 |
+
assert len(interface.examples) == 2
|
20 |
+
assert interface.examples == example
|
qnabot/ui.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from qnabot import QnABot
|
3 |
+
|
4 |
+
|
5 |
+
def create_interface(bot: QnABot, examples: list[list[str]] = []):
|
6 |
+
def ask(question: str):
|
7 |
+
return bot.ask(question)
|
8 |
+
|
9 |
+
interface = gr.Interface(
|
10 |
+
fn=ask,
|
11 |
+
inputs=gr.components.Textbox(lines=5, label="Question"),
|
12 |
+
outputs=gr.components.Textbox(lines=5, label="Answer"),
|
13 |
+
examples=examples,
|
14 |
+
)
|
15 |
+
|
16 |
+
return interface
|
setup.py
CHANGED
@@ -12,7 +12,7 @@ setup(
|
|
12 |
"unstructured",
|
13 |
"fastapi",
|
14 |
"faiss-cpu",
|
15 |
-
"
|
16 |
],
|
17 |
author="Megaklis Vasilakis",
|
18 |
author_email="megaklis.vasilakis@gmail.com",
|
|
|
12 |
"unstructured",
|
13 |
"fastapi",
|
14 |
"faiss-cpu",
|
15 |
+
"pdfminer.six",
|
16 |
],
|
17 |
author="Megaklis Vasilakis",
|
18 |
author_email="megaklis.vasilakis@gmail.com",
|