Azure99 commited on
Commit
caa2d3c
1 Parent(s): eaba406

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import gradio as gr
4
+ import spaces
5
+ from threading import Thread
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
7
+ import torch
8
+
9
+ MAX_INPUT_LIMIT = 3584
10
+
11
+ MODEL_NAME = "Azure99/blossom-v5-9b"
12
+
13
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, device_map="auto")
14
+
15
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
16
+
17
+ GENERATE_CONFIG = dict(
18
+ max_new_tokens=1536,
19
+ temperature=0.5,
20
+ top_p=0.85,
21
+ top_k=50,
22
+ repetition_penalty=1.05
23
+ )
24
+
25
+
26
+ def get_input_ids(inst, history):
27
+ prefix = ("A chat between a human and an artificial intelligence bot. "
28
+ "The bot gives helpful, detailed, and polite answers to the human's questions.")
29
+ patterns = []
30
+ for conv in history:
31
+ patterns.append(f'\n|Human|: {conv[0]}\n|Bot|: ')
32
+ patterns.append(f'{conv[1]}')
33
+ patterns.append(f'\n|Human|: {inst}\n|Bot|: ')
34
+ patterns[0] = prefix + patterns[0]
35
+
36
+ input_ids = []
37
+ for i, pattern in enumerate(patterns):
38
+ input_ids += tokenizer.encode(pattern, add_special_tokens=(i == 0))
39
+ if i % 2 == 1:
40
+ input_ids += [tokenizer.eos_token_id]
41
+ return input_ids
42
+
43
+
44
+ @spaces.GPU
45
+ def chat(inst, history):
46
+ with torch.no_grad():
47
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
48
+ input_ids = get_input_ids(inst, history)
49
+ print(len(input_ids))
50
+ if len(input_ids) > MAX_INPUT_LIMIT:
51
+ yield "The input is too long, please clear the history."
52
+ return
53
+ generation_kwargs = dict(input_ids=torch.tensor([input_ids]).to(model.device), do_sample=True,
54
+ streamer=streamer, **GENERATE_CONFIG)
55
+ Thread(target=model.generate, kwargs=generation_kwargs).start()
56
+
57
+ # stop watch
58
+ start = time.time()
59
+ outputs = ""
60
+ for new_text in streamer:
61
+ outputs += new_text
62
+ yield outputs
63
+ total_time = time.time() - start
64
+ output_token_len = len(tokenizer.encode(outputs, add_special_tokens=False))
65
+ speed = output_token_len / total_time
66
+ print(f"Speed: {speed:.2f} tokens/s")
67
+
68
+
69
+ gr.ChatInterface(chat,
70
+ chatbot=gr.Chatbot(show_label=False, height=500, show_copy_button=True, render_markdown=True),
71
+ textbox=gr.Textbox(placeholder="", container=False, scale=7),
72
+ title="Blossom Demo",
73
+ description='Hello, I am Blossom, an open source conversational large language model.🌠'
74
+ '<a href="https://github.com/Azure99/BlossomLM">GitHub</a>',
75
+ theme="soft",
76
+ examples=["Hello", "What is MBTI", "用Python实现二分查找", "为switch写一篇小红书种草文案,带上emoji"],
77
+ clear_btn="🗑️Clear",
78
+ undo_btn="↩️Undo",
79
+ retry_btn="🔄Retry",
80
+ submit_btn="➡️Submit",
81
+ ).queue().launch()