Guanzheng commited on
Commit
a572663
·
1 Parent(s): bf0e3a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -34
app.py CHANGED
@@ -86,20 +86,40 @@ def build_chat():
86
  prompt = conv.get_prompt()
87
  return prompt
88
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  from fastchat.model import get_conversation_template
90
 
91
  @spaces.GPU
92
  def generate(
93
  message: str,
94
- chat_history: list[tuple[str, str]],
95
  system_prompt: str,
 
96
  max_new_tokens: int = 1024,
97
- temperature: float = 0.6,
98
- top_p: float = 0.9,
99
  top_k: int = 50,
100
- repetition_penalty: float = 1.2,
101
  ) -> Iterator[str]:
 
 
 
 
 
102
  conv = get_conversation_template("vicuna")
 
 
103
  conv.append_message(conv.roles[0], message)
104
  conv.append_message(conv.roles[1], None)
105
  prompt = conv.get_prompt()
@@ -108,7 +128,7 @@ def generate(
108
  # for user, assistant in chat_history:
109
  # conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
110
  # conversation.append({"role": "user", "content": message})
111
-
112
  # chat = tokenizer.apply_chat_template(conversation, tokenize=False)
113
  inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
114
  if len(inputs) > MAX_INPUT_TOKEN_LENGTH:
@@ -136,34 +156,34 @@ def generate(
136
  yield "".join(outputs)
137
 
138
 
139
- def generate_with_pdf(
140
- message: str,
141
- chat_history: list[tuple[str, str]],
142
- system_prompt: str,
143
- input_pdf: BytesIO = None,
144
- max_new_tokens: int = 1024,
145
- temperature: float = 0.6,
146
- top_p: float = 0.9,
147
- top_k: int = 50,
148
- repetition_penalty: float = 1.2,
149
- ) -> Iterator[str]:
150
- if input_pdf is not None:
151
- pdf_text = process_pdf(input_pdf)
152
- # print(pdf_text)
153
- message += f"\nThis is the beginning of a pdf\n{pdf_text}This is the end of a pdf\n"
154
- yield from generate(
155
- message,
156
- chat_history,
157
- system_prompt,
158
- max_new_tokens,
159
- temperature,
160
- top_p,
161
- top_k,
162
- repetition_penalty
163
- )
164
 
165
  chat_interface = gr.ChatInterface(
166
- fn=generate_with_pdf,
167
  additional_inputs=[
168
  gr.Textbox(label="System prompt", lines=6),
169
  gr.File(label="PDF File", accept=".pdf"),
@@ -179,14 +199,14 @@ chat_interface = gr.ChatInterface(
179
  minimum=0.1,
180
  maximum=4.0,
181
  step=0.1,
182
- value=0.6,
183
  ),
184
  gr.Slider(
185
  label="Top-p (nucleus sampling)",
186
  minimum=0.05,
187
  maximum=1.0,
188
  step=0.05,
189
- value=0.9,
190
  ),
191
  gr.Slider(
192
  label="Top-k",
@@ -200,7 +220,7 @@ chat_interface = gr.ChatInterface(
200
  minimum=1.0,
201
  maximum=2.0,
202
  step=0.05,
203
- value=1.2,
204
  ),
205
  ],
206
  stop_btn=None,
 
86
  prompt = conv.get_prompt()
87
  return prompt
88
 
89
+
90
+ import re
91
+
92
+ def replace_repeated_spaces_and_newlines(text):
93
+ # Replace repeated spaces with a single space
94
+ text = re.sub(r'\s+', ' ', text)
95
+
96
+ # Replace repeated newlines with a single newline
97
+ text = re.sub(r'\n+', '\n', text)
98
+
99
+ return text
100
+
101
  from fastchat.model import get_conversation_template
102
 
103
  @spaces.GPU
104
  def generate(
105
  message: str,
106
+ chat_history,
107
  system_prompt: str,
108
+ input_pdf: BytesIO = None,
109
  max_new_tokens: int = 1024,
110
+ temperature: float = 0.7,
111
+ top_p: float = 1.0,
112
  top_k: int = 50,
113
+ repetition_penalty: float = 1.0,
114
  ) -> Iterator[str]:
115
+ if input_pdf is not None:
116
+ pdf_text = process_pdf(input_pdf)
117
+ # print(pdf_text)
118
+ pdf_text = replace_repeated_spaces_and_newlines(pdf_text)
119
+ message += f"\nThis is the beginning of a pdf\n{pdf_text}This is the end of a pdf\n"
120
  conv = get_conversation_template("vicuna")
121
+ if system_prompt is not None:
122
+ conv.set_system_message(system_prompt)
123
  conv.append_message(conv.roles[0], message)
124
  conv.append_message(conv.roles[1], None)
125
  prompt = conv.get_prompt()
 
128
  # for user, assistant in chat_history:
129
  # conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
130
  # conversation.append({"role": "user", "content": message})
131
+ print(prompt[500:1000])
132
  # chat = tokenizer.apply_chat_template(conversation, tokenize=False)
133
  inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
134
  if len(inputs) > MAX_INPUT_TOKEN_LENGTH:
 
156
  yield "".join(outputs)
157
 
158
 
159
+ # def generate_with_pdf(
160
+ # message: str,
161
+ # chat_history,
162
+ # system_prompt: str,
163
+ # input_pdf: BytesIO = None,
164
+ # max_new_tokens: int = 1024,
165
+ # temperature: float = 0.6,
166
+ # top_p: float = 0.9,
167
+ # top_k: int = 50,
168
+ # repetition_penalty: float = 1.2,
169
+ # ) -> Iterator[str]:
170
+ # if input_pdf is not None:
171
+ # pdf_text = process_pdf(input_pdf)
172
+ # # print(pdf_text)
173
+ # message += f"\nThis is the beginning of a pdf\n{pdf_text}This is the end of a pdf\n"
174
+ # yield from generate(
175
+ # message,
176
+ # chat_history,
177
+ # system_prompt,
178
+ # max_new_tokens,
179
+ # temperature,
180
+ # top_p,
181
+ # top_k,
182
+ # repetition_penalty
183
+ # )
184
 
185
  chat_interface = gr.ChatInterface(
186
+ fn=generate,
187
  additional_inputs=[
188
  gr.Textbox(label="System prompt", lines=6),
189
  gr.File(label="PDF File", accept=".pdf"),
 
199
  minimum=0.1,
200
  maximum=4.0,
201
  step=0.1,
202
+ value=0.7,
203
  ),
204
  gr.Slider(
205
  label="Top-p (nucleus sampling)",
206
  minimum=0.05,
207
  maximum=1.0,
208
  step=0.05,
209
+ value=1.0,
210
  ),
211
  gr.Slider(
212
  label="Top-k",
 
220
  minimum=1.0,
221
  maximum=2.0,
222
  step=0.05,
223
+ value=1.0,
224
  ),
225
  ],
226
  stop_btn=None,