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