Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import requests
|
| 4 |
import re
|
|
|
|
| 5 |
# ==== Константы ====
|
| 6 |
MODEL_NAME = "openai/gpt-oss-20b"
|
| 7 |
SYSTEM_MESSAGE = "Ты Pok.Bot, ты используешь открытую локальную модель GPT-OSS от OpenAI, не GPT-4 (наверно😆). Тебя создал POKilondron. Используй емодзи 😄.If you don't know the answer, output a command like this:#search <query> Do NOT invent facts."
|
|
@@ -10,21 +11,11 @@ TEMPERATURE = 0.7
|
|
| 10 |
TOP_P = 0.95
|
| 11 |
|
| 12 |
def search_web(query: str, max_results: int = 3):
|
| 13 |
-
url = f"https://api.duckduckgo.com/?q={query}&format=json"
|
| 14 |
-
try:
|
| 15 |
-
r = requests.get(url, timeout=10)
|
| 16 |
-
r.raise_for_status()
|
| 17 |
-
data = r.json()
|
| 18 |
-
except Exception as e:
|
| 19 |
-
return f"⚠️ Ошибка поиска: {e}"
|
| 20 |
-
|
| 21 |
results = []
|
| 22 |
-
|
| 23 |
-
for
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return "\n".join(results) if results else "Ничего не найдено."
|
| 28 |
|
| 29 |
|
| 30 |
# ==== Основная функция ====
|
|
@@ -45,13 +36,12 @@ def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
|
|
| 45 |
):
|
| 46 |
if not msg.choices:
|
| 47 |
continue
|
| 48 |
-
|
| 49 |
delta = msg.choices[0].delta.content
|
| 50 |
if delta:
|
| 51 |
response += delta
|
| 52 |
yield response
|
| 53 |
|
| 54 |
-
#
|
| 55 |
if response.strip().startswith("#search"):
|
| 56 |
query = response.replace("#search", "").strip()
|
| 57 |
web_context = search_web(query)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import requests
|
| 4 |
import re
|
| 5 |
+
from duckduckgo_search import DDGS
|
| 6 |
# ==== Константы ====
|
| 7 |
MODEL_NAME = "openai/gpt-oss-20b"
|
| 8 |
SYSTEM_MESSAGE = "Ты Pok.Bot, ты используешь открытую локальную модель GPT-OSS от OpenAI, не GPT-4 (наверно😆). Тебя создал POKilondron. Используй емодзи 😄.If you don't know the answer, output a command like this:#search <query> Do NOT invent facts."
|
|
|
|
| 11 |
TOP_P = 0.95
|
| 12 |
|
| 13 |
def search_web(query: str, max_results: int = 3):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
results = []
|
| 15 |
+
with DDGS() as ddg:
|
| 16 |
+
for r in ddg.text(query, max_results=max_results):
|
| 17 |
+
results.append(f"- {r['title']} ({r['href']})\n{r['body']}")
|
| 18 |
+
return "\n\n".join(results) if results else "Ничего не найдено."
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
# ==== Основная функция ====
|
|
|
|
| 36 |
):
|
| 37 |
if not msg.choices:
|
| 38 |
continue
|
|
|
|
| 39 |
delta = msg.choices[0].delta.content
|
| 40 |
if delta:
|
| 41 |
response += delta
|
| 42 |
yield response
|
| 43 |
|
| 44 |
+
# Если модель вернула команду поиска
|
| 45 |
if response.strip().startswith("#search"):
|
| 46 |
query = response.replace("#search", "").strip()
|
| 47 |
web_context = search_web(query)
|