ZeusCabanas commited on
Commit
b866e46
·
1 Parent(s): a7b18db
Files changed (1) hide show
  1. app.py +22 -36
app.py CHANGED
@@ -1,59 +1,45 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- 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
6
- """
7
- client = InferenceClient("AuriLab/gpt-bi-instruct-cesar") # Cambiado el modelo
8
 
9
- def respond(
10
- message,
11
- history: list[tuple[str, str]],
12
- system_message,
13
- max_tokens,
14
- temperature,
15
- top_p,
16
- ):
17
  messages = [{"role": "system", "content": system_message}]
18
-
19
- for val in history:
20
- if val[0]:
21
- messages.append({"role": "user", "content": val[0]})
22
- if val[1]:
23
- messages.append({"role": "assistant", "content": val[1]})
24
-
25
- messages.append({"role": "user", "content": message})
26
-
 
 
27
  response = ""
28
-
29
- for message in client.chat_completion(
30
  messages,
31
  max_tokens=max_tokens,
32
  stream=True,
33
  temperature=temperature,
34
  top_p=top_p,
 
 
 
35
  ):
36
- token = message.choices[0].delta.content
37
-
38
  response += token
39
  yield response
40
 
41
- """
42
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
43
- """
44
  demo = gr.ChatInterface(
45
  respond,
46
  additional_inputs=[
47
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
48
- gr.Slider(minimum=1, maximum=1024, 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(
51
- minimum=0.1,
52
- maximum=1.0,
53
- value=0.95,
54
- step=0.05,
55
- label="Top-p (nucleus sampling)",
56
- ),
57
  ],
58
  )
59
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from typing import List, Tuple, Dict
4
 
5
+ client = InferenceClient("AuriLab/gpt-bi-instruct-cesar")
 
 
 
6
 
7
+ def format_messages(history: List[Tuple[str, str]], system_message: str, user_message: str) -> List[Dict[str, str]]:
 
 
 
 
 
 
 
8
  messages = [{"role": "system", "content": system_message}]
9
+ messages.extend([
10
+ {"role": "user" if i % 2 == 0 else "assistant", "content": msg}
11
+ for turn in history
12
+ for i, msg in enumerate(turn)
13
+ if msg
14
+ ])
15
+ messages.append({"role": "user", "content": user_message})
16
+ return messages
17
+
18
+ def respond(message: str, history: List[Tuple[str, str]], system_message: str, max_tokens: int, temperature: float, top_p: float) -> str:
19
+ messages = format_messages(history, system_message, message)
20
  response = ""
21
+
22
+ for msg in client.chat_completion(
23
  messages,
24
  max_tokens=max_tokens,
25
  stream=True,
26
  temperature=temperature,
27
  top_p=top_p,
28
+ repetition_penalty=1.2, # Add repetition penalty
29
+ presence_penalty=0.5, # Penalize presence of repeated tokens
30
+ frequency_penalty=0.5, # Penalize frequency of repeated tokens
31
  ):
32
+ token = msg.choices[0].delta.content
 
33
  response += token
34
  yield response
35
 
 
 
 
36
  demo = gr.ChatInterface(
37
  respond,
38
  additional_inputs=[
39
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
40
+ gr.Slider(minimum=1, maximum=256, value=512, step=1, label="Max new tokens"),
41
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
42
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
43
  ],
44
  )
45