wangzhang commited on
Commit
7c26e10
·
1 Parent(s): 336c697

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -132
app.py CHANGED
@@ -1,56 +1,9 @@
1
- from threading import Thread
2
- from typing import Iterator
3
-
4
  import gradio as gr
5
- import spaces
6
- import torch
7
- from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
8
-
9
- MAX_MAX_NEW_TOKENS = 2048
10
- DEFAULT_MAX_NEW_TOKENS = 1024
11
- MAX_INPUT_TOKEN_LENGTH = 4096
12
-
13
- DESCRIPTION = """\
14
- # ChatSDB
15
-
16
- 这是SequioaDB旗下的AI智能大语言模型,训练超过上万条真实数据和7亿参数。
17
- ChatSDB是SequoiaDB旗下的AI智能大语言模型,训练超过上万条真实数据和7亿参数</h3>
18
- <br><strong>模型🔗: <a>https://huggingface.co/wangzhang/ChatSDB </a></strong>
19
- <br><strong>Dataset🔗: <a>https://huggingface.co/datasets/wangzhang/sdb </a></strong>
20
- <br><strong> API Doc🔗: <a>https://zgg3nzdpswxy4a-80.proxy.runpod.net/docs/ <a> </strong>
21
- """
22
-
23
- LICENSE = """ """
24
-
25
- if not torch.cuda.is_available():
26
- DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
27
-
28
-
29
- if torch.cuda.is_available():
30
- model_id = "wangzhang/ChatSDB-hf"
31
- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
32
- tokenizer = AutoTokenizer.from_pretrained(model_id)
33
- tokenizer.use_default_system_prompt = False
34
 
 
35
 
36
- @spaces.GPU
37
- def generate(
38
- message: str,
39
- chat_history: list[tuple[str, str]],
40
- system_prompt: str,
41
- max_new_tokens: int = 512,
42
- temperature: float = 0.2,
43
- top_p: float = 0.9,
44
- top_k: int = 10,
45
- repetition_penalty: float = 1.2,
46
- ) -> Iterator[str]:
47
- conversation = []
48
- if system_prompt:
49
- conversation.append({"role": "system", "content": system_prompt})
50
- for user, assistant in chat_history:
51
- conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
52
- conversation.append({"role": "user", "content": message})
53
-
54
  prompt = f"""### Instruction:
55
 
56
  ### Task:
@@ -61,85 +14,23 @@ def generate(
61
 
62
  ### Response:
63
  """
64
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids.cuda()
65
- if len(inputs) > MAX_INPUT_TOKEN_LENGTH:
66
- inputs = inputs[-MAX_INPUT_TOKEN_LENGTH:]
67
- gr.Warning("Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
68
-
69
- streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
70
- generate_kwargs = dict(
71
- inputs,
72
- streamer=streamer,
73
- max_new_tokens=max_new_tokens,
74
- do_sample=True,
75
- top_p=top_p,
76
- top_k=top_k,
77
- temperature=temperature,
78
- num_beams=1,
79
- repetition_penalty=repetition_penalty,
80
- )
81
- t = Thread(target=model.generate, kwargs=generate_kwargs)
82
- t.start()
83
-
84
- outputs = []
85
- for text in streamer:
86
- outputs.append(text)
87
- yield "".join(outputs)
88
-
89
-
90
- chat_interface = gr.ChatInterface(
91
- fn=generate,
92
- additional_inputs=[
93
- gr.Textbox(label="System prompt", lines=6),
94
- gr.Slider(
95
- label="Max new tokens",
96
- minimum=1,
97
- maximum=MAX_MAX_NEW_TOKENS,
98
- step=1,
99
- value=DEFAULT_MAX_NEW_TOKENS,
100
- ),
101
- gr.Slider(
102
- label="Temperature",
103
- minimum=0.1,
104
- maximum=4.0,
105
- step=0.1,
106
- value=0.6,
107
- ),
108
- gr.Slider(
109
- label="Top-p (nucleus sampling)",
110
- minimum=0.05,
111
- maximum=1.0,
112
- step=0.05,
113
- value=0.9,
114
- ),
115
- gr.Slider(
116
- label="Top-k",
117
- minimum=1,
118
- maximum=1000,
119
- step=1,
120
- value=50,
121
- ),
122
- gr.Slider(
123
- label="Repetition penalty",
124
- minimum=1.0,
125
- maximum=2.0,
126
- step=0.05,
127
- value=1.2,
128
- ),
129
- ],
130
- stop_btn=None,
131
- examples=[
132
- ["如何安装巨杉数据库SequioaDB?"],
133
- ["巨杉数据库SequioaDB有哪些优势?"],
134
- ["巨杉数据库SequioaDB是什么?"],
135
- ],
136
- )
137
-
138
- with gr.Blocks(css="style.css") as demo:
139
- gr.Markdown(DESCRIPTION)
140
- gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
141
- chat_interface.render()
142
- gr.Markdown(LICENSE)
143
-
144
- if __name__ == "__main__":
145
- demo.queue(max_size=20).launch()
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ client = InferenceClient(model="wangzhang/chatSDB-test")
5
 
6
+ def inference(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  prompt = f"""### Instruction:
8
 
9
  ### Task:
 
14
 
15
  ### Response:
16
  """
17
+ partial_message = ""
18
+ for token in client.text_generation(prompt=prompt, max_new_tokens=512, stream=True, best_of=1, temperature=0.1,
19
+ top_p=0.99, do_sample=True, repetition_penalty=1.2):
20
+ if token.startswith("<s>"):
21
+ return partial_message
22
+ partial_message += token
23
+ yield partial_message
24
+
25
+ gr.ChatInterface(
26
+ inference,
27
+ chatbot=gr.Chatbot(height=300, scale=7),
28
+ textbox=gr.Textbox(placeholder="你可以问我任何关于SequioaDB的��题!", container=False, scale=7),
29
+ description="这是SequioaDB旗下的AI智能大语言模型,训练超过上万条真实数据和7亿参数。",
30
+ title="ChatSDB",
31
+ examples=["SequoiaDB巨杉数据库是什么?", "SequoiaDB巨杉数据库支持哪些类型的数据库实例?"],
32
+ retry_btn="重试",
33
+ undo_btn="撤销",
34
+ clear_btn="清除",
35
+ submit_btn="提问",
36
+ ).queue().launch()