Ragnarok1988 commited on
Commit
cc78a8c
·
verified ·
1 Parent(s): d046ad1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -31
app.py CHANGED
@@ -2,12 +2,10 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- """
6
- Para obter mais informações sobre o suporte da API de inferência `huggingface_hub`, consulte a documentação:
7
- https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
8
- """
9
-
10
- client = InferenceClient( model="HuggingFaceH4/zephyr-7b-alph", token=os.getenv("HF_TOKEN"))
11
 
12
  def responder(
13
  mensagem,
@@ -17,37 +15,35 @@ def responder(
17
  temperatura,
18
  top_p,
19
  ):
20
- mensagens = [{"role": "system", "content": mensagem_do_sistema}]
21
-
22
- for usuario, bot in historico:
23
- if usuario:
24
- mensagens.append({"role": "user", "content": usuario})
25
- if bot:
26
- mensagens.append({"role": "assistant", "content": bot})
27
-
28
- mensagens.append({"role": "user", "content": mensagem})
29
- resposta = ""
30
 
31
- for resposta_parcial in client.chat_completion(
32
- messages=mensagens,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperatura,
36
- top_p=top_p,
37
- ):
38
- token = resposta_parcial.choices[0].delta.content or ""
39
- resposta += token
40
- yield resposta
 
 
 
 
41
 
42
 
43
- """
44
- Para obter informações sobre como personalizar o ChatInterface, consulte:
45
- https://www.gradio.app/docs/chatinterface
46
- """
47
  demo = gr.ChatInterface(
48
  responder,
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)"),
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ client = InferenceClient(
6
+ model="mistralai/Mistral-7B-Instruct-v0.1",
7
+ token=os.getenv("HF_TOKEN")
8
+ )
 
 
9
 
10
  def responder(
11
  mensagem,
 
15
  temperatura,
16
  top_p,
17
  ):
18
+ # Monta prompt manualmente
19
+ prompt = f"<s>[SYSTEM] {mensagem_do_sistema}\n"
20
+ for user_msg, bot_msg in historico:
21
+ if user_msg:
22
+ prompt += f"[USER] {user_msg}\n"
23
+ if bot_msg:
24
+ prompt += f"[ASSISTANT] {bot_msg}\n"
25
+ prompt += f"[USER] {mensagem}\n[ASSISTANT]"
 
 
26
 
27
+ resposta = ""
28
+ try:
29
+ for resposta_parcial in client.text_generation(
30
+ prompt=prompt,
31
+ max_new_tokens=max_tokens,
32
+ stream=True,
33
+ temperature=temperatura,
34
+ top_p=top_p,
35
+ ):
36
+ token = resposta_parcial.token.text
37
+ resposta += token
38
+ yield resposta
39
+ except Exception as e:
40
+ yield f"[ERRO]: {str(e)}"
41
 
42
 
 
 
 
 
43
  demo = gr.ChatInterface(
44
  responder,
45
  additional_inputs=[
46
+ gr.Textbox(value="Você é um assistente útil.", label="Mensagem do sistema"),
47
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Máximo de novos tokens"),
48
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperatura"),
49
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (amostragem de núcleo)"),