Jether commited on
Commit
5bb4979
·
verified ·
1 Parent(s): 66ad403

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -58
app.py CHANGED
@@ -1,72 +1,47 @@
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
 
11
- def respond(
12
- message,
13
- history: list[tuple[str, str]],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
18
- ):
19
- messages = [{"role": "system", "content": system_message}]
20
 
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
 
26
 
27
- messages.append({"role": "user", "content": message})
28
 
29
- response = ""
 
 
 
 
 
 
30
 
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = message.choices[0].delta.content
39
 
40
- response += token
41
- yield response
42
 
 
 
 
 
 
 
 
43
 
44
- """
45
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
46
- """
 
 
47
 
 
48
 
49
-
50
- demo = gr.ChatInterface(
51
- respond,
52
- additional_inputs=[
53
- gr.Textbox(value="You are chatbot for PGP clients.", 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
- )
66
-
67
-
68
-
69
-
70
-
71
- if __name__ == "__main__":
72
- demo.launch()
 
1
  import gradio as gr
2
+ import time
3
 
4
+ # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
 
 
 
5
 
6
 
7
+ def print_like_dislike(x: gr.LikeData):
8
+ print(x.index, x.value, x.liked)
9
 
 
 
 
 
 
 
 
 
 
10
 
11
+ def add_message(history, message):
12
+ for x in message["files"]:
13
+ history.append({"role": "user", "content": {"path": x}})
14
+ if message["text"] is not None:
15
+ history.append({"role": "user", "content": message["text"]})
16
+ return history, gr.MultimodalTextbox(value=None, interactive=False)
17
 
 
18
 
19
+ def bot(history: list):
20
+ response = "**That's cool!**"
21
+ history.append({"role": "assistant", "content": ""})
22
+ for character in response:
23
+ history[-1]["content"] += character
24
+ time.sleep(0.05)
25
+ yield history
26
 
 
 
 
 
 
 
 
 
27
 
28
+ with gr.Blocks() as demo:
29
+ chatbot = gr.Chatbot(elem_id="chatbot", bubble_full_width=False, type="messages")
30
 
31
+ chat_input = gr.MultimodalTextbox(
32
+ interactive=True,
33
+ file_count="multiple",
34
+ placeholder="Enter message or upload file...",
35
+ show_label=False,
36
+ sources=["microphone", "upload"],
37
+ )
38
 
39
+ chat_msg = chat_input.submit(
40
+ add_message, [chatbot, chat_input], [chatbot, chat_input]
41
+ )
42
+ bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
43
+ bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
44
 
45
+ chatbot.like(print_like_dislike, None, None, like_user_message=True)
46
 
47
+ demo.launch()