### What problem does this PR solve? ### Type of change - [x] Refactoringtags/v0.16.0
| def get_input(self): | def get_input(self): | ||||
| if self._param.debug_inputs: | if self._param.debug_inputs: | ||||
| return pd.DataFrame([{"content": v["value"]} for v in self._param.debug_inputs]) | |||||
| return pd.DataFrame([{"content": v["value"]} for v in self._param.debug_inputs if v.get("value")]) | |||||
| reversed_cpnts = [] | reversed_cpnts = [] | ||||
| if len(self._canvas.path) > 1: | if len(self._canvas.path) > 1: |
| return user[0] | return user[0] | ||||
| else: | else: | ||||
| return None | return None | ||||
| except Exception: | |||||
| logging.exception("load_user got exception") | |||||
| except Exception as e: | |||||
| logging.warning(f"load_user got exception {e}") | |||||
| return None | return None | ||||
| else: | else: | ||||
| return None | return None |
| "message": [{"role": "assistant", "content": dia.prompt_config["prologue"]}] | "message": [{"role": "assistant", "content": dia.prompt_config["prologue"]}] | ||||
| } | } | ||||
| ConversationService.save(**conv) | ConversationService.save(**conv) | ||||
| e, conv = ConversationService.get_by_id(conv["id"]) | |||||
| if not e: | |||||
| return get_data_error_result(message="Fail to new a conversation!") | |||||
| conv = conv.to_dict() | |||||
| return get_json_result(data=conv) | return get_json_result(data=conv) | ||||
| except Exception as e: | except Exception as e: | ||||
| return server_error_response(e) | return server_error_response(e) |
| def insert(cls, doc): | def insert(cls, doc): | ||||
| if not cls.save(**doc): | if not cls.save(**doc): | ||||
| raise RuntimeError("Database error (Document)!") | raise RuntimeError("Database error (Document)!") | ||||
| e, doc = cls.get_by_id(doc["id"]) | |||||
| if not e: | |||||
| raise RuntimeError("Database error (Document retrieval)!") | |||||
| e, kb = KnowledgebaseService.get_by_id(doc.kb_id) | |||||
| e, kb = KnowledgebaseService.get_by_id(doc["kb_id"]) | |||||
| if not KnowledgebaseService.update_by_id( | if not KnowledgebaseService.update_by_id( | ||||
| kb.id, {"doc_num": kb.doc_num + 1}): | kb.id, {"doc_num": kb.doc_num + 1}): | ||||
| raise RuntimeError("Database error (Knowledgebase)!") | raise RuntimeError("Database error (Knowledgebase)!") | ||||
| return doc | |||||
| return Document(**doc) | |||||
| @classmethod | @classmethod | ||||
| @DB.connection_context() | @DB.connection_context() |
| def get_data_error_result(code=settings.RetCode.DATA_ERROR, | def get_data_error_result(code=settings.RetCode.DATA_ERROR, | ||||
| message='Sorry! Data missing!'): | message='Sorry! Data missing!'): | ||||
| logging.exception(Exception(message)) | |||||
| result_dict = { | result_dict = { | ||||
| "code": code, | "code": code, | ||||
| "message": message} | "message": message} |
| FAILED_TASKS = 0 | FAILED_TASKS = 0 | ||||
| CURRENT_TASK = None | CURRENT_TASK = None | ||||
| class TaskCanceledException(Exception): | class TaskCanceledException(Exception): | ||||
| def __init__(self, msg): | def __init__(self, msg): | ||||
| self.msg = msg | self.msg = msg | ||||
| def set_progress(task_id, from_page=0, to_page=-1, prog=None, msg="Processing..."): | def set_progress(task_id, from_page=0, to_page=-1, prog=None, msg="Processing..."): | ||||
| global PAYLOAD | global PAYLOAD | ||||
| if prog is not None and prog < 0: | if prog is not None and prog < 0: | ||||
| STORAGE_IMPL.put(task["kb_id"], d["id"], output_buffer.getvalue()) | STORAGE_IMPL.put(task["kb_id"], d["id"], output_buffer.getvalue()) | ||||
| el += timer() - st | el += timer() - st | ||||
| except Exception: | except Exception: | ||||
| logging.exception("Saving image of chunk {}/{}/{} got exception".format(task["location"], task["name"], d["_id"])) | |||||
| logging.exception("Saving image of chunk {}/{}/{} got exception".format(task["location"], task["name"], d["id"])) | |||||
| raise | raise | ||||
| d["img_id"] = "{}-{}".format(task["kb_id"], d["id"]) | d["img_id"] = "{}-{}".format(task["kb_id"], d["id"]) | ||||
| if not c: | if not c: | ||||
| c = d["content_with_weight"] | c = d["content_with_weight"] | ||||
| c = re.sub(r"</?(table|td|caption|tr|th)( [^<>]{0,12})?>", " ", c) | c = re.sub(r"</?(table|td|caption|tr|th)( [^<>]{0,12})?>", " ", c) | ||||
| if not c: | |||||
| c = "None" | |||||
| cnts.append(c) | cnts.append(c) | ||||
| tk_count = 0 | tk_count = 0 | ||||
| return res, tk_count, vector_size | return res, tk_count, vector_size | ||||
| def do_handle_task(task): | def do_handle_task(task): | ||||
| task_id = task["id"] | task_id = task["id"] | ||||
| task_from_page = task["from_page"] | task_from_page = task["from_page"] |