Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,61 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
max_tokens,
|
| 9 |
-
|
| 10 |
top_p,
|
| 11 |
-
hf_token
|
| 12 |
):
|
| 13 |
"""
|
| 14 |
-
Função que envia mensagens para o modelo Meta-LLaMA 3.1 8B Instruct
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="meta-llama/Meta-Llama-3.1-8B-Instruct")
|
| 18 |
|
| 19 |
# Prepara mensagens no formato chat
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
# Streaming da resposta token por token
|
| 27 |
-
for
|
| 28 |
-
|
| 29 |
max_tokens=max_tokens,
|
| 30 |
stream=True,
|
| 31 |
-
temperature=
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
-
|
| 35 |
token = ""
|
| 36 |
-
if len(
|
| 37 |
-
token =
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield
|
| 41 |
|
| 42 |
# Cria interface de chat Gradio
|
| 43 |
chatbot = gr.ChatInterface(
|
| 44 |
-
|
| 45 |
type="messages",
|
| 46 |
additional_inputs=[
|
| 47 |
-
gr.Textbox(value="
|
| 48 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="
|
| 49 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (
|
| 51 |
],
|
| 52 |
)
|
| 53 |
|
| 54 |
-
# Layout
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
-
with gr.Sidebar():
|
| 57 |
-
gr.LoginButton()
|
| 58 |
chatbot.render()
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Coloque seu token Hugging Face aqui (ou use variável de ambiente)
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") or "SEU_TOKEN_AQUI"
|
| 7 |
+
|
| 8 |
+
def responder(
|
| 9 |
+
mensagem,
|
| 10 |
+
historico: list[dict[str, str]],
|
| 11 |
+
mensagem_do_sistema,
|
| 12 |
max_tokens,
|
| 13 |
+
temperatura,
|
| 14 |
top_p,
|
| 15 |
+
hf_token=None, # Ignorado, usamos HF_TOKEN direto
|
| 16 |
):
|
| 17 |
"""
|
| 18 |
+
Função que envia mensagens para o modelo Meta-LLaMA 3.1 8B Instruct usando token direto.
|
| 19 |
"""
|
| 20 |
+
cliente = InferenceClient(token=HF_TOKEN, model="meta-llama/Meta-Llama-3.1-8B-Instruct")
|
|
|
|
| 21 |
|
| 22 |
# Prepara mensagens no formato chat
|
| 23 |
+
mensagens = [{"role": "system", "content": mensagem_do_sistema}]
|
| 24 |
+
mensagens.extend(historico)
|
| 25 |
+
mensagens.append({"role": "user", "content": mensagem})
|
| 26 |
|
| 27 |
+
resposta = ""
|
| 28 |
|
| 29 |
# Streaming da resposta token por token
|
| 30 |
+
for trecho_da_mensagem in cliente.chat_completion(
|
| 31 |
+
mensagens,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
stream=True,
|
| 34 |
+
temperature=temperatura,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
+
escolhas = trecho_da_mensagem.choices
|
| 38 |
token = ""
|
| 39 |
+
if len(escolhas) and escolhas[0].delta.content:
|
| 40 |
+
token = escolhas[0].delta.content
|
| 41 |
|
| 42 |
+
resposta += token
|
| 43 |
+
yield resposta
|
| 44 |
|
| 45 |
# Cria interface de chat Gradio
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
+
responder,
|
| 48 |
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
+
gr.Textbox(value="Você é um chatbot amigável.", label="Mensagem do sistema"),
|
| 51 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Máximo de novos tokens"),
|
| 52 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperatura"),
|
| 53 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (amostragem de núcleo)"),
|
| 54 |
],
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Layout simples, sem login
|
| 58 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 59 |
chatbot.render()
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|