MrAlexGov commited on
Commit
50c1962
·
verified ·
1 Parent(s): 4b38efb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -1,19 +1,14 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- from typing import List, Dict, Any, Tuple # ← + Tuple!
4
 
5
- # 10 моделей
6
  MODELS = [
7
- "microsoft/Phi-3-mini-4k-instruct",
8
- "Qwen/Qwen2.5-0.5B-Instruct",
9
- "Qwen/Qwen2.5-1.5B-Instruct",
10
- "google/gemma-2-2b-it",
11
- "HuggingFaceH4/zephyr-7b-beta",
12
- "mistralai/Mistral-7B-Instruct-v0.3",
13
- "mistralai/Mistral-Nemo-Instruct-2407",
14
- "microsoft/Phi-3.5-mini-instruct",
15
- "NousResearch/Hermes-2-Theta-Llama-3.1-8B",
16
- "cognitivecomputations/dolphin-2.9-llama3-8b"
17
  ]
18
 
19
  def respond(message: str,
@@ -21,8 +16,10 @@ def respond(message: str,
21
  model_id: str,
22
  system_prompt: str,
23
  hf_token: str) -> Tuple[List[Dict[str, str]], str, Dict[str, Any]]:
24
- """HF API с native messages."""
25
  try:
 
 
26
  client = InferenceClient(model=model_id, token=hf_token.strip() or None)
27
 
28
  messages = []
@@ -39,6 +36,7 @@ def respond(message: str,
39
  )
40
 
41
  bot_reply = response.choices[0].message.content
 
42
 
43
  new_history = history + [
44
  {"role": "user", "content": message},
@@ -47,51 +45,48 @@ def respond(message: str,
47
  return new_history, "", gr.update(value="")
48
 
49
  except Exception as e:
50
- error_msg = f"❌ {str(e)}"
 
 
51
  if "429" in str(e) or "rate" in str(e).lower():
52
- error_msg += "\n🔥 Rate limit HF Token!"
53
- elif "token" in str(e).lower():
54
- error_msg += "\n🔑 Проверь токен."
 
 
 
 
55
 
56
  new_history = history + [
57
  {"role": "user", "content": message},
58
- {"role": "assistant", "content": error_msg}
59
  ]
60
- return new_history, error_msg, gr.update(value="")
61
 
62
  # UI
63
- with gr.Blocks(title="🚀 HF Чат-Тестер (готово!)", theme=gr.themes.Soft()) as demo:
64
- gr.Markdown("# Тест HF Inference API\n**Token** 1M tokens/мес с тебя. [Токен](https://huggingface.co/settings/tokens)")
65
 
66
  with gr.Row(variant="compact"):
67
  model_dropdown = gr.Dropdown(choices=MODELS, value=MODELS[0], label="🧠 Модель")
68
- system_prompt = gr.Textbox(label="📝 System", placeholder="Ты ИИ-хакер.", lines=2)
69
  hf_token = gr.Textbox(label="🔑 Token", placeholder="hf_...", type="password")
70
 
71
  chatbot = gr.Chatbot(type="messages", height=500)
72
 
73
  with gr.Row():
74
- msg_input = gr.Textbox(placeholder="Сообщение... (Enter)", scale=7, container=True)
75
  send_btn = gr.Button("📤", variant="primary", scale=1)
76
 
77
  with gr.Row():
78
- clear_btn = gr.Button("🗑️ Очистить")
79
- retry_btn = gr.Button("🔄 Повторить")
80
 
81
- status = gr.Textbox(label="Статус", interactive=False)
82
 
83
  # Events
84
- send_btn.click(
85
- fn=respond,
86
- inputs=[msg_input, chatbot, model_dropdown, system_prompt, hf_token],
87
- outputs=[chatbot, status, msg_input]
88
- )
89
-
90
- msg_input.submit(
91
- fn=respond,
92
- inputs=[msg_input, chatbot, model_dropdown, system_prompt, hf_token],
93
- outputs=[chatbot, status, msg_input]
94
- )
95
 
96
  def clear():
97
  return [], "", gr.update(value="")
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from typing import List, Dict, Any, Tuple
4
 
5
+ # ТОП-5 MODELS (chat-ready, быстрые, HF-tested)
6
  MODELS = [
7
+ "Qwen/Qwen2.5-0.5B-Instruct", # ⚡ Супер-быстрая (0.5B)
8
+ "Qwen/Qwen2.5-1.5B-Instruct", # Быстрая (1.5B)
9
+ "microsoft/Phi-3-mini-4k-instruct", # Надёжная Phi-3
10
+ "mistralai/Mistral-7B-Instruct-v0.3", # Классика
11
+ "HuggingFaceH4/zephyr-7b-beta" # Zephyr (chat-tuned)
 
 
 
 
 
12
  ]
13
 
14
  def respond(message: str,
 
16
  model_id: str,
17
  system_prompt: str,
18
  hf_token: str) -> Tuple[List[Dict[str, str]], str, Dict[str, Any]]:
19
+ """HF API с debug."""
20
  try:
21
+ print(f"🚀 Запрос: Model={model_id}, Token={'Yes' if hf_token else 'No'}, Msg='{message[:50]}...'") # Лог HF
22
+
23
  client = InferenceClient(model=model_id, token=hf_token.strip() or None)
24
 
25
  messages = []
 
36
  )
37
 
38
  bot_reply = response.choices[0].message.content
39
+ print(f"✅ Ответ: {bot_reply[:50]}...") # Лог успеха
40
 
41
  new_history = history + [
42
  {"role": "user", "content": message},
 
45
  return new_history, "", gr.update(value="")
46
 
47
  except Exception as e:
48
+ full_error = f"❌ Model: {model_id}\nError: {str(e)}\n"
49
+ print(f"💥 ERROR: {full_error}") # Лог HF
50
+
51
  if "429" in str(e) or "rate" in str(e).lower():
52
+ full_error += "🔥 RATE LIMIT! Вставь HF Token (huggingface.co/settings/tokens)."
53
+ elif "token" in str(e).lower() or "Unauthorized" in str(e):
54
+ full_error += "🔑 Неверный/нет Token. Создай новый."
55
+ elif "No chat template" in str(e) or "tokenizer" in str(e):
56
+ full_error += "🧠 Модель не chat-ready. Выбери другую (Qwen/Phi)."
57
+ else:
58
+ full_error += "🌐 HF API глючит. Попробуй Token или другую модель."
59
 
60
  new_history = history + [
61
  {"role": "user", "content": message},
62
+ {"role": "assistant", "content": full_error}
63
  ]
64
+ return new_history, full_error, gr.update(value="")
65
 
66
  # UI
67
+ with gr.Blocks(title="🚀 HF Чат (debug!)", theme=gr.themes.Soft()) as demo:
68
+ gr.Markdown("# Тест HF API\n**1. Выбери Qwen2.5-0.5B** ⚡\n**2. Вставь Token** Нет лимитов\n[Token](https://huggingface.co/settings/tokens)")
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
  chatbot = gr.Chatbot(type="messages", height=500)
76
 
77
  with gr.Row():
78
+ msg_input = gr.Textbox(placeholder="Привет! (Enter)", scale=7)
79
  send_btn = gr.Button("📤", variant="primary", scale=1)
80
 
81
  with gr.Row():
82
+ clear_btn = gr.Button("🗑️ Clear")
83
+ retry_btn = gr.Button("🔄 Retry")
84
 
85
+ status = gr.Textbox(label="Логи/Статус", interactive=False, lines=3)
86
 
87
  # Events
88
+ send_btn.click(fn=respond, inputs=[msg_input, chatbot, model_dropdown, system_prompt, hf_token], outputs=[chatbot, status, msg_input])
89
+ msg_input.submit(fn=respond, inputs=[msg_input, chatbot, model_dropdown, system_prompt, hf_token], outputs=[chatbot, status, msg_input])
 
 
 
 
 
 
 
 
 
90
 
91
  def clear():
92
  return [], "", gr.update(value="")