File size: 1,284 Bytes
931d2d1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import streamlit as st
import json
DATABASE_FILE = "database.json"
def load_database():
try:
with open(DATABASE_FILE, "r") as file:
return json.load(file)
except FileNotFoundError:
return {}
def save_database(database):
with open(DATABASE_FILE, "w") as file:
json.dump(database, file, indent=4)
def find_similar_question(question, database):
similar_questions = []
for q in database.keys():
if question.lower() in q.lower() or q.lower() in question.lower():
similar_questions.append(q)
return similar_questions
def get_answer(question, database):
similar_questions = find_similar_question(question, database)
if len(similar_questions) > 0:
return database[similar_questions[0]]
else:
return "Извините, я не понимаю ваш вопрос."
def main():
st.title("Простой чат-бот")
question = st.text_input("Задайте ваш вопрос")
if st.button("Отправить"):
database = load_database()
answer = get_answer(question, database)
st.text_area("Ответ", value=answer, height=200)
database[question] = answer
save_database(database)
if __name__ == "__main__":
main()
|