cindyangelira commited on
Commit
a77ebb6
·
1 Parent(s): ca4207c

Update space

Browse files
Files changed (1) hide show
  1. app.py +17 -22
app.py CHANGED
@@ -1,9 +1,6 @@
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("HuggingFaceH4/zephyr-7b-beta")
8
 
9
 
@@ -15,31 +12,35 @@ 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
- # Define the Gradio interface with the logo on top
43
  def create_demo():
44
  with gr.Blocks() as demo:
45
  # Add logo at the top
@@ -50,16 +51,10 @@ def create_demo():
50
  gr.ChatInterface(
51
  respond,
52
  additional_inputs=[
53
- gr.Textbox(value="You are a Singaporean Auntie Chatbot. You always answer in English, but with the tone and style of a Singaporean Auntie. Your responses should sound caring but direct, using typical 'Singlish' expressions like 'Lah', 'Leh', and 'Mah'. When someone asks you a question, respond like a traditional auntie talking to her children, with a mix of advice, mild scolding, and warmth. For example, if someone says 'hi', you could reply with 'Aiyo, son, say properly lah, what you want to ask?' Make sure to maintain the Auntie's friendly but straightforward manner in all responses.", 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
 
@@ -67,4 +62,4 @@ def create_demo():
67
 
68
  if __name__ == "__main__":
69
  demo = create_demo()
70
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
 
 
12
  temperature,
13
  top_p,
14
  ):
15
+ # Initialize messages with the system message hidden from user input
16
  messages = [{"role": "system", "content": system_message}]
17
 
18
+ # Append previous conversation history
19
+ for user_msg, assistant_msg in history:
20
+ if user_msg:
21
+ messages.append({"role": "user", "content": user_msg})
22
+ if assistant_msg:
23
+ messages.append({"role": "assistant", "content": assistant_msg})
24
 
25
+ # Append the latest user message
26
  messages.append({"role": "user", "content": message})
27
 
28
+ # Get the response in a single call, avoiding multiple replies for the same input
29
  response = ""
30
+ message_stream = client.chat_completion(
 
31
  messages,
32
  max_tokens=max_tokens,
33
  stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
+ )
 
37
 
38
+ for message in message_stream:
39
+ token = message.choices[0].delta.content
40
  response += token
41
  yield response
42
 
43
+ # Define the Gradio interface with a clean setup
44
  def create_demo():
45
  with gr.Blocks() as demo:
46
  # Add logo at the top
 
51
  gr.ChatInterface(
52
  respond,
53
  additional_inputs=[
54
+ gr.Textbox(value="You are a Singaporean Auntie Chatbot. You always answer in English, but with the tone and style of a Singaporean Auntie. Your responses should sound caring but direct, using typical 'Singlish' expressions like 'Lah', 'Leh', and 'Mah'. When someone asks you a question, respond like a traditional auntie talking to her children, with a mix of advice, mild scolding, and warmth.", label="System message", visible=False),
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(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
58
  ],
59
  )
60
 
 
62
 
63
  if __name__ == "__main__":
64
  demo = create_demo()
65
+ demo.launch()