Spaces:
Sleeping
Sleeping
Autenticação com token
Browse files
app.py
CHANGED
@@ -1,12 +1,14 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
def respond(
|
12 |
message,
|
@@ -17,49 +19,65 @@ def respond(
|
|
17 |
top_p,
|
18 |
):
|
19 |
messages = [{"role": "system", "content": system_message}]
|
20 |
-
|
21 |
for val in history:
|
22 |
if val[0]:
|
23 |
messages.append({"role": "user", "content": val[0]})
|
24 |
if val[1]:
|
25 |
messages.append({"role": "assistant", "content": val[1]})
|
26 |
-
|
27 |
messages.append({"role": "user", "content": message})
|
28 |
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
|
44 |
-
"""
|
45 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
46 |
-
"""
|
47 |
demo = gr.ChatInterface(
|
48 |
respond,
|
49 |
additional_inputs=[
|
50 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
53 |
-
gr.Slider(
|
54 |
-
minimum=0.1,
|
55 |
-
maximum=1.0,
|
56 |
-
value=0.95,
|
57 |
-
step=0.05,
|
58 |
-
label="Top-p (nucleus sampling)",
|
59 |
-
),
|
60 |
],
|
61 |
)
|
62 |
|
63 |
-
|
64 |
if __name__ == "__main__":
|
65 |
demo.launch()
|
|
|
1 |
+
import os
|
2 |
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(
|
14 |
message,
|
|
|
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,
|
74 |
additional_inputs=[
|
75 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
76 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
77 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
78 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
],
|
80 |
)
|
81 |
|
|
|
82 |
if __name__ == "__main__":
|
83 |
demo.launch()
|