so I am trying to make it so if the bot doesnt have a responce to what was said it will ask how it should respond and add that to a .json file. it pulls all of its responecs from that .json file. I need help making it upload thejson file to the database and json file to the database and update it when the code is ran, because I want it to keep things said even when preview mode. Any help is very much aprecitated.
I think he’s talking about expanding the storage besides the free storage that replit already give to you.
But again, I don’t think anyone will a small project will be consuming all of the free storage, and if that day arrives you will need to spend in resources as you would in any other place lol.
Some terrible “chatbot” I made, I don’t even know if it works or not:
ps. you require a folder named data & a json file named learned in it.
# - feel free to use this code, no credits required. -
import difflib
# import random
import json
import os
class Colors:
PURPLE = "\033[95m"
CYAN = "\033[96m"
DARKCYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
END = "\033[0m"
class SweetrLearn:
def __init__(self):
self._learned_data = json.load(open("./data/learned.json", "r"))
self._appended_data = []
def _read(self, _question):
_closest = difflib.get_close_matches(
_question,
self._learned_data.keys(),
n=1
)
try:
return f"{Colors.UNDERLINE}{Colors.PURPLE}You: {_question}\n{Colors.UNDERLINE}{Colors.BLUE}Sweetr: {self._learned_data[_closest[0]]}{Colors.END}"
except:
return f"{Colors.UNDERLINE}{Colors.PURPLE}You: {_question}\n{Colors.UNDERLINE}{Colors.BLUE}Sweetr: Im sorry, I do not have the answer for this question.{Colors.END}"
def _teach(self, _question: str, _resp):
try:
with open("./data/learned.json", "r+") as _data_file:
_data = self._learned_data
_data.append({_question: _resp})
json.dump(_data, _data_file, indent=3)
return f"{Colors.UNDERLINE}{Colors.BLUE}Sweetr Training\n\n{Colors.END}{Colors.BOLD}{Colors.GREEN}Successfully trained.{Colors.END}"
except Exception as e:
return f"{Colors.UNDERLINE}{Colors.BLUE}Sweetr Training\n\n{Colors.END}{Colors.BOLD}{Colors.RED}Failed to train.\nError: {e}{Colors.END}."
if __name__ == "__main__":
while True:
_input = input(f"{Colors.BOLD}{Colors.RED}-> {Colors.END}")
try:
os.system("clear")
except:
os.system("cls")
_answer = SweetrLearn()._read(_input)
if _answer == "Im sorry, I do not have the answer for this question.":
pass # _inpt = input(f"{Colors.BOLD}{Colors.BLUE}-> {Colors.END}")
else:
print(_answer)
# SweetrLearn()._teach("test", "Hello how can I help you?")