mattcracker commited on
Commit
b6f75f9
Β·
verified Β·
1 Parent(s): cc1184a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -60
app.py CHANGED
@@ -1,64 +1,84 @@
 
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("agentica-org/DeepScaleR-1.5B-Preview")
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
- 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
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
62
 
63
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  demo.launch()
 
 
 
 
 
1
+ from threading import Thread
2
  import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
4
+ import spaces
5
+ tokenizer = AutoTokenizer.from_pretrained("agentica-org/DeepScaleR-1.5B-Preview")
6
+ model = AutoModelForCausalLM.from_pretrained("agentica-org/DeepScaleR-1.5B-Preview", device_map='auto')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
 
9
+ def preprocess_messages(history):
10
+ messages = []
11
+
12
+ for idx, (user_msg, model_msg) in enumerate(history):
13
+ if idx == len(history) - 1 and not messages:
14
+ messages.append({"role": "user", "content": user_msg})
15
+ break
16
+ if user_msg:
17
+ messages.append({"role": "user", "content": user_msg})
18
+ if model_msg:
19
+ messages.append({"role": "assistant", "content": messages})
20
+
21
+ return messages
22
+
23
+
24
+ @spaces.GPU()
25
+ def predict(history, max_length, top_p, temperature):
26
+ messages = preprocess_messages(history)
27
+ model_inputs = tokenizer.apply_chat_template(
28
+ messages, add_generation_prompt=True, tokenize=True, return_tensors="pt", return_dict=True
29
+ ).to(model.device)
30
+ streamer = TextIteratorStreamer(tokenizer, timeout=60, skip_prompt=True, skip_special_tokens=True)
31
+ generate_kwargs = {
32
+ "input_ids": model_inputs["input_ids"],
33
+ "attention_mask": model_inputs["attention_mask"],
34
+ "streamer": streamer,
35
+ "max_new_tokens": max_length,
36
+ "do_sample": True,
37
+ "top_p": top_p,
38
+ "temperature": temperature,
39
+ "repetition_penalty": 1.2,
40
+ }
41
+
42
+ generate_kwargs['eos_token_id'] = tokenizer.encode("<|user|>")
43
+
44
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
45
+ t.start()
46
+ for new_token in streamer:
47
+ if new_token:
48
+ history[-1][1] += new_token
49
+ yield history
50
+
51
+
52
+ def main():
53
+ with gr.Blocks() as demo:
54
+ gr.HTML("""<h1 align="center">GLM-Edge-Chat Gradio Demo</h1>""")
55
+
56
+ with gr.Row():
57
+ with gr.Column(scale=3):
58
+ chatbot = gr.Chatbot()
59
+
60
+ with gr.Row():
61
+ with gr.Column(scale=2):
62
+ user_input = gr.Textbox(show_label=True, placeholder="Input...", label="User Input")
63
+ submitBtn = gr.Button("Submit")
64
+ emptyBtn = gr.Button("Clear History")
65
+ with gr.Column(scale=1):
66
+ max_length = gr.Slider(0, 8192, value=4096, step=1.0, label="Maximum length", interactive=True)
67
+ top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
68
+ temperature = gr.Slider(0.01, 1, value=0.6, step=0.01, label="Temperature", interactive=True)
69
+
70
+ # Define functions for button actions
71
+ def user(query, history):
72
+ return "", history + [[query, ""]]
73
+
74
+ submitBtn.click(user, [user_input, chatbot], [user_input, chatbot], queue=False).then(
75
+ predict, [chatbot, max_length, top_p, temperature], chatbot
76
+ )
77
+ emptyBtn.click(lambda: (None, None), None, [chatbot], queue=False)
78
+
79
+ demo.queue()
80
  demo.launch()
81
+
82
+
83
+ if __name__ == "__main__":
84
+ main()