yuchenlin commited on
Commit
131a07a
1 Parent(s): 9b8eb72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -71
app.py CHANGED
@@ -1,74 +1,43 @@
1
- import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import spaces
4
-
5
- # Load model and tokenizer
6
- model_name = "Magpie-Align/MagpieLM-4B-Chat-v0.1"
7
-
8
- device = "cuda" # the device to load the model onto
9
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
10
- model = AutoModelForCausalLM.from_pretrained(
11
- model_name,
12
- torch_dtype="auto"
13
- )
14
- model.to(device)
15
-
16
- @spaces.GPU(enable_queue=True)
17
- def respond(
18
- message,
19
- history: list[tuple[str, str]],
20
- system_message,
21
- max_tokens=2048,
22
- temperature=0.6,
23
- top_p=0.9,
24
- repetition_penalty=1.0,
25
- ):
26
- messages = [{"role": "system", "content": system_message}]
27
-
28
- for val in history:
29
- if val[0]:
30
- messages.append({"role": "user", "content": val[0]})
31
- if val[1]:
32
- messages.append({"role": "assistant", "content": val[1]})
33
-
34
- messages.append({"role": "user", "content": message})
35
-
36
- text = tokenizer.apply_chat_template(
37
- messages,
38
- tokenize=False,
39
- add_generation_prompt=True
40
- )
41
- model_inputs = tokenizer([text], return_tensors="pt").to(device)
42
-
43
- streamer = gr.utils.StreamingTextStreamer(tokenizer, skip_special_tokens=True, skip_prompt=True)
44
-
45
- _ = model.generate(
46
- model_inputs.input_ids,
47
- max_new_tokens = max_tokens,
48
- temperature = temperature,
49
- top_p = top_p,
50
  repetition_penalty=repetition_penalty,
51
- streamer=streamer
52
  )
 
 
53
 
54
- return streamer
55
-
56
- demo = gr.ChatInterface(
57
- respond,
58
- additional_inputs=[
59
- gr.Textbox(value="You are Magpie, a friendly Chatbot.", label="System message"),
60
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
61
- gr.Slider(minimum=0.1, maximum=4.0, value=0.6, step=0.1, label="Temperature"),
62
- gr.Slider(
63
- minimum=0.1,
64
- maximum=1.0,
65
- value=0.9,
66
- step=0.05,
67
- label="Top-p (nucleus sampling)",
68
- ),
69
- gr.Slider(minimum=0.5, maximum=1.5, value=1.0, step=0.1, label="Repetation Penalty"),
70
- ],
71
- )
72
-
73
- if __name__ == "__main__":
74
- demo.launch(share=True)
 
1
+ @spaces.GPU
2
+ def generate(
3
+ message: str,
4
+ chat_history: list[tuple[str, str]],
5
+ system_prompt: str,
6
+ max_new_tokens: int = 1024,
7
+ temperature: float = 0.6,
8
+ top_p: float = 0.9,
9
+ top_k: int = 50,
10
+ repetition_penalty: float = 1.2,
11
+ ) -> Iterator[str]:
12
+ conversation = []
13
+ if system_prompt:
14
+ conversation.append({"role": "system", "content": system_prompt})
15
+ for user, assistant in chat_history:
16
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
17
+ conversation.append({"role": "user", "content": message})
18
+
19
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
20
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
21
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
22
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
23
+ input_ids = input_ids.to(model.device)
24
+
25
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
26
+ generate_kwargs = dict(
27
+ {"input_ids": input_ids},
28
+ streamer=streamer,
29
+ max_new_tokens=max_new_tokens,
30
+ do_sample=True,
31
+ top_p=top_p,
32
+ top_k=top_k,
33
+ temperature=temperature,
34
+ num_beams=1,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  repetition_penalty=repetition_penalty,
 
36
  )
37
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
38
+ t.start()
39
 
40
+ outputs = []
41
+ for text in streamer:
42
+ outputs.append(text)
43
+ yield "".join(outputs)