Bndo commited on
Commit
21a471c
Β·
verified Β·
1 Parent(s): c687620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -22
app.py CHANGED
@@ -1,23 +1,10 @@
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
- import gradio as gr
10
- import requests
11
-
12
- def chat_with_mistral(user_input):
13
- return "Hello! This is Bandar AI responding."
14
-
15
- # βœ… Fix: Enable API mode in Gradio
16
- iface = gr.Interface(fn=chat_with_mistral, inputs="text", outputs="text")
17
-
18
- iface.launch(share=True)
19
-
20
-
21
  def respond(
22
  message,
23
  history: list[tuple[str, str]],
@@ -28,16 +15,19 @@ def respond(
28
  ):
29
  messages = [{"role": "system", "content": system_message}]
30
 
 
31
  for val in history:
32
  if val[0]:
33
  messages.append({"role": "user", "content": val[0]})
34
  if val[1]:
35
  messages.append({"role": "assistant", "content": val[1]})
36
 
 
37
  messages.append({"role": "user", "content": message})
38
 
39
  response = ""
40
 
 
41
  for message in client.chat_completion(
42
  messages,
43
  max_tokens=max_tokens,
@@ -46,18 +36,15 @@ def respond(
46
  top_p=top_p,
47
  ):
48
  token = message.choices[0].delta.content
49
-
50
  response += token
51
- yield response
52
 
53
 
54
- """
55
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
56
- """
57
  demo = gr.ChatInterface(
58
  respond,
59
  additional_inputs=[
60
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
61
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
62
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
63
  gr.Slider(
@@ -68,8 +55,10 @@ demo = gr.ChatInterface(
68
  label="Top-p (nucleus sampling)",
69
  ),
70
  ],
 
 
71
  )
72
 
73
-
74
  if __name__ == "__main__":
75
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # βœ… Set up Hugging Face Inference API with the correct model
 
 
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
+ # βœ… Define the response function
 
 
 
 
 
 
 
 
 
 
 
8
  def respond(
9
  message,
10
  history: list[tuple[str, str]],
 
15
  ):
16
  messages = [{"role": "system", "content": system_message}]
17
 
18
+ # Add chat history
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
+ # Add the new user input
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
 
30
+ # βœ… Stream responses from Hugging Face API
31
  for message in client.chat_completion(
32
  messages,
33
  max_tokens=max_tokens,
 
36
  top_p=top_p,
37
  ):
38
  token = message.choices[0].delta.content
 
39
  response += token
40
+ yield response # Stream output in real-time
41
 
42
 
43
+ # βœ… Improved Chatbot UI
 
 
44
  demo = gr.ChatInterface(
45
  respond,
46
  additional_inputs=[
47
+ gr.Textbox(value="You are Bandar AI, a friendly chatbot.", label="System message"),
48
  gr.Slider(minimum=1, maximum=2048, 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(
 
55
  label="Top-p (nucleus sampling)",
56
  ),
57
  ],
58
+ title="πŸ€– Bandar AI Chatbot",
59
+ description="πŸš€ Chat with Bandar AI, powered by Zephyr-7B."
60
  )
61
 
62
+ # βœ… Run the chatbot
63
  if __name__ == "__main__":
64
  demo.launch()