-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(mzapi): 重构项目并添加新功能 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .nlp import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import requests | ||
|
|
||
|
|
||
| def access_token(ak, sk): | ||
| url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}" | ||
| payload = "" | ||
| headers = { | ||
| 'Content-Type': 'application/json', | ||
| 'Accept': 'application/json' | ||
| } | ||
| response = requests.get(url, headers=headers, data=payload,timeout=30) | ||
| return response.json().get("access_token") |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from tencentcloud.ocr.v20181119 import ocr_client, models | ||
|
|
||
| from ...utlis.ImageValidator import ImageValidator | ||
| from ...utlis.verification import Verification | ||
|
|
||
|
|
||
| class GeneralAccurateOCR: | ||
|
|
@@ -31,6 +32,7 @@ def __init__(self, secret_id=None, secret_key=None, token=None, log_level=None): | |
| Raises: | ||
| TencentCloudSDKException: 初始化失败时抛出 | ||
| """ | ||
| self.sanitize_log_data = Verification | ||
| self.logger = logging.getLogger(__name__) | ||
| if log_level is not None: | ||
| self.logger.setLevel(log_level) | ||
|
|
@@ -72,18 +74,27 @@ def recognize(self,ImageBase64,ImageUrl,IsWords,EnableDetectSplit,IsPdf,PdfPageN | |
| :param ConfigID: 配置ID支持: OCR -- 通用场景 MulOCR--多语种场景 | ||
| """ | ||
| try: | ||
| self.logger.info("开始执行OCR识别") | ||
| self.logger.debug(f"输入参数: ImageBase64={ImageBase64}, ImageUrl={ImageUrl}, IsWords={IsWords}, EnableDetectSplit={EnableDetectSplit}, IsPdf={IsPdf}, PdfPageNumber={PdfPageNumber}, EnableDetectText={EnableDetectText}, ConfigID={ConfigID}") | ||
|
|
||
| if ImageBase64 is None and ImageUrl is None: | ||
| if (ImageBase64 is None or str(ImageBase64).strip() == "") and (ImageUrl is None or str(ImageUrl).strip() == ""): | ||
| error_msg = "ImageBase64和ImageUrl必须提供一个" | ||
| self.logger.error(error_msg) | ||
| raise ValueError(error_msg) | ||
|
|
||
| if ImageUrl: | ||
| self.logger.debug(f"验证图片URL: {ImageUrl}") | ||
| self.logger.debug("验证图片URL: %s", ImageUrl) | ||
| self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"]) | ||
| self.logger.debug("图片URL验证通过") | ||
| self.logger.debug("图片Base64验证通过") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| self.logger.info("开始执行OCR识别") | ||
| self.logger.debug("输入参数: ImageBase64=%s, ImageUrl=%s, IsWords=%s, EnableDetectSplit=%s, IsPdf=%s, PdfPageNumber=%s, EnableDetectText=%s, ConfigID=%s", | ||
| self.sanitize_log_data.sanitize_log_data(ImageBase64,100), | ||
| ImageUrl, | ||
| IsWords, | ||
| EnableDetectSplit, | ||
| IsPdf, | ||
| PdfPageNumber, | ||
| EnableDetectText, | ||
| ConfigID) | ||
| req = models.GeneralAccurateOCRRequest() | ||
| params = { | ||
| "ImageBase64": ImageBase64, | ||
|
|
@@ -99,7 +110,7 @@ def recognize(self,ImageBase64,ImageUrl,IsWords,EnableDetectSplit,IsPdf,PdfPageN | |
| self.logger.info("正在向腾讯云OCR API发送请求...") | ||
| resp = self.client.GeneralAccurateOCR(req) | ||
| self.logger.info("OCR识别请求成功完成") | ||
| self.logger.debug(f"响应数据: {resp.to_json_string()}") | ||
| self.logger.debug("响应数据: %s", self.sanitize_log_data.sanitize_log_data(resp.to_json_string(),50)) | ||
| return resp.to_json_string() | ||
|
|
||
| except TencentCloudSDKException as err: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| from tencentcloud.ocr.v20181119 import ocr_client, models | ||
|
|
||
| from ...utlis.ImageValidator import ImageValidator | ||
| from ...utlis.verification import Verification | ||
|
|
||
|
|
||
| class GeneralBasicOCR: | ||
|
|
@@ -30,6 +31,7 @@ def __init__(self, secret_id=None, secret_key=None, token=None, log_level=None): | |
| Raises: | ||
| TencentCloudSDKException: 初始化失败时抛出 | ||
| """ | ||
| self.sanitize_log_data = Verification | ||
| self.logger = logging.getLogger(__name__) | ||
| if log_level is not None: | ||
| self.logger.setLevel(log_level) | ||
|
|
@@ -106,15 +108,23 @@ def recognize(self, ImageBase64=None, ImageUrl=None, Scene=None, LanguageType=No | |
| """ | ||
| try: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (code-quality): Explicitly raise from a previous error ( |
||
| self.logger.info("开始执行OCR识别") | ||
| self.logger.debug(f"输入参数: ImageUrl={ImageUrl}, LanguageType={LanguageType}, IsPdf={IsPdf}") | ||
| safe_base64 = self.sanitize_log_data.sanitize_log_data(ImageBase64,100) | ||
| self.logger.debug("输入参数: ImageBase64=%s, ImageUrl=%s, Scene=%s, LanguageType=%s, IsPdf=%s, PdfPageNumber=%s, IsWords=%s", | ||
| safe_base64, | ||
| ImageUrl, | ||
| Scene, | ||
| LanguageType, | ||
| IsPdf, | ||
| PdfPageNumber, | ||
| IsWords) | ||
|
|
||
| if ImageBase64 is None and ImageUrl is None: | ||
| error_msg = "ImageBase64和ImageUrl必须提供一个" | ||
| self.logger.error(error_msg) | ||
| raise ValueError(error_msg) | ||
|
|
||
| if ImageUrl: | ||
| self.logger.debug(f"验证图片URL: {ImageUrl}") | ||
| self.logger.debug("验证图片URL: %s", ImageUrl) | ||
| self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"]) | ||
| self.logger.debug("图片URL验证通过") | ||
|
|
||
|
|
@@ -136,7 +146,8 @@ def recognize(self, ImageBase64=None, ImageUrl=None, Scene=None, LanguageType=No | |
| # 执行OCR识别 | ||
| resp = self.client.GeneralBasicOCR(req) | ||
| self.logger.info("OCR识别请求成功完成") | ||
| self.logger.debug(f"响应数据: {resp.to_json_string()}") # 只记录前200字符避免日志过大 | ||
| resp_json = resp.to_json_string() | ||
| self.logger.debug("响应数据: %s", self.sanitize_log_data.sanitize_log_data(resp_json,50)) | ||
|
|
||
| return resp.to_json_string() | ||
|
|
||
|
|
@@ -145,4 +156,4 @@ def recognize(self, ImageBase64=None, ImageUrl=None, Scene=None, LanguageType=No | |
| raise err | ||
| except Exception as e: | ||
| self.logger.error(f"处理OCR请求时发生意外错误: {str(e)}", exc_info=True) | ||
| raise TencentCloudSDKException("OCR处理错误", str(e)) | ||
| raise TencentCloudSDKException("OCR处理错误", str(e)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
simplify-empty-collection-comparison)raise-from-previous-error)