victor422 commited on
Commit
6fee99b
·
verified ·
1 Parent(s): c2afbff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -1,60 +1,61 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- def respond(
5
- message,
6
- history: list[dict[str, str]],
7
- system_message,
 
 
 
8
  max_tokens,
9
- temperature,
10
  top_p,
11
- hf_token: gr.OAuthToken,
12
  ):
13
  """
14
- Função que envia mensagens para o modelo Meta-LLaMA 3.1 8B Instruct via Hugging Face Inference API.
15
  """
16
- # Inicializa o cliente Hugging Face com o modelo Meta-LLaMA 3.1 8B Instruct
17
- client = InferenceClient(token=hf_token.token, model="meta-llama/Meta-Llama-3.1-8B-Instruct")
18
 
19
  # Prepara mensagens no formato chat
20
- messages = [{"role": "system", "content": system_message}]
21
- messages.extend(history)
22
- messages.append({"role": "user", "content": message})
23
 
24
- response = ""
25
 
26
  # Streaming da resposta token por token
27
- for message_chunk in client.chat_completion(
28
- messages,
29
  max_tokens=max_tokens,
30
  stream=True,
31
- temperature=temperature,
32
  top_p=top_p,
33
  ):
34
- choices = message_chunk.choices
35
  token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
42
  # Cria interface de chat Gradio
43
  chatbot = gr.ChatInterface(
44
- respond,
45
  type="messages",
46
  additional_inputs=[
47
- gr.Textbox(value="You are a friendly chatbot.", label="System message"),
48
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
49
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
50
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
51
  ],
52
  )
53
 
54
- # Layout com login Hugging Face
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__":