Pupolina commited on
Commit
8de7105
1 Parent(s): f3051e2

Update functionality.py

Browse files
Files changed (1) hide show
  1. functionality.py +38 -16
functionality.py CHANGED
@@ -231,7 +231,7 @@ def _chat_stream(initial_text: str, parts: list):
231
  )
232
  inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
233
  streamer = TextIteratorStreamer(
234
- tokenizer=tokenizer, skip_prompt=True, timeout=60.0, skip_special_tokens=True
235
  )
236
  generation_kwargs = {
237
  **inputs,
@@ -253,22 +253,44 @@ def predict(goal: str, parts: list, context: str):
253
  logging.info("No context was provided!")
254
  elif goal == 'Fix Academic Style':
255
  formal_text = ""
256
- for new_text in fix_academic_style(context):
257
- formal_text = new_text
258
- yield formal_text
 
 
 
 
 
 
 
 
259
 
260
- logging.info(f"\n---Academic style corrected:---\n {formal_text}\n")
261
  elif goal == 'Fix Grammar':
262
- full_response = ""
263
- for new_text in fix_grammar(context):
264
- full_response = new_text
265
- yield full_response
266
-
267
- logging.info(f"\n---Grammar corrected:---\n{full_response}\n")
 
 
 
 
 
 
 
 
268
  else:
269
- full_response = ""
270
- for new_text in _chat_stream(context, parts):
271
- full_response = new_text
272
- yield full_response
 
 
 
 
273
 
274
- logging.info(f"\nThe text was generated!\n{full_response}")
 
 
 
 
231
  )
232
  inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
233
  streamer = TextIteratorStreamer(
234
+ tokenizer=tokenizer, skip_prompt=True, timeout=160.0, skip_special_tokens=True
235
  )
236
  generation_kwargs = {
237
  **inputs,
 
253
  logging.info("No context was provided!")
254
  elif goal == 'Fix Academic Style':
255
  formal_text = ""
256
+ try:
257
+ for new_text in fix_academic_style(context):
258
+ formal_text = new_text
259
+ yield formal_text
260
+ if not formal_text:
261
+ yield "Generation failed or timed out. Please try again!"
262
+
263
+ logging.info(f"\n---Academic style corrected:---\n {formal_text}\n")
264
+ except Exception as e:
265
+ logging.error(f"Error in 'Fix Academic Style' occured: {e}")
266
+ yield "Try to wait a little bit and resend your request!"
267
 
 
268
  elif goal == 'Fix Grammar':
269
+ try:
270
+ full_response = ""
271
+ for new_text in fix_grammar(context):
272
+ full_response = new_text
273
+ yield full_response
274
+
275
+ if not full_response:
276
+ yield "Generation failed or timed out. Please try again!"
277
+
278
+ logging.info(f"\n---Grammar corrected:---\n{full_response}\n")
279
+ except Exception as e:
280
+ logging.error(f"Error in 'Fix Grammar' occured: {e}")
281
+ yield "Try to wait a little bit and resend your request!"
282
+
283
  else:
284
+ try:
285
+ full_response = ""
286
+ for new_text in _chat_stream(context, parts):
287
+ full_response = new_text
288
+ yield full_response
289
+
290
+ if not full_response:
291
+ yield "Generation failed or timed out. Please try again!"
292
 
293
+ logging.info(f"\nThe text was generated!\n{full_response}")
294
+ except Exception as e:
295
+ logging.error(f"Error in 'Write Text' occured: {e}")
296
+ yield "Try to wait a little bit and resend your request!"