Mattimax commited on
Commit
ace4373
·
verified ·
1 Parent(s): c57b239

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -2,11 +2,11 @@ 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("Mattimax/DATA-AI_Chat_3_0.5B")
8
 
9
-
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -15,30 +15,30 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
 
 
 
42
 
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -46,7 +46,10 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are DATA-AI, created by M.INC.. You are a helpful assistant. Respond in the user language. DO NOT REPEAT THINGS. DO NOT START TO LOOP THE MESSAGES.", label="System message"),
 
 
 
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
@@ -59,6 +62,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
 
4
  """
5
+ For more information on `huggingface_hub` Inference API support, please check the docs:
6
+ https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
  """
8
  client = InferenceClient("Mattimax/DATA-AI_Chat_3_0.5B")
9
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
 
15
  temperature,
16
  top_p,
17
  ):
18
+ # Prepara il prompt basato sulla cronologia
19
+ prompt = system_message + "\n"
20
+ for user_msg, bot_msg in history:
21
+ prompt += f"User: {user_msg}\n"
22
+ if bot_msg:
23
+ prompt += f"Assistant: {bot_msg}\n"
24
+
25
+ prompt += f"User: {message}\nAssistant:"
26
+
27
+ # Invia la richiesta al modello usando text_generation
28
+ response = client.text_generation(
29
+ prompt=prompt,
30
+ max_new_tokens=max_tokens,
 
 
 
31
  temperature=temperature,
32
  top_p=top_p,
33
+ stream=True,
34
+ )
35
+
36
+ # Costruisce la risposta a partire dal generato
37
+ generated_text = ""
38
+ for chunk in response:
39
+ token = chunk.token.text if hasattr(chunk.token, 'text') else chunk.token
40
+ generated_text += token
41
+ yield generated_text
42
 
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
+ gr.Textbox(
50
+ value="You are DATA-AI, created by M.INC.. You are a helpful assistant. Respond in the user language. DO NOT REPEAT THINGS. DO NOT START TO LOOP THE MESSAGES.",
51
+ label="System message"
52
+ ),
53
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
54
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
55
  gr.Slider(
 
62
  ],
63
  )
64
 
 
65
  if __name__ == "__main__":
66
+ demo.launch()