Spaces:
Sleeping
Sleeping
Compatibilidade com GGUF
Browse files
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 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
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,
|