This code is intended to store a server application that provides the user answers to trivia-like questions. It is part of a computer science assignment (2nd year, 1st semester).
(Self imposed) deadline: 29 / 12 / 2022
Hard deadline: 19 / 01 / 2023
This project will be available on Github once the hard deadline passes.
Once the hard deadline passes, this project will be available under the MIT license. The code and its documentation were kindly contributed by:
- Maria-Miruna ALDICA (@miruna15aldica) - The GUI module;
- Matei-Ștefan IONESCU (@Matei77) - The Server module;
- Valentin-Ioan VINTILĂ (@w1bb) - The AI module.
There are three ways to run this project, and each comes with its advantages and disadvantages:
- Running the project locally without venv - this has the benefit of running out of the box as long as a few requirements are met (please read the dedicated subsection);
- Running the project locally with venv - this is almost the same as the first option, only it is safer, but it consumes more time;
- Running the project using docker - this will take even longer, but will work on any machine.
Pick your poison!
In order to run this project, a few assumptions are made:
- The user is on a Linux OS (preferably Ubuntu or Arch);
- The user has
make
,python3.10
andpip3
installed, alongside a browser to check out the resulting server.
To build and run the project, please follow these steps:
- From the parent (current) folder, run
make build
. This will create a folder calledproduct
that will contain all the files required by the server. The command will also check to see if the required packages are installed and, if they are missing, it will install them. If you dislike this behaviour, check out the other ways of running the server. - Run
make run
in order to start the server. In a browser (e.g. Firefox), you can now go to127.0.0.1:8080
and check the functionality of the application. Please note that the terminal in which you typed the command will now act as a logger. You should also make sure the8080
port is not already in use. - When you are done, you can exit the server by pressing
Ctrl+C
. - (Optional) Once you are done testing the functionality, you can run a
cleanup procedure by typing
make clean
. Please make sure the server is not running in the background before running this command.
In order to run this project, a few assumptions are made:
- The user is on a Linux OS (preferably Ubuntu or Arch);
- The user has
make
,python3.10
,pip3
andvenv
installed, alongside a browser to check out the resulting server. Note thatvenv
should come with the Python package, but this is not guaranteed (Debian might not shippython
withvenv
).
To build and run the project, please follow these steps:
- From the parent (current) folder, run
make venv-build
. This will create a folder calledvenv-product
that will contain all the files required by the server. The command will also install the required packages in the virtual environment. - Run
make venv-run
in order to start the server. In a browser (e.g. Firefox), you can now go to127.0.0.1:8080
and check the functionality of the application. Please note that the terminal in which you typed the command will now act as a logger. You should also make sure the8080
port is not already in use. - When you are done, you can exit the server by pressing
Ctrl+C
. - (Optional) Once you are done testing the functionality, you can run a
cleanup procedure by typing
make venv-clean
. Please make sure the server is not running in the background before running this command.
In order to run this project, a few assumptions are made:
- The user is on a Linux OS (preferably Ubuntu or Arch);
- The user has
docker
correctly installed and configured on its machine. This might require the user to get added to a special group.
To build and run the project, please follow these steps:
- From the parent (current) folder, run
make docker-build
. This will create the files insidedocker-product
and then will promptdocker
to create an image file. This process will take a while. - After the first step is complete, one should get a message similar to this
one:
In this case, the resulting image is called
(...) Step 13/13 : CMD ["make", "run-here"] ---> Running in bcdf1f83fd11 Removing intermediate container bcdf1f83fd11 ---> ee0ed09054f9 Successfully built ee0ed09054f9 Successfully tagged w1bb/ia4:1.0 Everything was setup successfully!
ee0ed09054f9
(it can be determined as it follows theSuccessfully built
message). Use that name to start the actual server - this can be accomplished by running:Based on the example above, one should runwi@arch:~$ docker run -p 8080:8080 <IMAGE NAME>
docker run -p 8080:8080 ee0ed09054f9
. In a browser (e.g. Firefox), you can now go to127.0.0.1:8080
and check the functionality of the application. Please note that the terminal in which you typed the command will now act as a logger. You should also make sure the8080
port is not already in use. - When you are done, you can exit the server by pressing
Ctrl+C
. - (Optional) Once you are done testing the functionality, you can run a
cleanup procedure by typing
make docker-clean
. Please make sure the server is not running in the background before running this command.
This section will present the inner-workings of the code.
In order to function properly, the project requires a number of packages, each with its specific functionality:
bs4
, required by the AI module - it helps scrape the Wikipedia articles;flask
, required by the Server module - it provides the functionality to instantiate the actual server;google
, required by the AI module - it searches Google for the best Wikipedia articles;lxml
, required by the AI module - it is used by BeautifulSoup;rich
- used throught the project to make text beautiful;tensorflow
, required by the AI module - it is used by thetransformers
package;transformers
, required by the AI module - it is the actual brain of the operation;waitress
, required by the Server module - it provides the functionality to host the server onlocalhost:8080
.
Please note that all these packages can be installed via pip3
.
The AI folder contains the following files:
ai.py = The file containing the AI implementation;
ai-test.py = An example that tests the AI implementation by allowing an agent to
ask as many questions as he desires (break with Ctrl+C).
This file contains two important functions:
-
ask_question(original_question)
This function receives a question (
original_question
) and provides an answer in the form of a string. If no results could be found in a given time span, the"Sorry, could not find any answer on Wikipedia!"
string is returned instead. -
get_urls(original_question, stop_no, urls)
This function searches Google for a question (
original_question
) and tries to findstop_no
answers; the result is returned inresults
(this design choice was made so thatask_question
could call it as a separate process in order to limit its maximum time consumption).
The former function is created to be exported, whilst the latter is used
inside ask_question
and should not be relevant to the other parts of the
project.
Note: The ask_question
function is slow at times, please be patient!
Run this script and try questions such as Who is the current president of the United States?
or When did the roman empire fall?
. Were the results
satisfactory?
The GUI folder contains the following files:
static/ = The folder containing the styles and other static elements (in this
| case, only the styles);
|-- styles/ = The folder containing the styles;
| |-> style-answer.css = The CSS corresponding to the answer.html page;
| |-> style-index.css = The CSS corresponding to the index.html page;
templates/ = The folder containing the HTML files;
|-> answer.html = The page loaded once an answer is provided by the bot;
|-> index.html = The query page.
These files contains the design of the program.
The web application is implemented using the Flask
framework. When the user
first loads the web page the server will render the index.html page. After
a question is asked and submitted the server will be notified that the POST
method was used and will get the question from the form and try to find the
best answer for it, which will be given by rendering the answer.html page.
The server is run using the waitress
WSGI (Web Server Gateway Interface).
The serve
function starts the server and runs the application.