Kolyadual commited on
Commit
8bf24da
·
verified ·
1 Parent(s): 1be283a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ import json
4
+ import random
5
+ import re
6
+ from difflib import get_close_matches
7
+
8
+ with open("patterns.json", "r", encoding="utf-8") as f:
9
+ PATTERNS = json.load(f)
10
+
11
+ KEYWORDS = {
12
+ "привет": ["привет", "здравствуй", "хай", "прив"],
13
+ "как дела": ["дела", "как ты", "настроение"],
14
+ "имя": ["имя", "кто ты", "зовут"],
15
+ "школа": ["школа", "урок", "задача", "математика"],
16
+ "пока": ["пока", "выход", "до свидания"],
17
+ "knowledge": ["что такое", "как приготовить", "расскажи про"]
18
+ }
19
+
20
+ def preprocess(text):
21
+ return re.sub(r'[^а-яё\s]', ' ', text.lower()).strip()
22
+
23
+ def find_response(user_input):
24
+ clean = preprocess(user_input)
25
+ if not clean:
26
+ return "Пожалуйста, напиши что-нибудь!"
27
+
28
+ if any(w in clean for w in ["пока", "выход", "стоп"]):
29
+ return "До свидания! Возвращайся скорее! 😊"
30
+
31
+ knowledge = PATTERNS.get("knowledge", {})
32
+ if clean in knowledge:
33
+ return knowledge[clean]
34
+ matches = get_close_matches(clean, knowledge.keys(), n=1, cutoff=0.6)
35
+ if matches:
36
+ return knowledge[matches[0]]
37
+
38
+ all_keywords = []
39
+ keyword_to_intent = {}
40
+ for intent, words in KEYWORDS.items():
41
+ if intent == "knowledge": continue
42
+ for word in words:
43
+ all_keywords.append(word)
44
+ keyword_to_intent[word] = intent
45
+
46
+ for token in clean.split():
47
+ matches = get_close_matches(token, all_keywords, n=1, cutoff=0.6)
48
+ if matches:
49
+ return random.choice(PATTERNS[keyword_to_intent[matches[0]]])
50
+
51
+ return random.choice(PATTERNS["default"])
52
+
53
+ demo = gr.Interface(
54
+ fn=find_response,
55
+ inputs=gr.Textbox(placeholder="Например: 'что такое любовь?' или 'превет!'."),
56
+ outputs="text",
57
+ title="🧠 Newton Micro — сверхлёгкий ИИ. Теперь умнее!",
58
+ description="Мини-ИИ на чистом Python. Поддерживает опечатки, рецепты и определения!",
59
+ examples=[
60
+ ["Превет!"],
61
+ ["Что такое дружба?"],
62
+ ["Как приготовить яичницу?"],
63
+ ["выхад"]
64
+ ]
65
+ )
66
+
67
+ if __name__ == "__main__":
68
+ demo.launch()