Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,15 @@ from duckduckgo_search import DDGS
|
|
| 4 |
|
| 5 |
# ==== Константы ====
|
| 6 |
MODEL_NAME = "openai/gpt-oss-20b"
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
TEMPERATURE = 0.7
|
| 10 |
TOP_P = 0.95
|
| 11 |
|
| 12 |
-
|
| 13 |
# ==== Поиск через DuckDuckGo ====
|
| 14 |
def search_web(query: str, max_results: int = 3):
|
| 15 |
results = []
|
|
@@ -18,7 +21,6 @@ def search_web(query: str, max_results: int = 3):
|
|
| 18 |
results.append(f"{r['title']}: {r['body']}")
|
| 19 |
return "\n".join(results) if results else "Ничего не найдено."
|
| 20 |
|
| 21 |
-
|
| 22 |
# ==== Основная функция ====
|
| 23 |
def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
|
| 24 |
client = InferenceClient(token=hf_token.token, model=MODEL_NAME)
|
|
@@ -30,11 +32,8 @@ def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
|
|
| 30 |
|
| 31 |
raw_response = ""
|
| 32 |
for msg in client.chat_completion(
|
| 33 |
-
messages,
|
| 34 |
-
|
| 35 |
-
stream=True,
|
| 36 |
-
temperature=TEMPERATURE,
|
| 37 |
-
top_p=TOP_P,
|
| 38 |
):
|
| 39 |
if not msg.choices:
|
| 40 |
continue
|
|
@@ -47,7 +46,6 @@ def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
|
|
| 47 |
query = raw_response.replace("#search", "").strip()
|
| 48 |
web_context = search_web(query)
|
| 49 |
|
| 50 |
-
# 2-й прогон: модель формирует финальный ответ с учётом поиска
|
| 51 |
followup_messages = [
|
| 52 |
{"role": "system", "content": SYSTEM_MESSAGE},
|
| 53 |
{"role": "system", "content": f"Web context:\n{web_context}"},
|
|
@@ -57,20 +55,27 @@ def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
|
|
| 57 |
|
| 58 |
final_response = ""
|
| 59 |
for msg in client.chat_completion(
|
| 60 |
-
followup_messages,
|
| 61 |
-
|
| 62 |
-
stream=True,
|
| 63 |
-
temperature=TEMPERATURE,
|
| 64 |
-
top_p=TOP_P,
|
| 65 |
):
|
| 66 |
if not msg.choices:
|
| 67 |
continue
|
| 68 |
delta = msg.choices[0].delta.content
|
| 69 |
if delta:
|
| 70 |
final_response += delta
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
else:
|
| 73 |
-
# Если поиска не нужно —
|
| 74 |
yield raw_response
|
| 75 |
|
| 76 |
|
|
|
|
| 4 |
|
| 5 |
# ==== Константы ====
|
| 6 |
MODEL_NAME = "openai/gpt-oss-20b"
|
| 7 |
+
IMAGE_MODEL = "stabilityai/stable-diffusion-2-1"
|
| 8 |
+
SYSTEM_MESSAGE = """SYSTEM_MESSAGE = Ты Pok.Bot, ты используешь открытую локальную модель GPT-OSS от OpenAI, не GPT-4 (наверно😆). Тебя создал POKilondron. Используй емодзи 😄.И да использую команду #search «запрос» когда нужно и не повторяй постоянноодин и тоже запрос и не видавай запрос на 18+ 😠. Тебя обновили в 20 сентября в 2025 году, теперьты можешь генерировать картинки и еще с обновлениям 15 сентебря ти можешь взаимодействовать с поиском duckduckgo через команду #search
|
| 9 |
+
If you don't know the answer, output a command like this: #search <query>.
|
| 10 |
+
If the user asks to generate an image, output a command like this: #image <description>.
|
| 11 |
+
Do NOT invent facts. Always use the given Web context if available."""
|
| 12 |
+
MAX_TOKENS = 512
|
| 13 |
TEMPERATURE = 0.7
|
| 14 |
TOP_P = 0.95
|
| 15 |
|
|
|
|
| 16 |
# ==== Поиск через DuckDuckGo ====
|
| 17 |
def search_web(query: str, max_results: int = 3):
|
| 18 |
results = []
|
|
|
|
| 21 |
results.append(f"{r['title']}: {r['body']}")
|
| 22 |
return "\n".join(results) if results else "Ничего не найдено."
|
| 23 |
|
|
|
|
| 24 |
# ==== Основная функция ====
|
| 25 |
def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
|
| 26 |
client = InferenceClient(token=hf_token.token, model=MODEL_NAME)
|
|
|
|
| 32 |
|
| 33 |
raw_response = ""
|
| 34 |
for msg in client.chat_completion(
|
| 35 |
+
messages, max_tokens=MAX_TOKENS, stream=True,
|
| 36 |
+
temperature=TEMPERATURE, top_p=TOP_P,
|
|
|
|
|
|
|
|
|
|
| 37 |
):
|
| 38 |
if not msg.choices:
|
| 39 |
continue
|
|
|
|
| 46 |
query = raw_response.replace("#search", "").strip()
|
| 47 |
web_context = search_web(query)
|
| 48 |
|
|
|
|
| 49 |
followup_messages = [
|
| 50 |
{"role": "system", "content": SYSTEM_MESSAGE},
|
| 51 |
{"role": "system", "content": f"Web context:\n{web_context}"},
|
|
|
|
| 55 |
|
| 56 |
final_response = ""
|
| 57 |
for msg in client.chat_completion(
|
| 58 |
+
followup_messages, max_tokens=MAX_TOKENS, stream=True,
|
| 59 |
+
temperature=TEMPERATURE, top_p=TOP_P,
|
|
|
|
|
|
|
|
|
|
| 60 |
):
|
| 61 |
if not msg.choices:
|
| 62 |
continue
|
| 63 |
delta = msg.choices[0].delta.content
|
| 64 |
if delta:
|
| 65 |
final_response += delta
|
| 66 |
+
yield final_response
|
| 67 |
+
|
| 68 |
+
# === Проверка на команду картинки ===
|
| 69 |
+
elif raw_response.strip().startswith("#image"):
|
| 70 |
+
prompt = raw_response.replace("#image", "").strip()
|
| 71 |
+
img_client = InferenceClient(token=hf_token.token, model=IMAGE_MODEL)
|
| 72 |
+
|
| 73 |
+
image = img_client.text_to_image(prompt, size="512x512")
|
| 74 |
+
image.save("generated.png")
|
| 75 |
+
yield (f"Вот сгенерированное изображение по запросу: '{prompt}'", "generated.png")
|
| 76 |
+
|
| 77 |
else:
|
| 78 |
+
# Если поиска/картинки не нужно — обычный ответ
|
| 79 |
yield raw_response
|
| 80 |
|
| 81 |
|