mariusjabami commited on
Commit
1a3bdf2
verified
1 Parent(s): a17afb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -40
app.py CHANGED
@@ -3,50 +3,39 @@ from huggingface_hub import InferenceClient
3
  from dotenv import load_dotenv
4
  import os
5
 
6
-
7
  load_dotenv()
8
- token=os.getenv('TOKEN')
9
- def respond(
10
- message,
11
- history: list[dict[str, str]],
12
- system_message,
13
- max_tokens,
14
- temperature,
15
- top_p,
16
- hf_token: token,
17
- ):
18
- """
19
- 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
20
- """
21
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
22
-
23
- messages = [{"role": "system", "content": system_message}]
24
 
25
- messages.extend(history)
 
 
 
 
 
26
 
 
 
 
27
  messages.append({"role": "user", "content": message})
28
 
29
  response = ""
30
 
31
- for message in client.chat_completion(
32
- messages,
 
33
  max_tokens=max_tokens,
34
  stream=True,
35
  temperature=temperature,
36
  top_p=top_p,
37
  ):
38
- choices = message.choices
39
- token = ""
40
- if len(choices) and choices[0].delta.content:
41
- token = choices[0].delta.content
42
-
43
- response += token
44
- yield response
45
 
46
 
47
- """
48
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
49
- """
50
  chatbot = gr.ChatInterface(
51
  respond,
52
  type="messages",
@@ -54,21 +43,15 @@ chatbot = gr.ChatInterface(
54
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
55
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
56
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
57
- gr.Slider(
58
- minimum=0.1,
59
- maximum=1.0,
60
- value=0.95,
61
- step=0.05,
62
- label="Top-p (nucleus sampling)",
63
- ),
64
  ],
65
  )
66
 
 
67
  with gr.Blocks() as demo:
68
  with gr.Sidebar():
69
- gr.LoginButton()
70
  chatbot.render()
71
 
72
-
73
  if __name__ == "__main__":
74
- demo.launch()
 
3
  from dotenv import load_dotenv
4
  import os
5
 
6
+ # carregar vari谩veis de ambiente
7
  load_dotenv()
8
+ hf_token = os.getenv("TOKEN")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
11
+ # criar cliente HF
12
+ client = InferenceClient(
13
+ model="lxcorp/WNL468M",
14
+ token=hf_token
15
+ )
16
 
17
+ # mensagens no formato esperado
18
+ messages = [{"role": "system", "content": system_message}]
19
+ messages.extend([{"role": "user", "content": turn[0]} for turn in history])
20
  messages.append({"role": "user", "content": message})
21
 
22
  response = ""
23
 
24
+ # streaming da resposta
25
+ for msg in client.chat_completion(
26
+ messages=messages,
27
  max_tokens=max_tokens,
28
  stream=True,
29
  temperature=temperature,
30
  top_p=top_p,
31
  ):
32
+ if msg.choices and msg.choices[0].delta.get("content"):
33
+ token = msg.choices[0].delta["content"]
34
+ response += token
35
+ yield response
 
 
 
36
 
37
 
38
+ # interface
 
 
39
  chatbot = gr.ChatInterface(
40
  respond,
41
  type="messages",
 
43
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
44
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
45
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
46
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
 
 
 
 
 
 
47
  ],
48
  )
49
 
50
+ # com sidebar (opcional)
51
  with gr.Blocks() as demo:
52
  with gr.Sidebar():
53
+ gr.Markdown("### Hugging Face Chatbot")
54
  chatbot.render()
55
 
 
56
  if __name__ == "__main__":
57
+ demo.launch()