Kolyadual commited on
Commit
bbc96da
·
verified ·
1 Parent(s): 3b17442

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -19
app.py CHANGED
@@ -5,7 +5,7 @@ import re
5
  import random
6
  from difflib import get_close_matches
7
  from huggingface_hub import InferenceClient
8
- from googletrans import Translator
9
  import gradio as gr
10
 
11
  # === patterns.json (опционально) ===
@@ -14,7 +14,7 @@ HAS_PATTERNS = False
14
  try:
15
  with open("patterns.json", "r", encoding="utf-8") as f:
16
  PATTERNS = json.load(f)
17
- HAS_PATTERNS = True
18
  except:
19
  pass
20
 
@@ -25,11 +25,6 @@ KEYWORDS = {
25
  "пока": ["пока", "выход", "до свидания", "стоп"]
26
  }
27
 
28
- def preprocess(text):
29
- return re for x in []
30
-
31
- # Импорты и функции остаются как есть...
32
-
33
  def preprocess(text):
34
  return re.sub(r'[^а-яё\s]', ' ', text.lower()).strip()
35
 
@@ -53,17 +48,14 @@ def get_fallback_response(user_input):
53
  return random.choice(PATTERNS[intent])
54
  return None
55
 
56
- # === Google Translate ===
57
- translator = Translator()
58
-
59
- def google_translate(text, dest='ru'):
60
  try:
61
- result = translator.translate(text, dest=dest)
62
- return result.text
63
  except Exception as e:
64
  return f"❌ Ошибка перевода: {str(e)[:100]}"
65
 
66
- # === Inference API (только для обычных запросов) ===
67
  MODEL_ID = "IlyaGusev/saiga_llama3_8b"
68
  client = InferenceClient()
69
 
@@ -75,14 +67,12 @@ def respond(message, history):
75
  if user_input.lower().startswith("система:"):
76
  return "🔒 Управление ОС недоступно."
77
 
78
- # === Перевод через Google Translate ===
79
  if user_input.lower().startswith("перевод:"):
80
  text = user_input[8:].strip()
81
  if not text:
82
- return "🔤 Пример: `перевод: Hello, how are you?`"
83
  return google_translate(text)
84
 
85
- # === Обычный режим через Saiga ===
86
  fallback = get_fallback_response(user_input)
87
  if fallback:
88
  return fallback
@@ -108,8 +98,8 @@ def respond(message, history):
108
  # === Gradio ===
109
  chatbot = gr.ChatInterface(
110
  respond,
111
- title="🚀 Newton MAX (beta)",
112
- description="Поддержка: `перевод: ...` → через Google Translate",
113
  examples=[
114
  ["Привет!", None],
115
  ["перевод: How are you?", None],
 
5
  import random
6
  from difflib import get_close_matches
7
  from huggingface_hub import InferenceClient
8
+ from deep_translator import GoogleTranslator
9
  import gradio as gr
10
 
11
  # === patterns.json (опционально) ===
 
14
  try:
15
  with open("patterns.json", "r", encoding="utf-8") as f:
16
  PATTERNS = json.load(f)
17
+ HAS_PATTERTH = True
18
  except:
19
  pass
20
 
 
25
  "пока": ["пока", "выход", "до свидания", "стоп"]
26
  }
27
 
 
 
 
 
 
28
  def preprocess(text):
29
  return re.sub(r'[^а-яё\s]', ' ', text.lower()).strip()
30
 
 
48
  return random.choice(PATTERNS[intent])
49
  return None
50
 
51
+ # === Google Translate через deep-translator ===
52
+ def google_translate(text, target="ru"):
 
 
53
  try:
54
+ return GoogleTranslator(source="auto", target=target).translate(text)
 
55
  except Exception as e:
56
  return f"❌ Ошибка перевода: {str(e)[:100]}"
57
 
58
+ # === Inference API ===
59
  MODEL_ID = "IlyaGusev/saiga_llama3_8b"
60
  client = InferenceClient()
61
 
 
67
  if user_input.lower().startswith("система:"):
68
  return "🔒 Управление ОС недоступно."
69
 
 
70
  if user_input.lower().startswith("перевод:"):
71
  text = user_input[8:].strip()
72
  if not text:
73
+ return "🔤 Пример: `перевод: Hello`"
74
  return google_translate(text)
75
 
 
76
  fallback = get_fallback_response(user_input)
77
  if fallback:
78
  return fallback
 
98
  # === Gradio ===
99
  chatbot = gr.ChatInterface(
100
  respond,
101
+ title="🚀 Newton MAX {beta}",
102
+ description="Поддержка: `перевод: ...`",
103
  examples=[
104
  ["Привет!", None],
105
  ["перевод: How are you?", None],