ivpich commited on
Commit
96e30a1
·
verified ·
1 Parent(s): a63313c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -46
app.py CHANGED
@@ -1,62 +1,151 @@
 
 
 
 
 
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("AnatoliiPotapov/T-lite-instruct-0.1")
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
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
 
62
  if __name__ == "__main__":
 
1
+ import os
2
+ import time
3
+ import spaces
4
+ import torch
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
6
  import gradio as gr
7
+ from threading import Thread
8
 
9
+ MODEL_LIST = ["AnatoliiPotapov/T-lite-instruct-0.1"]
10
+
11
+ MODEL = MODEL_LIST[0]
12
+ TITLE = "<h1><center>T-lite</center></h1>"
13
+
14
+ PLACEHOLDER = """
15
+ <center>
16
+ <p>T-lite model by T-Bank.</p>
17
+ </center>
18
  """
19
+
20
+
21
+ CSS = """
22
+ .duplicate-button {
23
+ margin: auto !important;
24
+ color: white !important;
25
+ background: black !important;
26
+ border-radius: 100vh !important;
27
+ }
28
+ h3 {
29
+ text-align: center;
30
+ }
31
  """
 
32
 
33
+ device = "cuda" # for GPU usage or "cpu" for CPU usage
34
 
35
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
36
+ model = AutoModelForCausalLM.from_pretrained(
37
+ MODEL,
38
+ torch_dtype=torch.bfloat16,
39
+ device_map="auto",
40
+ ignore_mismatched_sizes=True)
41
+
42
+ @spaces.GPU()
43
+ def stream_chat(
44
+ message: str,
45
+ history: list,
46
+ temperature: float = 0.3,
47
+ max_new_tokens: int = 1024,
48
+ top_p: float = 1.0,
49
+ top_k: int = 20,
50
+ penalty: float = 1.2,
51
  ):
52
+ print(f'message: {message}')
53
+ print(f'history: {history}')
54
 
55
+ conversation = []
56
+ for prompt, answer in history:
57
+ conversation.extend([
58
+ {"role": "user", "content": prompt},
59
+ {"role": "assistant", "content": answer},
60
+ ])
61
 
62
+ conversation.append({"role": "user", "content": message})
63
 
64
+ input_text=tokenizer.apply_chat_template(conversation, tokenize=False)
65
+ inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
66
+ streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
67
+
68
+ generate_kwargs = dict(
69
+ input_ids=inputs,
70
+ max_new_tokens = max_new_tokens,
71
+ do_sample = False if temperature == 0 else True,
72
+ top_p = top_p,
73
+ top_k = top_k,
74
+ temperature = temperature,
75
+ streamer=streamer,
76
+ pad_token_id = 10,
77
+ )
78
 
79
+ with torch.no_grad():
80
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
81
+ thread.start()
82
+
83
+ buffer = ""
84
+ for new_text in streamer:
85
+ buffer += new_text
86
+ yield buffer
87
 
88
+
89
+ chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
90
 
91
+ with gr.Blocks(css=CSS, theme="soft") as demo:
92
+ gr.HTML(TITLE)
93
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
94
+ gr.ChatInterface(
95
+ fn=stream_chat,
96
+ chatbot=chatbot,
97
+ fill_height=True,
98
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
99
+ additional_inputs=[
100
+ gr.Slider(
101
+ minimum=0,
102
+ maximum=1,
103
+ step=0.1,
104
+ value=0.3,
105
+ label="Temperature",
106
+ render=False,
107
+ ),
108
+ gr.Slider(
109
+ minimum=128,
110
+ maximum=8192,
111
+ step=1,
112
+ value=1024,
113
+ label="Max new tokens",
114
+ render=False,
115
+ ),
116
+ gr.Slider(
117
+ minimum=0.0,
118
+ maximum=1.0,
119
+ step=0.1,
120
+ value=1.0,
121
+ label="top_p",
122
+ render=False,
123
+ ),
124
+ gr.Slider(
125
+ minimum=1,
126
+ maximum=20,
127
+ step=1,
128
+ value=20,
129
+ label="top_k",
130
+ render=False,
131
+ ),
132
+ gr.Slider(
133
+ minimum=0.0,
134
+ maximum=2.0,
135
+ step=0.1,
136
+ value=1.2,
137
+ label="Repetition penalty",
138
+ render=False,
139
+ ),
140
+ ],
141
+ examples=[
142
+ ["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],
143
+ ["What are 5 creative things I could do with my kids' art? I don't want to throw them away, but it's also so much clutter."],
144
+ ["Tell me a random fun fact about the Roman Empire."],
145
+ ["Show me a code snippet of a website's sticky header in CSS and JavaScript."],
146
+ ],
147
+ cache_examples=False,
148
+ )
149
 
150
 
151
  if __name__ == "__main__":