Skip to content

Commit e1437df

Browse files
author
Abhay-1552
committed
Done
Necessary changes like comments, docstring, readme file, OOP and KISS based changes
1 parent 0dfa1e0 commit e1437df

File tree

5 files changed

+261
-142
lines changed

5 files changed

+261
-142
lines changed

ThirdAI/Terms and Conditions/Readme

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# ThirdAIApp and NeuralDBClient
2+
3+
This repository contains two components: `ThirdAIApp` and `NeuralDBClient`. `ThirdAIApp` is a graphical user interface (GUI) application for interacting with the ThirdAI neural database client. It allows you to perform training with PDF files and query the database. `NeuralDBClient` is a Python class that serves as a client for interacting with the ThirdAI neural database. It allows you to train the database with PDF files and perform queries to retrieve information.
4+
5+
## ThirdAIApp
6+
7+
### Features
8+
9+
- Insert PDF files for training.
10+
- Train the neural database client.
11+
- Enter queries to retrieve information from the database.
12+
- Display the output in a new window.
13+
14+
### Installation
15+
16+
To run `ThirdAIApp`, you need to have Python and Tkinter installed. You also need the `ThirdAI` library, which you can install using pip:
17+
18+
```bash
19+
pip install ThirdAI
20+
```
21+
22+
### Usage
23+
24+
1. Run the `ThirdAIApp.py` script.
25+
2. The main window will appear.
26+
3. Click the "Insert File!" button to select a PDF file for training.
27+
4. Click the "Training" button to train the neural database client with the selected file.
28+
5. Enter your query in the "Query" field.
29+
6. Click the "Processing" button to process the query and display the output in a new window.
30+
7. You can click the "Clear" button to clear the query and file selections.
31+
32+
### Dependencies
33+
34+
- Python 3.x
35+
- Tkinter
36+
- ThirdAI
37+
38+
## NeuralDBClient
39+
40+
### Features
41+
42+
- Train the neural database with PDF files.
43+
- Perform queries on the neural database.
44+
45+
### Installation
46+
47+
To use `NeuralDBClient`, you need to have the `thirdai` library installed, and you'll need an API key from ThirdAI.
48+
49+
You can install the `thirdai` library using pip:
50+
51+
```bash
52+
pip install thirdai
53+
```
54+
55+
### Usage
56+
57+
1. Import the `NeuralDBClient` class from `neural_db_client.py`.
58+
2. Create an instance of the `NeuralDBClient` class, providing your ThirdAI API key as an argument.
59+
60+
```python
61+
from neural_db_client import NeuralDBClient
62+
63+
client = NeuralDBClient(api_key="YOUR_API_KEY")
64+
```
65+
66+
3. Train the neural database with PDF files using the `train` method. Provide a list of file paths to the PDF files you want to use for training.
67+
68+
```python
69+
client.train(file_paths=["file1.pdf", "file2.pdf"])
70+
```
71+
72+
4. Perform queries on the neural database using the `query` method. Provide your query as a string, and the method will return the query results as a string.
73+
74+
```python
75+
result = client.query(question="What is the capital of France?")
76+
```
77+
78+
### Dependencies
79+
80+
- `thirdai` library
81+
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from thirdai import licensing, neural_db as ndb
2+
3+
4+
class NeuralDBClient:
5+
def __init__(self):
6+
# Activating ThirdAI Key
7+
licensing.activate("ADD-YOUR-THIRDAI-ACTIVATION-KEY")
8+
9+
# Creating NeuralBD variable to access Neural Database
10+
self.db = ndb.NeuralDB(user_id="my_user")
11+
12+
def train(self, file_paths):
13+
# Retrieving path of file
14+
insertable_docs = []
15+
pdf_files = file_paths
16+
17+
# Appending PDF file to the Database stack
18+
pdf_doc = ndb.PDF(pdf_files)
19+
insertable_docs.append(pdf_doc)
20+
21+
# Inserting/Uploading PDF file to Neural database for training
22+
self.db.insert(insertable_docs, train=True)
23+
24+
def query(self, question):
25+
# Searching of required query in neural database
26+
search_results = self.db.search(
27+
query=question,
28+
top_k=2,
29+
on_error=lambda error_msg: print(f"Error! {error_msg}"))
30+
31+
output = ""
32+
for result in search_results:
33+
output += result.text + "\n\n"
34+
35+
return output
36+
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import tkinter as tk
2+
from tkinter.font import Font
3+
from tkinter import messagebox
4+
from tkinter import filedialog
5+
from ThirdAI import NeuralDBClient as Ndb
6+
7+
8+
class ThirdAIApp:
9+
"""
10+
A GUI application for using the ThirdAI neural database client to train and query data.
11+
"""
12+
def __init__(self, root):
13+
"""
14+
Initialize the user interface window.
15+
16+
Args:
17+
root (tk.Tk): The main Tkinter window.
18+
"""
19+
# Initialize the main window
20+
self.root = root
21+
self.root.geometry("600x500")
22+
self.root.title('ThirdAI - T&C')
23+
24+
# Initialize variables
25+
self.path = []
26+
self.client = Ndb()
27+
28+
# GUI elements
29+
30+
# Labels and buttons
31+
self.menu = tk.Label(self.root, text="Terms & Conditions", font=self.custom_font(30), fg='black',
32+
highlightthickness=2, highlightbackground="red")
33+
self.menu.place(x=125, y=10)
34+
35+
self.insert_button = tk.Button(self.root, text="Insert File!", font=self.custom_font(15), fg='black', bg="grey",
36+
width=10, command=self.file_input)
37+
self.insert_button.place(x=245, y=100)
38+
39+
self.text_box = tk.Text(self.root, wrap=tk.WORD, width=30, height=1)
40+
self.text_box.place(x=165, y=150)
41+
42+
self.training_button = tk.Button(self.root, text="Training", font=self.custom_font(15), fg='black', bg="grey",
43+
width=10, command=self.training)
44+
self.training_button.place(x=245, y=195)
45+
46+
self.query_label = tk.Label(self.root, text="Query", font=self.custom_font(20), fg='black')
47+
self.query_label.place(x=255, y=255)
48+
49+
self.query_entry = tk.Entry(self.root, font=self.custom_font(20), width=30)
50+
self.query_entry.place(x=70, y=300)
51+
52+
self.processing_button = tk.Button(self.root, text="Processing", font=self.custom_font(15), fg='black',
53+
bg="grey", width=10, command=self.processing)
54+
self.processing_button.place(x=245, y=355)
55+
56+
self.clear_button = tk.Button(self.root, text="Clear", font=15, fg='black', bg="grey", width=10,
57+
command=self.clear_all)
58+
self.clear_button.place(x=245, y=405)
59+
60+
@staticmethod
61+
def custom_font(size):
62+
"""
63+
Create a custom font with the specified size.
64+
65+
Args:
66+
size (int): The font size.
67+
68+
Returns:
69+
Font: The custom Font object.
70+
"""
71+
return Font(size=size)
72+
73+
def file_input(self):
74+
"""
75+
Open a file dialog to select a PDF file and display its name in the text box.
76+
"""
77+
file_type = dict(defaultextension=".pdf", filetypes=[("pdf file", "*.pdf")])
78+
file_path = filedialog.askopenfilename(**file_type)
79+
80+
if file_path:
81+
self.path.append(file_path)
82+
file_name = file_path.split("/")[-1]
83+
self.text_box.delete(1.0, tk.END)
84+
self.text_box.insert(tk.INSERT, file_name)
85+
86+
def clear_all(self):
87+
"""
88+
Clear the query entry, text box, and reset the path.
89+
"""
90+
self.query_entry.delete(0, tk.END)
91+
self.text_box.delete(1.0, tk.END)
92+
self.path.clear()
93+
94+
def training(self):
95+
"""
96+
Train the neural database client with the selected PDF file.
97+
"""
98+
if not self.path:
99+
messagebox.showwarning("No File Selected", "Please select a PDF file before training.")
100+
return
101+
102+
self.client.train(self.path[0])
103+
104+
messagebox.showinfo("Training Complete", "Training is done!")
105+
106+
def processing(self):
107+
"""
108+
Process a user query and display the output in a new window.
109+
"""
110+
question = self.query_entry.get()
111+
112+
# When there is no query submitted by the user
113+
if not question:
114+
messagebox.showwarning("No Query", "Please enter a query.")
115+
return
116+
117+
output = self.client.query(question)
118+
self.display_output(output)
119+
120+
def display_output(self, output_data):
121+
"""
122+
Display the output data in a new window.
123+
124+
Args:
125+
output_data (str): The output text to be displayed.
126+
"""
127+
output_window = tk.Toplevel(self.root)
128+
output_window.title("Output Data")
129+
output_window.geometry("500x500")
130+
131+
output_text = tk.Text(output_window, wrap=tk.WORD, width=50, height=50)
132+
output_text.pack(padx=10, pady=10)
133+
output_text.insert(tk.END, output_data)
134+
135+
136+
if __name__ == "__main__":
137+
"""
138+
Initializing the main application window
139+
"""
140+
141+
# Calling the main application window
142+
win = tk.Tk()
143+
app = ThirdAIApp(win)
144+
win.mainloop()

ThirdAI/Terms and Conditions/third_AI.py

-34
This file was deleted.

0 commit comments

Comments
 (0)