DivyanshuSinha/Pythonaibrain-v1.1.9
Pythonaibrain
PythonAIBrain is a versatile, plug-and-play Python package designed to help you build offline intelligent AI assistants and applications effortlessly. With modules covering text-to-speech, natural language understanding, and more, Pythonaibrain lets you create powerful AI solutions without deep expertise or complex setup. Whether you’re a beginner or an experienced developer, get ready to bring your AI ideas to life quickly and efficiently.
Installation
Install the Pythonaibrain package:
pip install pythonaibrain==1.1.9About Pythonaibrain Package
Pythonaibrain package consists of pyaitk (which means Python AI Toolkit) module and PyAgent modules, pyaitk provides you methods to create your AI models/chatbots where as PyAgent provides best GUI and Web supports for intrection with models/chatbots. It also provide best contorl on your device. In this package you got default pre-trained models.
pyaitk (Python AI ToolKit)
pyaitk provides various type of methods and functions to create an advance AI.
How to import pyaitk
After pythonaibrain installations run this code for importing pyaitk
import pyaitkOn importing the module it internally train built-in models to work more smootly and fast.
Note : It takes few minutes to train model.
Built-in methods, modules and function in pyaitk :
Methods & Functions
- Camera
- TTS (Text To Speech)
- PTT (PDF To Text)
- ITT (Image To Text)
- MathAI
- Search
- Memory
- Contexts
- Brain
- Advance Brain
Camera Method
The pyaitk toolkit supports Camera to click photos and make videos. It can save photos/videos and send them to pyaitk Brain method for processing.
Example: Start your camera
import pyaitk
from pyaitk import Camera
import tkinter as tk
root = tk.Tk() # Create GUI
Camera(root) # Start camera and pass root as master
root.mainloop() # Run the GUIOr simply:
from pyaitk.Camera import Start
Start()Or via Brain module:
from pyaitk import Brain
brain = Brain()
brain.load()
brain.process_messages('Click Photo')TTS (Text To Speech) Method
Converts text to speech in male or female voice.
Example
from pyaitk import TTS
tts = TTS(text='Hello World')
tts.say(voice='david') # Male voice in windows
tts.say(voice='zira') # Female voice in windows
tts.say(voice= 'alex') # Male voice in macos
tts.say(voice= 'samantha') # Female voice in macos
tts.say(voice= 'english') # for linuxBy default, tts.say() uses the male voice of windows so agrest with your device type, also see the TTS _doc_.
You should know the voice for different OS
Other ways to use TTS.
from pyaitk import speak
text = "Hello World" # Taking a text
speak(text) # speak functions speak the given text without doing all the setup of TTS method.Syntax for speak function
speak(text: str = 'Hi', voice: str = <accordind to device type>):What speak function returns?
speak function returns None.
Note: You can also set voice according to your choice
PTT (PDF To Text) Function
PTT (PPT To Text) function extract text from the given .pdf file.
Example
import pyaitk
from pyaitk import PTT
ptt = PTT(path='example.pdf') # Provide your file path
print(ptt) # Extracted text outputWhat PPT function returns?
The PPT function return the extracted text in the string formate.
MathAI Function
It is a built-in function in pyaitk for solving complex, and symbolic math problems.
Way to use MathAI function
Steps
- First
import MathAIfunction frompyaitk module.
from pyaitk import MathAI- Create a variable for storing query in string formate.
query = "X + Y - 2X + 3Y" # let's take a query- Now get the answer from the
MathAIfunction for the query.
answer = MathAI(query= query) # MathAI gives the answer in string formate.- After that print the answer.
print(answer)From this way we can ask any query of math with MathAI.
Full Code
from pyaitk import MathAI
query = "X + Y - 2X + 3Y" # let's take a query
answer = MathAI(query= query) # MathAI gives the answer in string formate.
print(answer)Note: MathAI have fixed pattern of prompts, means you can ask question in direct form not question with string like, ``python query = "Solve X + Y - 2X + 3Y" `` Try to ask direct questions only.Prompts for MathAI
Solve normal numeric problems.
1 + 2 + 3 + 4 - 5 (1 - 55) + 10Solve symbolic methamatic.
X + 2Y - X + 10Z * (10 - 100)XMatrix
Matrix([[1, 0], [0, 1]])
Matrix([[1,2,3], [2, 3, 10]])Trigonometric Functions
sin
sin(0)
sin(30)
sin(45)
sin(60)
sin(90)
...
sin(180)
...Syntax
sin(<value of theata>)cos
cos(0)
cos(30)
cos(45)
cos(60)
cos(90)
...
cos(180)
...Syntax
cos(<value of theata>)tan
tan(0)
tan(30)
tan(45)
tan(60)
tan(90)
...
tan(180)
...Syntax
tan(<value of theata>)cosec
csc(0)
csc(30)
csc(45)
csc(60)
csc(90)
...
csc(180)
...Syntax
csc(<value of theata>)sec
sec(0)
sec(30)
sec(45)
sec(60)
sec(90)
...
sec(180)
...Syntax
sec(<value of theata>)cot
cot(0)
cot(30)
cot(45)
cot(60)
cot(90)
...
cot(180)
...Syntax
cot(<value of theata>)Limit
limit('2X')
limit('2X + 3Y')Syntax
limit(<symbolic and trigonometric values in string formate>)Determinants
det(Matrix([[10]]))
det(Matrix([[10], [20]]))
det(Matrix([[10], [30], [40]]))
det(Matrix([[10], [100], [0], [50]]))
det(Matrix([[10], [97], [95], [1], [99]]))
det(Matrix([[10], [11], [100], [3], [2], [150]]))
det(Matrix([[10, 20]]))
det(Matrix([[10, 20], [20, 30]]))
det(Matrix([[10, 20], [60, 70], [80, 100]]))
det(Matrix([[10, 20, 30]]))
det(Matrix([[10, 20, 30], [40, 50, 60]]))
det(Matrix([[10, 20, 30], [40, 50, 60], [90, 100, 102]]))
det(Matrix([[10, 20, 30], [40, 50, 60], [90, 100, 102], [95, 101, 1000]]))
...Log
log(10)
log(20)
log(0)
log(100)
...Ln
ln(0)
ln(1)
ln(10)
ln(100)
ln(1000)
ln(102)
ln(3)
ln(90)
ln(893)
ln(9)
...E
e()↑ Get the value of e.
e(10)
e(3)
e(21)
e(38)
e(0)
...⊼ (pi)
pi()↑ Get the value of ⊼
Square
Get all square of the value.
sqrt(10)
sqrt(2)
sqrt(30)
sqrt(28039)
sqrt(19)
sqrt(289843190)
...Differential
diff('2x')
diff('x')
diff('y')
...Give all the values in string formate.
Integration (∫)
Give all the values in string formate.
integrate('dx')
integrate('x dx')
integrate('2x dx')
integrate('2x dy')
integrate('x dy')
integrate('2x + 3y - 3y dx')
...Factor
Get all the factors of the numbers
factor(10)
factor(213)
factor(389)
factor(1983)
factor(0)
factor(12)
...Search Method
Search method is for searching queries from internate, and gives top five answers, links, summaries.
Ways to use Search method
Steps
- First
import Searchmethod frompyaitk module.
from pyaitk import Search- Assigne a varible for query
query = 'pythonaibrain'- Assigne a varible, and pass query in it.
search = Search(query = query)- > If no query given it by default takes 'pythonaibrain' as a query.
- Run the run funcition inside Search to get all top five answers, links, summaries.
search.run()- Get answer.
search.get_results_str()- > Note: It return all the answer in string formate, so we will try to fetch all the answers in a variable.
- Fetch answer.
answer = search.get_results_str()- At last print all the fetch answers.
print(answer)Full code
from pyaitk import Search
query = 'pythonaibrain'
search = Search(query)
search.run()
# Get answer.
answer = search.get_results_str()
# print the fetched answer.
print(answer)Memory Method
It is use for LTM (Long-Term-Memory) ans STM (Short-Term-Memory), or we can say LSTM (Long-Short-Term-Memory)
Way to use Memory Method
Steps
- First we will
import Memorymethod frompyaitk module.
from pyaitk import memory- After that.
memory = Memory() # you can pass name of your memory file otherwise it by default takes memory.json- > Note : The memory file extention should be
.json
- Now we will remember function for remember.
memory.remember('user', 'Hi') # It takes key value pairs as a parameter.- Now we recall the messages.
memory.recall('user') # It takes key as a peremeter.- Now we save the memory for LTM.
memory.save_memory() # It saves the memory for long time.- Now we load our LTM.
memory.load_memory() # It load our saved memory, but if the memory is not saved it takes empty dict.- If we want to delete our existing memory we will use
clear_memoryfunction.
memory.clear_memory() # It cleare all the data to our memory.- > Remember : It will not delete the file.
Contexts Method
Contexts method extract sutable answer asked by user from the given context.
Example
from pyaitk import Contexts
context = '''
Patanjali Ayurved is an Indian company. It was founded by Baba Ramdev and Acharya Balkrishna in 2006.
'''
question = 'Who founded Patanjali Ayurved?'
contexts = Contexts()
answer = contexts.ask(context=context, question=question)
print(answer)What Contexts method returns?
The Contexts method itself rethurn nothing, but it's ask attribute returns answers in string formate.
Brain Method
It is a powerfull method which allow you to create a smart AI models and chatbots. It gives you better flexibility to work on your model.
You can also give your dataset to train it. It also supports function mapping.
You can also download pre-trained models from it.
Note: Not all models, you can download models which supports pythonaibrain.
Ways to use Brain Method
Steps
- First we will
import Brainfrompyaitk module.
from pyaitk import Brain- Let's create brain of our chatbot.
brain = Brain() # You can pass your intents path, and functions for function mapping.- Let's load our default model.
brain.load() # It loads the default model- If you give your dataset then you should train and save your model.
brain = Brain('intents.json')
brain.train() # It takes few minutes or may be hour, it depends you dataset size.
brain.save()- Let's start chatting with default model (in basic style).
while True:
message = input('Message : ')
answer = brain.process_messages(answer)
brain.pyai_say(answer)- Other ways to talk with our model (in advance style).
while True:
message = input('Message : ')
answer = brain.talk(answer, grammer= False, TTS= True) # You can also close TTS by passing TTS= False.
brain.write(answer)Full Code
from pyaitk import Brain
brain = Brain()
# def Hello():
# return "Hello"
# brain = Brain(intents_path= 'intents.json', hello= Hello)
# brain.train()
# brain.save()
brain.load()
while True:
message = input("Message : ")
# answer = brain.process_messages(message)
answer = brain.talk(message, grammer= False, TTS= True)
# brain.pyai_say(answer)
brain.write(answer)
# for more detail about functions run:
# print(brain.__all__)Functions of Brain method
from pyaitk import Brain
brain = Brain()- If you want to know the model is loaded or not, then use
is_loadedfunction.
brain.is_loaded()It returns True/ False.
- If you want to know the model is saved or not, then use
is_savedfunction.
brain.is_saved()It also returns True/ False.
- Or, if you want to konw the model is trained or not, then use
is_trainedfunction.
brain.is_trained()It also returns True/ False.
- If you want to know type of inputed message then use
predict_message_typefunction.
brain.predict_message_type('What is your name?') # Returns -> Question.What predict_message_type function can returns for the message
- Question
- Answer
- Command
- Shutdown
- Make Directory
- Statement
- Name
- Know
- Start
Notes:
ShutdownandStartcommands require terminal support and do not work on Android or iOS.
Make Directorycreates folders on your device.Statementis any plain text that is not a command/question.Namedetects if a message contains a person's name.
How to create intents.json
{
"intents": [
{
"tag": "greeting",
"patterns": ["Hi", "Hello", "Hey", "What's up?", "Howdy"],
"responses": ["Hello! How can I help you today?", "Hey there!", "Hi! What can I do for you?"]
},
{
"tag": "bye",
"patterns": ["Bye", "See you soon", "Take care"],
"responses": ["Bye! Have a great day", "See you"]
}
]
}Save this as a .json file and provide it to the Brain module.
Other way
from pyaitk import Brain
brain = Brain(intents_path='intents.json') # or Brain() with default
brain.train()
brain.save()
message = input('Message: ')
message_type = brain.predict_message_type(message=message)
if message_type in ['Question', 'Answer']:
print(f'Answer: {brain.process_messages(message=message)}')Note : train and save the file when use first time or change the default intents.json with your intents.json otherwise use load function by writting,
brain.load() # it returns boolAnd also don't forgot to load otherwise it through an error Or,
from pyaitk import Brain
brain = Brain()
brain.load()
message = input('Message: ')
message_type = brain.predict_message_type(message=message)
if message_type in ['Question', 'Answer']:
print(f'Answer: {brain.process_messages(message=message)}')Advance Brain Module
An advanced version of the Brain module with smarter classification and better entity recognition.
Usage
from pyaitk import AdvanceBrain
advance_brain = AdvanceBrain(intents_path='intents.json') # or AdvanceBrain()
message = input('Message: ')
message_type = advance_brain.predict_message_type(message=message)
if message_type in ['Question', 'Answer']:
print(f'Answer: {advance_brain.process_messages(message=message)}')Same limitations apply as Brain module for commands.
Pythonaibrain Summary.
Built-in AI Assistant - PyAgent
If you prefer not to code your own AI, use the built-in PyAgent GUI or web assistant.
GUI
import PyAgent
PyAgent.App()Web Server
from PyAgent import PYAS
PYAS.app.run(debug=False)Or
from PyAgent import Server
server = Server()
server.run()Socket Client-AI Server
Make your own client-AI server for handling multiples client togeather, for client side we use pythonaibrain-client module.
Install pythonaibrain-client
pip install pythonaibrain-clientAfter that,
from pyaibrain.client import ClientServer
cServer = ClientServer()
cServer.serve()This will create client side in cli (Command Line Interface).
Socket Server
from pyaitk import server
server()From this way we can make main server for which handle clients.
Visit PyPI for installation and more details.
Visit GitHub for more detail about package.
Visit Pythonaibrain for Documentation.
Visit Pythonaibrain Issues for any issues.
Start building your AI assistant today with Pythonaibrain! Try to ask your doubt with AI releated to this package!
