IvanPSG commited on
Commit
b605fd6
·
verified ·
1 Parent(s): 173c735

Alteração modelo...

Browse files
Files changed (1) hide show
  1. app.py +88 -14
app.py CHANGED
@@ -1,11 +1,67 @@
1
  import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
 
4
 
5
- MODEL_ID = "unsloth/Qwen3-30B-A3B-GGUF"
 
 
 
6
  token = os.environ.get("HF_TOKEN")
 
 
7
  client = InferenceClient(model=MODEL_ID, token=token)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def respond(
10
  message,
11
  history: list[tuple[str, str]],
@@ -14,7 +70,12 @@ def respond(
14
  temperature,
15
  top_p,
16
  ):
17
- # Monta o prompt tipo chat manualmente
 
 
 
 
 
18
  prompt = f"{system_message}\n\n"
19
  for user_msg, bot_msg in history:
20
  if user_msg:
@@ -23,24 +84,37 @@ def respond(
23
  prompt += f"Assistant: {bot_msg}\n"
24
  prompt += f"User: {message}\nAssistant:"
25
 
26
- # Chama o endpoint de geração de texto normal, sem streaming
27
- response = client.text_generation(
28
- prompt,
29
- max_new_tokens=max_tokens,
30
- temperature=temperature,
31
- top_p=top_p,
32
- )
33
- # A resposta vem como string simples
34
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  demo = gr.ChatInterface(
37
  respond,
38
  additional_inputs=[
39
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
40
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
41
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
42
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
43
  ],
 
44
  )
45
 
46
  if __name__ == "__main__":
 
1
  import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
+ from huggingface_hub.utils import HfHubHTTPError
5
 
6
+ # Modelo Mistral Instruct disponível no Hub
7
+ MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
8
+
9
+ # token vindo do secret HF_TOKEN do Space (ou env local)
10
  token = os.environ.get("HF_TOKEN")
11
+
12
+ # Cliente (se token for None, o client tenta usar config local)
13
  client = InferenceClient(model=MODEL_ID, token=token)
14
 
15
+ def _extract_text_from_response(resp):
16
+ """
17
+ Tenta extrair texto de várias possíveis formas de retorno da API.
18
+ Retorna string sempre.
19
+ """
20
+ # string direta
21
+ if isinstance(resp, str):
22
+ return resp
23
+
24
+ # dataclass-like (possível)
25
+ try:
26
+ # alguns SDKs retornam objeto com atributo 'generated_text' ou 'text'
27
+ if hasattr(resp, "generated_text"):
28
+ return getattr(resp, "generated_text") or ""
29
+ if hasattr(resp, "text"):
30
+ return getattr(resp, "text") or ""
31
+ except Exception:
32
+ pass
33
+
34
+ # dict-like formas comuns
35
+ if isinstance(resp, dict):
36
+ # chaves óbvias
37
+ for key in ("generated_text", "generated_texts", "text", "output_text", "result"):
38
+ if key in resp:
39
+ v = resp[key]
40
+ if isinstance(v, list) and v:
41
+ return v[0] if isinstance(v[0], str) else str(v[0])
42
+ if isinstance(v, str):
43
+ return v
44
+
45
+ # choices -> message -> content (formato chat-like)
46
+ if "choices" in resp and isinstance(resp["choices"], list) and resp["choices"]:
47
+ first = resp["choices"][0]
48
+ if isinstance(first, dict):
49
+ # try message.content
50
+ if "message" in first and isinstance(first["message"], dict) and "content" in first["message"]:
51
+ maybe = first["message"]["content"]
52
+ if isinstance(maybe, str):
53
+ return maybe
54
+ # try text or content directly
55
+ for k in ("text", "content", "generated_text"):
56
+ if k in first and isinstance(first[k], str):
57
+ return first[k]
58
+
59
+ # fallback
60
+ try:
61
+ return str(resp)
62
+ except Exception:
63
+ return "<unable to decode response>"
64
+
65
  def respond(
66
  message,
67
  history: list[tuple[str, str]],
 
70
  temperature,
71
  top_p,
72
  ):
73
+ # valida token
74
+ if not token:
75
+ yield "ERRO: variável de ambiente HF_TOKEN não encontrada. Adicione um secret HF_TOKEN no Settings do Space."
76
+ return
77
+
78
+ # monta prompt estilo chat (simples)
79
  prompt = f"{system_message}\n\n"
80
  for user_msg, bot_msg in history:
81
  if user_msg:
 
84
  prompt += f"Assistant: {bot_msg}\n"
85
  prompt += f"User: {message}\nAssistant:"
86
 
87
+ try:
88
+ # chamada sem streaming (resposta completa)
89
+ out = client.text_generation(
90
+ prompt,
91
+ max_new_tokens=int(max_tokens),
92
+ temperature=float(temperature),
93
+ top_p=float(top_p),
94
+ do_sample=True,
95
+ )
96
+ except HfHubHTTPError as e:
97
+ # captura erros HTTP da Hugging Face e retorna mensagem legível
98
+ yield f"ERRO na chamada de inferência: {e}\n(verifique HF_TOKEN, permissões e se o modelo está disponível via Inference API)"
99
+ return
100
+ except Exception as e:
101
+ yield f"Erro inesperado ao chamar a API: {e}"
102
+ return
103
+
104
+ # extrai texto (robusto a vários formatos de retorno)
105
+ text = _extract_text_from_response(out)
106
+ yield text
107
+
108
 
109
  demo = gr.ChatInterface(
110
  respond,
111
  additional_inputs=[
112
+ gr.Textbox(value="You are a helpful assistant.", label="System message"),
113
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
114
+ gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.05, label="Temperature"),
115
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
116
  ],
117
+ title="Chat com Mistral-7B",
118
  )
119
 
120
  if __name__ == "__main__":