Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,17 +5,17 @@ import re
|
|
| 5 |
import random
|
| 6 |
from difflib import get_close_matches
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
-
from
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
-
# ===
|
| 12 |
PATTERNS = {}
|
| 13 |
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 |
|
| 21 |
KEYWORDS = {
|
|
@@ -25,6 +25,11 @@ KEYWORDS = {
|
|
| 25 |
"пока": ["пока", "выход", "до свидания", "стоп"]
|
| 26 |
}
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def preprocess(text):
|
| 29 |
return re.sub(r'[^а-яё\s]', ' ', text.lower()).strip()
|
| 30 |
|
|
@@ -48,63 +53,51 @@ def get_fallback_response(user_input):
|
|
| 48 |
return random.choice(PATTERNS[intent])
|
| 49 |
return None
|
| 50 |
|
| 51 |
-
# ===
|
| 52 |
-
|
| 53 |
-
client = InferenceClient()
|
| 54 |
|
| 55 |
-
def
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
def respond(message, history):
|
| 67 |
user_input = message.strip()
|
| 68 |
if not user_input:
|
| 69 |
-
return "
|
| 70 |
|
| 71 |
-
# Система
|
| 72 |
if user_input.lower().startswith("система:"):
|
| 73 |
-
return "🔒 Управление
|
| 74 |
|
| 75 |
-
# Перевод
|
| 76 |
if user_input.lower().startswith("перевод:"):
|
| 77 |
text = user_input[8:].strip()
|
| 78 |
if not text:
|
| 79 |
return "🔤 Пример: `перевод: Hello, how are you?`"
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
prompt = f"ИНФОРМАЦИЯ ИЗ ИНТЕРНЕТА:\n{context}\n\nОтветь кратко и точно на русском языке: {query}"
|
| 89 |
-
# Обычный режим
|
| 90 |
-
else:
|
| 91 |
-
fallback = get_fallback_response(user_input)
|
| 92 |
-
if fallback:
|
| 93 |
-
yield fallback
|
| 94 |
-
return
|
| 95 |
-
prompt = f"Ответь кратко и чётко на русском языке: {user_input}"
|
| 96 |
-
|
| 97 |
-
# Вызов модели через Inference API
|
| 98 |
messages = [{"role": "user", "content": prompt}]
|
| 99 |
response = ""
|
| 100 |
try:
|
| 101 |
for chunk in client.chat_completion(
|
| 102 |
messages,
|
| 103 |
model=MODEL_ID,
|
| 104 |
-
max_tokens=
|
|
|
|
| 105 |
temperature=0.6,
|
| 106 |
-
top_p=0.92
|
| 107 |
-
stream=True
|
| 108 |
):
|
| 109 |
token = chunk.choices[0].delta.content or ""
|
| 110 |
response += token
|
|
@@ -112,16 +105,15 @@ def respond(message, history):
|
|
| 112 |
except Exception as e:
|
| 113 |
yield f"⚠️ Ошибка: {str(e)}"
|
| 114 |
|
| 115 |
-
# === Gradio
|
| 116 |
chatbot = gr.ChatInterface(
|
| 117 |
respond,
|
| 118 |
-
title="🚀 Newton MAX",
|
| 119 |
-
description="Поддержка:
|
| 120 |
examples=[
|
| 121 |
["Привет!", None],
|
| 122 |
-
["
|
| 123 |
-
["
|
| 124 |
-
["перевод: How are you?", None]
|
| 125 |
],
|
| 126 |
theme="soft"
|
| 127 |
)
|
|
|
|
| 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 (опционально) ===
|
| 12 |
PATTERNS = {}
|
| 13 |
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 |
|
| 21 |
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 |
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 |
|
| 70 |
def respond(message, history):
|
| 71 |
user_input = message.strip()
|
| 72 |
if not user_input:
|
| 73 |
+
return "Введите запрос."
|
| 74 |
|
|
|
|
| 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
|
| 89 |
+
|
| 90 |
+
prompt = f"Ответь кратко на русском: {user_input}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
messages = [{"role": "user", "content": prompt}]
|
| 92 |
response = ""
|
| 93 |
try:
|
| 94 |
for chunk in client.chat_completion(
|
| 95 |
messages,
|
| 96 |
model=MODEL_ID,
|
| 97 |
+
max_tokens=256,
|
| 98 |
+
stream=True,
|
| 99 |
temperature=0.6,
|
| 100 |
+
top_p=0.92
|
|
|
|
| 101 |
):
|
| 102 |
token = chunk.choices[0].delta.content or ""
|
| 103 |
response += token
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
yield f"⚠️ Ошибка: {str(e)}"
|
| 107 |
|
| 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],
|
| 116 |
+
["Что такое ИИ?", None]
|
|
|
|
| 117 |
],
|
| 118 |
theme="soft"
|
| 119 |
)
|