Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,52 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from ddgs import DDGS
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 11 |
-
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
| 12 |
|
| 13 |
def web_search(query, max_results=3):
|
| 14 |
try:
|
| 15 |
with DDGS() as ddgs:
|
| 16 |
results = ddgs.text(query, region="ru-ru", max_results=max_results)
|
| 17 |
return "\n".join([f"{r['title']}: {r['body']}" for r in results])
|
| 18 |
-
except Exception:
|
| 19 |
-
return ""
|
| 20 |
|
| 21 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 22 |
user_input = message.strip()
|
| 23 |
user_lower = user_input.lower()
|
| 24 |
|
| 25 |
if user_lower.startswith("система:"):
|
| 26 |
-
return "🔒 Управление системой недоступно в
|
| 27 |
|
| 28 |
if user_lower.startswith("поиск:"):
|
| 29 |
query = user_input[6:].strip()
|
| 30 |
if not query:
|
| 31 |
-
return "🔍
|
|
|
|
| 32 |
context = web_search(query)
|
| 33 |
-
|
| 34 |
f"{system_message}\n\n"
|
| 35 |
-
f"
|
| 36 |
-
f"
|
| 37 |
)
|
| 38 |
else:
|
| 39 |
-
|
| 40 |
|
| 41 |
-
messages = [{"role": "user", "content":
|
| 42 |
-
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
| 43 |
|
| 44 |
response = ""
|
| 45 |
try:
|
| 46 |
for chunk in client.chat_completion(
|
| 47 |
-
messages,
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
):
|
| 50 |
token = chunk.choices[0].delta.content or ""
|
| 51 |
response += token
|
|
@@ -55,15 +56,24 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 55 |
|
| 56 |
chatbot = gr.ChatInterface(
|
| 57 |
respond,
|
|
|
|
| 58 |
additional_inputs=[
|
| 59 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 60 |
gr.Slider(1, 1024, 512, label="Макс. токенов"),
|
| 61 |
gr.Slider(0.1, 1.5, 0.7, label="Температура"),
|
| 62 |
gr.Slider(0.1, 1.0, 0.92, label="Top-p"),
|
| 63 |
],
|
| 64 |
-
title="🚀 Newton MAX",
|
| 65 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
chatbot.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from ddgs import DDGS
|
| 5 |
|
| 6 |
+
# Публичная модель → токен НЕ нужен!
|
| 7 |
+
MODEL_NAME = "openai/gpt-oss-20b"
|
| 8 |
+
client = InferenceClient() # ← без токена!
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def web_search(query, max_results=3):
|
| 11 |
try:
|
| 12 |
with DDGS() as ddgs:
|
| 13 |
results = ddgs.text(query, region="ru-ru", max_results=max_results)
|
| 14 |
return "\n".join([f"{r['title']}: {r['body']}" for r in results])
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return f"Ошибка поиска: {str(e)[:100]}"
|
| 17 |
|
| 18 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 19 |
user_input = message.strip()
|
| 20 |
user_lower = user_input.lower()
|
| 21 |
|
| 22 |
if user_lower.startswith("система:"):
|
| 23 |
+
return "🔒 Управление системой недоступно в демо-версии."
|
| 24 |
|
| 25 |
if user_lower.startswith("поиск:"):
|
| 26 |
query = user_input[6:].strip()
|
| 27 |
if not query:
|
| 28 |
+
return "🔍 Пример: `поиск: погода в Москве`"
|
| 29 |
+
yield "🔍 Ищу...\n"
|
| 30 |
context = web_search(query)
|
| 31 |
+
full_prompt = (
|
| 32 |
f"{system_message}\n\n"
|
| 33 |
+
f"ИНФОРМАЦИЯ ИЗ ИНТЕРНЕТА:\n{context}\n\n"
|
| 34 |
+
f"ВОПРОС: {query}\nОТВЕТ НА РУССКОМ:"
|
| 35 |
)
|
| 36 |
else:
|
| 37 |
+
full_prompt = f"{system_message}\n\nВОПРОС: {user_input}\nОТВЕТ НА РУССКОМ:"
|
| 38 |
|
| 39 |
+
messages = [{"role": "user", "content": full_prompt}]
|
|
|
|
| 40 |
|
| 41 |
response = ""
|
| 42 |
try:
|
| 43 |
for chunk in client.chat_completion(
|
| 44 |
+
messages,
|
| 45 |
+
model=MODEL_NAME,
|
| 46 |
+
max_tokens=max_tokens,
|
| 47 |
+
stream=True,
|
| 48 |
+
temperature=temperature,
|
| 49 |
+
top_p=top_p,
|
| 50 |
):
|
| 51 |
token = chunk.choices[0].delta.content or ""
|
| 52 |
response += token
|
|
|
|
| 56 |
|
| 57 |
chatbot = gr.ChatInterface(
|
| 58 |
respond,
|
| 59 |
+
type="messages",
|
| 60 |
additional_inputs=[
|
| 61 |
+
gr.Textbox(
|
| 62 |
+
value="Ты — Newton MAX. Отвечай кратко, точно и только на русском языке.",
|
| 63 |
+
label="Системное сообщение"
|
| 64 |
+
),
|
| 65 |
gr.Slider(1, 1024, 512, label="Макс. токенов"),
|
| 66 |
gr.Slider(0.1, 1.5, 0.7, label="Температура"),
|
| 67 |
gr.Slider(0.1, 1.0, 0.92, label="Top-p"),
|
| 68 |
],
|
| 69 |
+
title="🚀 Newton MAX (gpt-oss-20b + поиск)",
|
| 70 |
+
description="Работает без входа! Поддержка: `поиск: ...`",
|
| 71 |
+
examples=[
|
| 72 |
+
["Привет!", "Ты — Newton MAX...", 512, 0.7, 0.92],
|
| 73 |
+
["Что такое ИИ?", "Ты — Newton MAX...", 512, 0.7, 0.92],
|
| 74 |
+
["поиск: последние новости", "Ты — Newton MAX...", 768, 0.8, 0.95],
|
| 75 |
+
],
|
| 76 |
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
chatbot.launch())
|