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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -1,8 +1,8 @@
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",
@@ -17,22 +17,20 @@ MODELS = [
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,
@@ -42,17 +40,16 @@ def respond(message: str,
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
 
@@ -62,20 +59,17 @@ def respond(message: str,
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)
@@ -84,9 +78,9 @@ with gr.Blocks(title="🚀 HF Чат-Тестер (финал!)", theme=gr.theme
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],
@@ -99,13 +93,14 @@ with gr.Blocks(title="🚀 HF Чат-Тестер (финал!)", theme=gr.theme
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__":
 
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",
 
17
  ]
18
 
19
  def respond(message: str,
20
+ history: List[Dict[str, 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 = []
29
  if system_prompt.strip():
30
  messages.append({"role": "system", "content": system_prompt})
31
  messages.extend(history)
32
  messages.append({"role": "user", "content": message})
33
 
 
34
  response = client.chat_completion(
35
  messages=messages,
36
  max_tokens=512,
 
40
 
41
  bot_reply = response.choices[0].message.content
42
 
 
43
  new_history = history + [
44
  {"role": "user", "content": message},
45
  {"role": "assistant", "content": bot_reply}
46
  ]
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
 
 
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)
 
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],
 
93
  outputs=[chatbot, status, msg_input]
94
  )
95
 
 
96
  def clear():
97
  return [], "", gr.update(value="")
98
  clear_btn.click(clear, outputs=[chatbot, status, msg_input])
99
 
100
  def retry(history):
101
+ if len(history) >= 2 and history[-2]["role"] == "user":
102
+ return history[-2]["content"]
103
+ return ""
104
  retry_btn.click(retry, inputs=[chatbot], outputs=[msg_input])
105
 
106
  if __name__ == "__main__":