def ml_qa(question: Question, auth: str = Depends(ensure_auth)): if question.batch_size > 10: return {"error": "batch_size too large. maximum is 10"} try: answers = bert_qa.qa.ask( question.question, n_answers=10, n_docs_considered=15, batch_size=question.batch_size, ) result = [] for answer in answers: result.append({ "answer": answer["answer"], "score": float(answer["confidence"]), "context": answer["context"], "full_answer": answer["full_answer"], "document": answer["reference"], }) return result except Exception as e: logger.error(f"error in /ml/question_answering: {type(e)}: {e}") return {"error": "who knows"}
def chowdown_migrate(session: Session, zip_file: Path): temp_dir = unpack_zip(zip_file) with temp_dir as dir: chow_dir = next(Path(dir).iterdir()) image_dir = app_dirs.TEMP_DIR.joinpath(chow_dir, "images") recipe_dir = app_dirs.TEMP_DIR.joinpath(chow_dir, "_recipes") failed_recipes = [] successful_recipes = [] for recipe in recipe_dir.glob("*.md"): try: new_recipe = read_chowdown_file(recipe) db.recipes.create(session, new_recipe.dict()) successful_recipes.append(new_recipe.name) except Exception as inst: session.rollback() logger.error(inst) failed_recipes.append(recipe.stem) failed_images = [] for image in image_dir.iterdir(): try: if image.stem not in failed_recipes: shutil.copy(image, app_dirs.IMG_DIR.joinpath(image.name)) except Exception as inst: logger.error(inst) failed_images.append(image.name) report = {"successful": successful_recipes, "failed": failed_recipes} migrate_images() return report
async def send_push( client: httpx.AsyncClient, apn: str, payload: schemas.ApnPayload, db: Session, user: models.User, ) -> bool: """Send a push notification to iOS Return True in case of success """ logger.info(f"Send notification to {user.username} (apn: {apn[:10]}...)") try: response = await client.post(f"https://{APPLE_SERVER}/3/device/{apn}", json=payload.dict()) response.raise_for_status() except httpx.RequestError as exc: logger.error(f"HTTP Exception for {exc.request.url} - {exc}") return False except httpx.HTTPStatusError as exc: logger.warning(f"{exc}") if response.status_code == 410: logger.info( f"Device token no longer active. Delete {apn} for user {user.username}" ) crud.remove_user_device_token(db, user, apn) return False logger.info(f"Notification sent to user {user.username}") return True
async def set_link(rqt: Request, rsp: Response, cookie: Optional[str] = Cookie(None)): try: # step 0 - get url url, schema = UrlLogic.parser_url(full_url=str(rqt.url), endpoint=endpoint_set) # step 1 - check and add cookie process_cookie(rsp, cookie) # step 2 - check url url_is_good, check_msg = UrlLogic.check(url, schema) if url_is_good is False: return schm.ReportAdd(ok=False, msg=check_msg, request_url=str(rqt.url)) # step 3 - generate a code added, code = await Linker.gen_code_add_link(url, cookie) if added is False: return schm.ReportAdd(ok=False, msg=settings.MSG_REJECT, request_url=str(rqt.url)) return schm.ReportAdd( ok=True, msg='ok', request_url=str(rqt.url), target_url=url, redirect_url=f'{host}{code}' ) except Exception as e: err_msg = settings.MSG_FAIL logger.error(f"{err_msg} - {cookie}") logger.error(e) return schm.ReportAdd(ok=False, msg=err_msg, request_url=str(rqt.url))
async def set_with(given: str, rqt: Request, rsp: Response, cookie: Optional[str] = Cookie(None)): try: # step 0 - get url url, schema = UrlLogic.parser_url(full_url=str(rqt.url), endpoint=endpoint_with, name=given) # step 1 - check and add cookie process_cookie(rsp, cookie) # step 2 - check url url_is_good, check_msg = UrlLogic.check(url, schema) if url_is_good is False: return schm.ReportAdd(ok=False, msg=check_msg, request_url=str(rqt.url)) # step 3 - try to add rlt = await Linker.add_redirect(code=given, link=url, cookie=cookie) if rlt is not schm.LinkerReject.PASS: return schm.ReportAdd(ok=False, msg=settings.MSG_REJECT, request_url=str(rqt.url)) return schm.ReportAdd( ok=True, msg='ok', request_url=str(rqt.url), target_url=url, redirect_url=f'{host}{given}' ) except Exception as e: err_msg = settings.MSG_FAIL logger.error(f"{err_msg} - {cookie}") logger.error(e) return schm.ReportAdd(ok=False, msg=err_msg, request_url=str(rqt.url))
def create_submission(execution: schemas.Execution): try: logger.debug( f"Start code submission request with payload: {execution}") result = celery_client.send_task( 'worker.execute_code', args=(execution.language, execution.code, execution.timeout)).wait(timeout=None, interval=0.1) result['submission_id'] = str(uuid.uuid4()) if not result['has_error']: result['results'] = python_result_extractor(result['raw_output']) else: result['results'] = [] logger.debug(f"Code submission succeeded with result: {result}") return result except ValueError as vle: logger.error(f"Invalid raw output test format, error: {str(vle)}") raise HTTPException(status_code=400, detail="Invalid test code format") except Exception as exc: logger.error( f"Worker could not process submission, error {exc}, payload: {execution}" ) raise HTTPException( status_code=500, detail="The server could not process the code execution")
def update_blog_site(site_id: int = Path(None, title="站点ID"), the_blog_site: UpdateBlogSite = None, current_user: User = Depends(get_current_user)): title = the_blog_site.title site_name = the_blog_site.site_name theme = the_blog_site.theme if current_user.user_type != 0: fail_response("用户权限不足") blog_site = BlogSite.filter(BlogSite.id == site_id).first() if blog_site is None: return fail_response('博客站点不存在') try: # 法一: query = BlogSite.update(title=title, site_name=site_name, theme=theme, update_time=str(datetime.now())).where( BlogSite.id == site_id) query.execute() # 法二: # blog_site.title = title # blog_site.site_name = site_name # blog_site.theme = theme # blog_site.save() return success_response('更新修改博客站点成功') except Exception as e: db.rollback() logger.error(f'更新修改博客站点失败,失败原因:{e}') return fail_response('更新修改博客站点失败')
def delete_article(article_id: str, current_user: User = Depends(get_current_user)): article = Article.filter(Article.id == article_id, Article.user_id == current_user.uuid).first() if not article: return fail_response('此文章不存在') title = article.title try: # 之前这个是 物理删除 # result = article.delete_instance() # 删除改为逻辑删除 query = Article.update(is_delete=True).where( Article.id == article_id, Article.user_id == current_user.uuid) result = query.execute() # print(result, "运行后的结果") if result: res = redis_client.exists("article_rank_list") if res: # 存在了但是这个文章 res_score = redis_client.zscore("article_rank_list", "article_%s_%s" % (article_id, title)) if res_score: # 删除之前的分数记录 redis_client.zrem("article_rank_list", "article_%s_%s" % (article_id, title)) return success_response('删除博客文章成功') return fail_response('删除博客文章失败') except Exception as e: db.rollback() logger.error(f'删除博客文章失败,失败原因:{e}') return fail_response('删除博客文章失败')
def add_static_node_to_db(node: Node, neigh_infos: List[Neighbor] = None) -> None: add_node(node.name, node.group) if neigh_infos: for neigh in neigh_infos: neigh.name = neigh.name if neigh.name else neigh.addr if get_node(neigh.name): add_link(node.name, neigh.name, neigh.node_iface, neigh.iface) add_link(neigh.name, node.name, neigh.iface, neigh.node_iface) add_fake_iface_stats(node.name, neigh.node_iface) add_fake_iface_utilization(node.name, neigh.node_iface) add_fake_iface_stats(neigh.name, neigh.iface) add_fake_iface_utilization(neigh.name, neigh.iface) else: logger.error( "Node's neighbor doesn't exist, we can't add the link") elif node.ifaces: for iface in node.ifaces: add_fake_iface_stats(node.name, iface) add_fake_iface_utilization(node.name, iface)
def reply_comment(create_comment: CreateComment, article_id: str = Path(2, description="博客文章id"), comment_id: str = Path(2, description="评论id"), current_user: User = Depends(get_current_user)): content = create_comment.content article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') if article.user_id == current_user.uuid: # 博主的情况 comment = Comment.filter(Comment.id == comment_id, Comment.article_id == article_id, Comment.user_id != current_user.uuid).first() else: # 用户 comment = Comment.filter(Comment.id == comment_id, Comment.article_id == article_id, Comment.user_id != current_user.uuid, Comment.parent_id != '').first() if not comment: return fail_response('此评论不存在或原评论你不能进行回复!') try: Comment.create(article_id=article_id, user_id=current_user.uuid, content=content, parent_id=comment.id) except Exception as e: db.rollback() logger.error(f'评论回复失败,失败原因:{e}') return fail_response('评论回复失败') return success_response('评论回复成功!')
def background_time_update(): global TIMEOUT, TIME now: int = int(time()) logger.error(f"bgtimeupd: {now}, {TIME}, {TIMEOUT}") if now - TIME > CACHED_TIME: TIMEOUT = True TIME = now logger.error(f"bgtimeupdEnd: {now}, {TIME}, {TIMEOUT}")
async def check_health(): db = router.db.get_session() try: db.check_health() except: logger.error("Unexpected error:", sys.exc_info()[0]) raise HTTPException(status_code=503, detail='db connection error') return 'OK'
async def session_commit(*args, **kwargs): db: Session = kwargs.get('db') try: return await func(*args, **kwargs) except Exception as e: logger.error('db operation error,here are details{}'.format(e)) logger.warning('transaction rollbacks') db.rollback()
async def validation_exception_handler(request: Request, exc: RequestValidationError): exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") # or logger.error(f'{exc}') logger.error(request, exc_str) content = {"status_code": 10422, "message": exc_str, "data": None} return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
async def get_config(name: str, db: DataBase = Depends(get_db)): config_collection = db.get_collection("configs") config_res = await config_collection.find_one({"name": name}) if config_res is None: message = f"Configuration {name} not found" logger.error(message) raise HTTPException(status_code=404, detail=message) return ConfigRespSchema(config=config_res["config"])
async def app(request: Request) -> Response: try: body = None if body_field: if is_body_form: body = await request.form() else: body_bytes = await request.body() if body_bytes: body = await request.json() except Exception as e: logger.error(f"Error getting request body: {e}") raise HTTPException( status_code=400, detail="There was an error parsing the body" ) from e solved_result = await solve_dependencies( request=request, dependant=dependant, body=body, dependency_overrides_provider=dependency_overrides_provider, ) values, errors, background_tasks, sub_response, _ = solved_result if errors: raise RequestValidationError(errors, body=body) else: raw_response = await run_endpoint_function( dependant=dependant, values=values, is_coroutine=is_coroutine ) if asyncio.iscoroutine(raw_response): raw_response = await raw_response if isinstance(raw_response, Response): if raw_response.background is None: raw_response.background = background_tasks return raw_response response_data = await serialize_response( field=response_field, response_content=raw_response, include=response_model_include, exclude=response_model_exclude, by_alias=response_model_by_alias, exclude_unset=response_model_exclude_unset, exclude_defaults=response_model_exclude_defaults, exclude_none=response_model_exclude_none, is_coroutine=is_coroutine, ) response = response_class( content=response_data, status_code=status_code, background=background_tasks, ) response.headers.raw.extend(sub_response.headers.raw) if sub_response.status_code: response.status_code = sub_response.status_code return response
def dumps_item(db: DbEngine, key: str, values: List[any]): if db is None: return try: vals = dumps(values) client = db.get_session() client.set(key, vals, ex=DEFAULT_TIMEOUT) except Exception: # ignore all exceptions logger.error("Unexpected error:", exc_info()[0]) pass
async def registration(email): logger.info(f'Receive request to create a new client: {email}') try: result = mongo_config.init_client(email) except: logger.error(f'Receive request to create a new client: {email}') if result["status"]: return {"message": email, "password": result["password"]} else: return result
def create_table(): try: if not db.dialect.has_table(db, "predictions"): prediction_table.create() logger.info("Prediction table created") else: logger.info("Prediction table already exists") pass except Exception as e: logger.error(f"Error when trying to create/connect to the db {e}")
def rename_image(original_slug, new_slug) -> Path: current_path = app_dirs.IMG_DIR.joinpath(original_slug) new_path = app_dirs.IMG_DIR.joinpath(new_slug) try: new_path = current_path.rename(new_path) except FileNotFoundError: logger.error(f"Image Directory {original_slug} Doesn't Exist") return new_path
def sample(): logger.critical('CRITICAL message') logger.error('ERROR message') logger.warning('WARNING message') logger.info('INFO message') logger.debug('DEBUG message') module() return 'hello, world'
def log_action(cls, msg): op = str(cls) current_user = cls.get_current_user() if current_user: email = current_user.email else: email = 'None' log = dm.ActionsLog(user=email, context=op, action=msg, origin="GraphQL API") log.save() logger.error(log.to_mongo().to_dict())
async def get_firmware(name: str, db: DataBase = Depends(get_db)): firmwares_collection = db.get_collection("firmwares") firmware = await firmwares_collection.find_one({"name": name}) if firmware is None: logger.error(f"No Firmware named {name}") return HTTPException(404, detail=f"No Firmware named {name}") firmware_file = io.BytesIO(firmware["firmware"]) firmware_file.name = firmware["name"] return StreamingResponse(firmware_file, media_type="application/octet-stream")
async def update(self, who: str, data: dict): try: await self.set_name(who, data["roomName"]) await self.set_open(who, data["open"]) await self.set_custom_cards(who, data["customCards"]) await self.set_selecting_time(who, data["time"]) await self.set_game_type(who, data["gameType"]) await self.set_paused(who, data["paused"]) self.set_room_password(data["password"]) except KeyError: logger.error(f"Received invalid settings: {data}")
def authenticate_user(username: str, password: str) -> bool: """Return True if the authentication is successful, False otherwise""" if username == "demo" and password == str(DEMO_ACCOUNT_PASSWORD): return True if AUTHENTICATION_METHOD == "ldap": return ldap_authenticate_user(username, password) elif AUTHENTICATION_METHOD == "url": return url_authenticate_user(username, password) else: logger.error(f"Invalid authentication method: {AUTHENTICATION_METHOD}") return False
def get_article_info(article_id: str): article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') try: up_count = ArticleUpDown.filter(ArticleUpDown.article_id == article.id, ArticleUpDown.is_up == True).count() except Exception as e: db.rollback() logger.error(f'更新博客文章失败,失败原因:{e}') return fail_response('删除博客文章失败') return success_response({"article_id": article_id, "up_count": up_count})
def get_user_by_name(name: str): """Handle requests to retrieve a single user from the simulated database. :param name: Name of the user to "search for" :return: The user data if found, HTTP 404 if not """ user_data = fakedb.get(name) if not user_data: logger.error(f"GET user for: '{name}', HTTP 404 not found") raise HTTPException(status_code=404, detail="User not found") logger.error(f"GET user for: '{name}', returning: {user_data}") return user_data
def loads_item(db: DbEngine, key: str): if db is None: return None try: client = db.get_session() vals = client.get(key) if vals is None: return None values = loads(vals) return values except Exception: # ignore all exceptions logger.error("Unexpected error:", exc_info()[0]) return None
def insert_prediction(image_id: str, prediction: str): conn = None try: conn = db.connect() insert = prediction_table.insert().values(image_id=image_id, prediction=prediction, timestamp=int(time())) return conn.execute(insert) except Exception as e: logger.error(f"Error when inserting to the db {e}") finally: if conn is not None: conn.close()
def get_from_db_or_cache(element: str, func=None, query=None): global TIMEOUT, CACHE if not CACHE.get(element) or TIMEOUT: if not func: return None logger.error(f"Oops, {element} not in cache, calling db") if query: CACHE[element] = func(query) else: CACHE[element] = func() TIMEOUT = False return CACHE[element]