Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
from typing import List,
|
| 4 |
|
| 5 |
-
# 10 моделей (instruct
|
| 6 |
MODELS = [
|
| 7 |
"microsoft/Phi-3-mini-4k-instruct",
|
| 8 |
"Qwen/Qwen2.5-0.5B-Instruct",
|
|
@@ -16,30 +16,23 @@ MODELS = [
|
|
| 16 |
"cognitivecomputations/dolphin-2.9-llama3-8b"
|
| 17 |
]
|
| 18 |
|
| 19 |
-
def convert_history(history: List[Tuple[str, str]]) -> List[dict]:
|
| 20 |
-
"""Gradio tuples -> OpenAI messages."""
|
| 21 |
-
messages = []
|
| 22 |
-
for user_msg, bot_msg in history:
|
| 23 |
-
messages.append({"role": "user", "content": user_msg})
|
| 24 |
-
if bot_msg:
|
| 25 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 26 |
-
return messages
|
| 27 |
-
|
| 28 |
def respond(message: str,
|
| 29 |
-
history: List[
|
| 30 |
model_id: str,
|
| 31 |
system_prompt: str,
|
| 32 |
-
hf_token: str) -> Tuple[List[
|
| 33 |
-
"""HF API
|
| 34 |
try:
|
| 35 |
client = InferenceClient(model=model_id, token=hf_token.strip() or None)
|
| 36 |
|
|
|
|
| 37 |
messages = []
|
| 38 |
if system_prompt.strip():
|
| 39 |
messages.append({"role": "system", "content": system_prompt})
|
| 40 |
-
messages.extend(
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
|
|
|
|
| 43 |
response = client.chat_completion(
|
| 44 |
messages=messages,
|
| 45 |
max_tokens=512,
|
|
@@ -48,66 +41,72 @@ def respond(message: str,
|
|
| 48 |
)
|
| 49 |
|
| 50 |
bot_reply = response.choices[0].message.content
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
error_msg = f"❌
|
| 56 |
-
if "429" in str(e) or "rate
|
| 57 |
-
error_msg += "🔥 Rate limit
|
| 58 |
-
elif "token" in str(e).lower()
|
| 59 |
-
error_msg += "🔑 Проверь
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
# UI
|
| 64 |
-
with gr.Blocks(title="
|
| 65 |
-
gr.Markdown("#
|
| 66 |
-
gr.Markdown("[Токен](https://huggingface.co/settings/tokens) | [Usage](https://huggingface.co/settings/billing)")
|
| 67 |
|
| 68 |
# Контролы ВЕРХОМ
|
| 69 |
with gr.Row(variant="compact"):
|
| 70 |
model_dropdown = gr.Dropdown(choices=MODELS, value=MODELS[0], label="🧠 Модель")
|
| 71 |
-
system_prompt = gr.Textbox(label="📝 System
|
| 72 |
-
hf_token = gr.Textbox(label="🔑
|
| 73 |
|
| 74 |
-
# Чат (
|
| 75 |
-
chatbot = gr.Chatbot(type="
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
-
send_btn = gr.Button("📤 Отправить", variant="primary")
|
| 80 |
clear_btn = gr.Button("🗑️ Очистить")
|
| 81 |
retry_btn = gr.Button("🔄 Повторить")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# Send (клик + Enter)
|
| 86 |
-
def send_message(msg, hist, model, sys, token):
|
| 87 |
-
return respond(msg, hist, model, sys, token) # hist — list из State
|
| 88 |
|
|
|
|
| 89 |
send_btn.click(
|
| 90 |
-
fn=
|
| 91 |
-
inputs=[msg_input,
|
| 92 |
-
outputs=[chatbot,
|
| 93 |
-
)
|
| 94 |
|
| 95 |
msg_input.submit(
|
| 96 |
-
fn=
|
| 97 |
-
inputs=[msg_input,
|
| 98 |
-
outputs=[chatbot,
|
| 99 |
-
)
|
| 100 |
|
| 101 |
# Кнопки
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
fn=lambda hist: hist[-1][0] if hist else "",
|
| 106 |
-
inputs=[history_state],
|
| 107 |
-
outputs=[msg_input]
|
| 108 |
-
)
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
demo.queue(max_size=20).launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from typing import List, Dict, Any
|
| 4 |
|
| 5 |
+
# 10 моделей (instruct)
|
| 6 |
MODELS = [
|
| 7 |
"microsoft/Phi-3-mini-4k-instruct",
|
| 8 |
"Qwen/Qwen2.5-0.5B-Instruct",
|
|
|
|
| 16 |
"cognitivecomputations/dolphin-2.9-llama3-8b"
|
| 17 |
]
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def respond(message: str,
|
| 20 |
+
history: List[Dict[str, str]], # Native messages от Chatbot
|
| 21 |
model_id: str,
|
| 22 |
system_prompt: str,
|
| 23 |
+
hf_token: str) -> Tuple[List[Dict[str, str]], str, Dict[str, Any]]:
|
| 24 |
+
"""HF API. History — List[dict] → append user/assistant."""
|
| 25 |
try:
|
| 26 |
client = InferenceClient(model=model_id, token=hf_token.strip() or None)
|
| 27 |
|
| 28 |
+
# messages = system + history + new user
|
| 29 |
messages = []
|
| 30 |
if system_prompt.strip():
|
| 31 |
messages.append({"role": "system", "content": system_prompt})
|
| 32 |
+
messages.extend(history)
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
+
# API call
|
| 36 |
response = client.chat_completion(
|
| 37 |
messages=messages,
|
| 38 |
max_tokens=512,
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
bot_reply = response.choices[0].message.content
|
| 44 |
+
|
| 45 |
+
# New history: + user + assistant
|
| 46 |
+
new_history = history + [
|
| 47 |
+
{"role": "user", "content": message},
|
| 48 |
+
{"role": "assistant", "content": bot_reply}
|
| 49 |
+
]
|
| 50 |
+
return new_history, "", gr.update(value="") # Clear input
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
+
error_msg = f"❌ {str(e)}"
|
| 54 |
+
if "429" in str(e) or "rate" in str(e).lower():
|
| 55 |
+
error_msg += "\n🔥 Rate limit → Вставь HF Token!"
|
| 56 |
+
elif "token" in str(e).lower():
|
| 57 |
+
error_msg += "\n🔑 Проверь токен."
|
| 58 |
+
|
| 59 |
+
new_history = history + [
|
| 60 |
+
{"role": "user", "content": message},
|
| 61 |
+
{"role": "assistant", "content": error_msg}
|
| 62 |
+
]
|
| 63 |
+
return new_history, error_msg, gr.update(value="")
|
| 64 |
|
| 65 |
+
# UI (без State, native messages)
|
| 66 |
+
with gr.Blocks(title="🚀 HF Чат-Тестер (финал!)", theme=gr.themes.Soft()) as demo:
|
| 67 |
+
gr.Markdown("# Тест HF Inference API\n**HF Token** → твои лимиты 1M tokens/мес. [Токен](https://huggingface.co/settings/tokens)")
|
|
|
|
| 68 |
|
| 69 |
# Контролы ВЕРХОМ
|
| 70 |
with gr.Row(variant="compact"):
|
| 71 |
model_dropdown = gr.Dropdown(choices=MODELS, value=MODELS[0], label="🧠 Модель")
|
| 72 |
+
system_prompt = gr.Textbox(label="📝 System", placeholder="Ты хакер ИИ.", lines=2)
|
| 73 |
+
hf_token = gr.Textbox(label="🔑 Token", placeholder="hf_...", type="password")
|
| 74 |
|
| 75 |
+
# Чат (messages — 0 warning!)
|
| 76 |
+
chatbot = gr.Chatbot(type="messages", height=500)
|
| 77 |
+
|
| 78 |
+
# Input + кнопки
|
| 79 |
+
with gr.Row():
|
| 80 |
+
msg_input = gr.Textbox(placeholder="Сообщение... (Enter)", scale=7, container=True)
|
| 81 |
+
send_btn = gr.Button("📤", variant="primary", scale=1)
|
| 82 |
|
| 83 |
with gr.Row():
|
|
|
|
| 84 |
clear_btn = gr.Button("🗑️ Очистить")
|
| 85 |
retry_btn = gr.Button("🔄 Повторить")
|
| 86 |
|
| 87 |
+
status = gr.Textbox(label="Статус", interactive=False) # Для ошибок
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Send (клик + Enter) — outputs: chatbot, status, msg_input
|
| 90 |
send_btn.click(
|
| 91 |
+
fn=respond,
|
| 92 |
+
inputs=[msg_input, chatbot, model_dropdown, system_prompt, hf_token],
|
| 93 |
+
outputs=[chatbot, status, msg_input]
|
| 94 |
+
)
|
| 95 |
|
| 96 |
msg_input.submit(
|
| 97 |
+
fn=respond,
|
| 98 |
+
inputs=[msg_input, chatbot, model_dropdown, system_prompt, hf_token],
|
| 99 |
+
outputs=[chatbot, status, msg_input]
|
| 100 |
+
)
|
| 101 |
|
| 102 |
# Кнопки
|
| 103 |
+
def clear():
|
| 104 |
+
return [], "", gr.update(value="")
|
| 105 |
+
clear_btn.click(clear, outputs=[chatbot, status, msg_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
def retry(history):
|
| 108 |
+
return history[-2]["content"] if len(history) >= 2 else "" # Последний user msg
|
| 109 |
+
retry_btn.click(retry, inputs=[chatbot], outputs=[msg_input])
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
demo.queue(max_size=20).launch(debug=True)
|