Download bert/cpp_postprocess/a.py from jva96160/DriveBert: direct link, hf CLI and curl.
- Browser
- Download file 2.07 kB
-
https://huggingface.co/jva96160/DriveBert/resolve/main/bert/cpp_postprocess/a.py
- Command line
-
hf download hf://jva96160/DriveBert/bert/cpp_postprocess/a.py
-
curl -L -o a.py https://huggingface.co/jva96160/DriveBert/resolve/main/bert/cpp_postprocess/a.py
2.07 kB
| import ctypes | |
| import os | |
| # 加載共享函式庫 | |
| # 請將 'libpostprocess.so' 替換為您實際編譯的檔案名稱 (例如: 'libpostprocess.dll' 或 'libpostprocess.dylib') | |
| lib_path = "postprocess.so" | |
| lib = ctypes.CDLL(lib_path) | |
| # 設定函式參數和回傳值的資料型別 | |
| # postprocess_c 函式接收兩個 C 字串並回傳一個 C 字串 | |
| lib.postprocess_c.argtypes = [ctypes.c_char_p, ctypes.c_char_p] | |
| lib.postprocess_c.restype = ctypes.c_char_p | |
| def call_postprocess(query, pred_class): | |
| """ | |
| 呼叫 C++ postprocess 函式並處理回傳結果。 | |
| """ | |
| # 將 Python 字串編碼為 bytes,以符合 C++ 的 char* 參數要求 | |
| query_bytes = query.encode('utf-8') | |
| pred_class_bytes = pred_class.encode('utf-8') | |
| # 呼叫 C++ 函式 | |
| result_ptr = lib.postprocess_c(query_bytes, pred_class_bytes) | |
| # 將回傳的 C 字串解碼為 Python 字串 | |
| if result_ptr: | |
| result_str = result_ptr.decode('utf-8') | |
| # 釋放 C++ 函式動態分配的記憶體 | |
| return result_str | |
| else: | |
| return None | |
| # --- 測試案例 --- | |
| # 測試 1: 查詢空調溫度 | |
| query_1 = "請開始關頂棚遮陽簾" | |
| pred_class_1 = "POWER_SUNSHADE" | |
| output_1 = call_postprocess(query_1, pred_class_1) | |
| print(f"查詢: {query_1}") | |
| print(f"預測類別: {pred_class_1}") | |
| print(f"結果:\n{output_1}\n") | |
| query_1 = "麻煩關閉第一排左座前除霜" | |
| pred_class_1 = "HVAC_DEFROSTER%false" | |
| output_1 = call_postprocess(query_1, pred_class_1) | |
| print(f"查詢: {query_1}") | |
| print(f"預測類別: {pred_class_1}") | |
| print(f"結果:\n{output_1}\n") | |
| query_1 = "麻煩關掉遮陽簾" | |
| pred_class_1 = "POWER_SUNSHADE" | |
| output_1 = call_postprocess(query_1, pred_class_1) | |
| print(f"查詢: {query_1}") | |
| print(f"預測類別: {pred_class_1}") | |
| print(f"結果:\n{output_1}\n") | |
| # # 測試 1: 查詢空調溫度 | |
| # query_1 = "冷到一直打噴嚏" | |
| # pred_class_1 = "HVAC_TEMPERATURE_SET" | |
| # output_1 = call_postprocess(query_1, pred_class_1) | |
| # print(f"查詢: {query_1}") | |
| # print(f"預測類別: {pred_class_1}") | |
| # print(f"結果:\n{output_1}\n") |