IvanPSG commited on
Commit
6161aaf
·
verified ·
1 Parent(s): abbab7a

Compatibilidade com GGUF

Browse files
Files changed (1) hide show
  1. app.py +18 -54
app.py CHANGED
@@ -3,11 +3,7 @@ import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
  MODEL_ID = "unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF"
6
-
7
- # Pega o token do secret HF_TOKEN que você adicionou no Space
8
  token = os.environ.get("HF_TOKEN")
9
-
10
- # Inicializa o cliente; se token for None, InferenceClient tentará usar o token local/config.
11
  client = InferenceClient(model=MODEL_ID, token=token)
12
 
13
  def respond(
@@ -18,56 +14,24 @@ def respond(
18
  temperature,
19
  top_p,
20
  ):
21
- messages = [{"role": "system", "content": system_message}]
22
- for val in history:
23
- if val[0]:
24
- messages.append({"role": "user", "content": val[0]})
25
- if val[1]:
26
- messages.append({"role": "assistant", "content": val[1]})
27
- messages.append({"role": "user", "content": message})
28
-
29
- # MODE: escolha "stream_mode = True" para token por token, ou False para resposta completa de uma vez
30
- stream_mode = True
31
-
32
- if stream_mode:
33
- response = ""
34
- # stream=True entrega chunks — iteramos e extraímos 'content' do delta
35
- for chunk in client.chat_completion(
36
- messages,
37
- max_tokens=max_tokens,
38
- stream=True,
39
- temperature=temperature,
40
- top_p=top_p,
41
- ):
42
- # chunk pode ser dataclass/obj ou dict-like; tentamos extrair o texto com segurança
43
- token_piece = ""
44
- try:
45
- delta = chunk.choices[0].delta
46
- if isinstance(delta, dict):
47
- token_piece = delta.get("content", "") or ""
48
- else:
49
- # objeto dataclass-like
50
- token_piece = getattr(delta, "content", "") or ""
51
- except Exception:
52
- # fallback genérico (caso a API retorne formato diferente)
53
- token_piece = str(chunk)
54
-
55
- response += token_piece
56
- yield response
57
-
58
- else:
59
- # Sem streaming: recupera a resposta completa
60
- completion = client.chat_completion(
61
- messages,
62
- max_tokens=max_tokens,
63
- stream=False,
64
- temperature=temperature,
65
- top_p=top_p,
66
- )
67
- # conforme docs, a resposta completa aparece em:
68
- text = completion.choices[0].message.content
69
- yield text
70
-
71
 
72
  demo = gr.ChatInterface(
73
  respond,
 
3
  from huggingface_hub import InferenceClient
4
 
5
  MODEL_ID = "unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF"
 
 
6
  token = os.environ.get("HF_TOKEN")
 
 
7
  client = InferenceClient(model=MODEL_ID, token=token)
8
 
9
  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:
21
+ prompt += f"User: {user_msg}\n"
22
+ if bot_msg:
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,