kaitwithkwk commited on
Commit
e330931
·
verified ·
1 Parent(s): ed2c719

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -11
app.py CHANGED
@@ -1,24 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
- def respond(message, history):
7
- messages = [{"role": "system", "content": "You are a friendly chatbot."}]
8
-
9
  if history:
10
  messages.extend(history)
11
-
12
  messages.append({"role": "user", "content": message})
13
-
14
- response = client.chat_completion(
15
- messages,
16
- max_tokens=100
17
- )
18
-
19
  return response['choices'][0]['message']['content'].strip()
20
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- chatbot = gr.ChatInterface(respond, type="messages")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  chatbot.launch()
 
1
+ # import gradio as gr
2
+ # from huggingface_hub import InferenceClient
3
+
4
+ # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
+
6
+ # def respond(message, history):
7
+ # messages = [{"role": "system", "content": "You are a friendly chatbot."}]
8
+
9
+ # if history:
10
+ # messages.extend(history)
11
+
12
+ # messages.append({"role": "user", "content": message})
13
+
14
+ # response = client.chat_completion(
15
+ # messages,
16
+ # max_tokens=100
17
+ # )
18
+
19
+ # return response['choices'][0]['message']['content'].strip()
20
+
21
+
22
+ # chatbot = gr.ChatInterface(respond, type="messages")
23
+
24
+ # chatbot.launch()
25
+
26
  import gradio as gr
27
  from huggingface_hub import InferenceClient
28
 
29
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
30
 
31
+ def respond(message, history, user_name):
32
+ messages = [{"role": "system", "content": f"You are a friendly chatbot talking to {user_name}."}]
33
+
34
  if history:
35
  messages.extend(history)
36
+
37
  messages.append({"role": "user", "content": message})
38
+
39
+ response = client.chat_completion(messages, max_tokens=100)
 
 
 
 
40
  return response['choices'][0]['message']['content'].strip()
41
 
42
+ def start_chat(name):
43
+ if name:
44
+ return f"Hello {name}! How can I help you today?", gr.update(interactive=True)
45
+ return "", gr.update(interactive=False)
46
+
47
+ def chat_fn(message, history, user_name):
48
+ if not user_name:
49
+ return history, ""
50
+
51
+ bot_response = respond(message, history, user_name)
52
+ history.append([message, bot_response])
53
+ return history, ""
54
 
55
+ with gr.Blocks() as chatbot:
56
+ gr.Markdown("# Personal Chatbot")
57
+
58
+ user_name = gr.State("")
59
+
60
+ with gr.Row():
61
+ with gr.Column():
62
+ name_input = gr.Textbox(label="Your Name")
63
+ start_btn = gr.Button("Start Chat")
64
+
65
+ with gr.Column():
66
+ chatbot = gr.Chatbot(height=400)
67
+ msg_input = gr.Textbox(label="Message", interactive=False)
68
+ send_btn = gr.Button("Send")
69
+
70
+ start_btn.click(
71
+ lambda name: [name, start_chat(name)[0], start_chat(name)[1]],
72
+ inputs=[name_input],
73
+ outputs=[user_name, chatbot, msg_input]
74
+ )
75
+
76
+ send_btn.click(
77
+ chat_fn,
78
+ inputs=[msg_input, chatbot, user_name],
79
+ outputs=[chatbot, msg_input]
80
+ )
81
 
82
  chatbot.launch()