Erikaww commited on
Commit
8f46cb8
·
verified ·
1 Parent(s): be5bfbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -24
app.py CHANGED
@@ -4,7 +4,7 @@ from huggingface_hub import InferenceClient
4
 
5
  def respond(
6
  message,
7
- history: list[dict[str, str]],
8
  system_message,
9
  max_tokens,
10
  temperature,
@@ -12,40 +12,44 @@ def respond(
12
  hf_token: gr.OAuthToken,
13
  ):
14
  """
15
- For more information on `huggingface_hub` Inference API support, please check the docs:
16
- https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
17
  """
 
18
  client = InferenceClient(token=hf_token.token, model="apple/FastVLM-7B")
19
 
 
20
  messages = [{"role": "system", "content": system_message}]
21
- messages.extend(history)
 
 
 
 
 
 
 
 
 
22
  messages.append({"role": "user", "content": message})
23
 
24
  response = ""
25
 
26
  try:
27
- for message 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.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
  except Exception as e:
42
  yield f"Erro durante a execução: {str(e)}"
43
 
44
 
45
- """
46
- For information on how to customize the ChatInterface, peruse the gradio docs:
47
- https://www.gradio.app/docs/chatinterface
48
- """
49
  chatbot = gr.ChatInterface(
50
  respond,
51
  type="messages",
@@ -53,13 +57,7 @@ chatbot = gr.ChatInterface(
53
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
54
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
55
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
56
- gr.Slider(
57
- minimum=0.1,
58
- maximum=1.0,
59
- value=0.95,
60
- step=0.05,
61
- label="Top-p (nucleus sampling)",
62
- ),
63
  ],
64
  )
65
 
@@ -68,6 +66,6 @@ with gr.Blocks() as demo:
68
  gr.LoginButton()
69
  chatbot.render()
70
 
71
-
72
  if __name__ == "__main__":
73
  demo.launch()
 
 
4
 
5
  def respond(
6
  message,
7
+ history,
8
  system_message,
9
  max_tokens,
10
  temperature,
 
12
  hf_token: gr.OAuthToken,
13
  ):
14
  """
15
+ Função de resposta usando Hugging Face Inference API.
 
16
  """
17
+ # Use o token diretamente se estiver testando localmente
18
  client = InferenceClient(token=hf_token.token, model="apple/FastVLM-7B")
19
 
20
+
21
  messages = [{"role": "system", "content": system_message}]
22
+
23
+
24
+ if history:
25
+ for h in history:
26
+
27
+ if isinstance(h, tuple) and len(h) == 2:
28
+ user_msg, bot_msg = h
29
+ messages.append({"role": "user", "content": user_msg})
30
+ messages.append({"role": "assistant", "content": bot_msg})
31
+
32
  messages.append({"role": "user", "content": message})
33
 
34
  response = ""
35
 
36
  try:
37
+ for message_chunk in client.chat_completion(
38
  messages,
39
  max_tokens=max_tokens,
40
  stream=True,
41
  temperature=temperature,
42
  top_p=top_p,
43
  ):
44
+ if hasattr(message_chunk, "choices") and message_chunk.choices:
45
+ delta = message_chunk.choices[0].delta
46
+ if delta and hasattr(delta, "content"):
47
+ response += delta.content
48
+ yield response
 
 
49
  except Exception as e:
50
  yield f"Erro durante a execução: {str(e)}"
51
 
52
 
 
 
 
 
53
  chatbot = gr.ChatInterface(
54
  respond,
55
  type="messages",
 
57
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
58
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
59
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
60
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
61
  ],
62
  )
63
 
 
66
  gr.LoginButton()
67
  chatbot.render()
68
 
 
69
  if __name__ == "__main__":
70
  demo.launch()
71
+