|
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() |
|
|