Riswan-Nopiyar commited on
Commit
666fdec
·
verified ·
1 Parent(s): b6385ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -37
app.py CHANGED
@@ -1,15 +1,25 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
- # Fungsi untuk merespons pesan
7
- def respond(message, history, system_message, max_tokens, temperature, top_p):
8
- system_message = "Hai, apa ada yang bisa saya bantu?" # Pesan sistem di awal
9
 
10
- # Menyusun riwayat percakapan
 
 
 
 
 
 
 
 
 
 
11
  messages = [{"role": "system", "content": system_message}]
12
-
13
  for val in history:
14
  if val[0]:
15
  messages.append({"role": "user", "content": val[0]})
@@ -19,6 +29,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
19
  messages.append({"role": "user", "content": message})
20
 
21
  response = ""
 
22
  for message in client.chat_completion(
23
  messages,
24
  max_tokens=max_tokens,
@@ -27,41 +38,30 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
27
  top_p=top_p,
28
  ):
29
  token = message.choices[0].delta.content
 
30
  response += token
31
  yield response
32
 
33
- # Mengembalikan dua output yang diperlukan oleh Gradio
34
- history.append((message, response)) # Menyimpan riwayat percakapan
35
- return history, "" # Mengembalikan history percakapan dan input textbox yang kosong
36
-
37
- # Initial message dalam format yang benar
38
- initial_messages = [{"role": "system", "content": "Hai, apa ada yang bisa saya bantu?"}]
39
 
40
- # Membuat antarmuka Gradio
41
- with gr.Blocks(fill_height=True, css="footer {visibility: hidden}") as demo:
42
- gr.HTML("<div style='text-align: center;'><h2>Chatbot</h2></div>") # Judul
43
-
44
- chatbot = gr.Chatbot(
45
- label="Chatbot",
46
- elem_id="chatbot",
47
- bubble_full_width=True,
48
- scale=1,
49
- avatar_images=(None, "https://iili.io/d5tk44a.md.png"),
50
- placeholder="Masukkan pesan...",
51
- render_markdown=True,
52
- sanitize_html=True,
53
- show_copy_button=True,
54
- value=initial_messages, # Format yang benar (messages)
55
- type="messages" # Menambahkan 'type="messages"' untuk menghindari peringatan
56
- )
57
-
58
- chat_input = gr.Textbox(interactive=True, placeholder="Masukkan pesan...", show_label=False)
59
-
60
- # Proses input dan respons
61
- chat_msg = chat_input.submit(respond, [chat_input, chatbot], [chatbot, chat_input])
62
- bot_msg = chat_msg.then(respond, inputs=[chat_input, chatbot], outputs=[chatbot, chat_input])
63
 
64
- chatbot.like(lambda: "Like/dislike", None, None) # Fitur like tetap ada
65
 
66
- # Menjalankan antarmuka
67
- demo.launch()
 
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
 
10
+ def respond(
11
+ message,
12
+ history: list[tuple[str, str]],
13
+ system_message,
14
+ max_tokens,
15
+ temperature,
16
+ top_p,
17
+ ):
18
+ # Set system message in Indonesian
19
+ system_message = "Hai, apa ada yang bisa saya bantu?"
20
+
21
  messages = [{"role": "system", "content": system_message}]
22
+
23
  for val in history:
24
  if val[0]:
25
  messages.append({"role": "user", "content": val[0]})
 
29
  messages.append({"role": "user", "content": message})
30
 
31
  response = ""
32
+
33
  for message in client.chat_completion(
34
  messages,
35
  max_tokens=max_tokens,
 
38
  top_p=top_p,
39
  ):
40
  token = message.choices[0].delta.content
41
+
42
  response += token
43
  yield response
44
 
 
 
 
 
 
 
45
 
46
+ """
47
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
48
+ """
49
+ demo = gr.ChatInterface(
50
+ respond,
51
+ additional_inputs=[
52
+ gr.Textbox(value="Anda adalah chatbot yang ramah.", label="Pesan Sistem"),
53
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max token baru"),
54
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperatur"),
55
+ gr.Slider(
56
+ minimum=0.1,
57
+ maximum=1.0,
58
+ value=0.95,
59
+ step=0.05,
60
+ label="Top-p (nukleus sampling)",
61
+ ),
62
+ ],
63
+ )
 
 
 
 
 
64
 
 
65
 
66
+ if __name__ == "__main__":
67
+ demo.launch()