Spaces:
Runtime error
Runtime error
Update functionality.py
Browse files- 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=
|
| 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 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
-
logging.info(f"\n---Academic style corrected:---\n {formal_text}\n")
|
| 261 |
elif goal == 'Fix Grammar':
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
else:
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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!"
|