|
|
"""
|
|
|
无人机决策系统 - 分析引擎模块
|
|
|
作者:刘宇杰
|
|
|
日期:2025-07-13
|
|
|
功能说明:
|
|
|
本模块负责统一调度文本、图像分析,调用ERNIE大模型API,生成分析结果和打击计划。
|
|
|
"""
|
|
|
|
|
|
from typing import Dict, Any, List
|
|
|
from datetime import datetime
|
|
|
from config.settings import settings
|
|
|
from .ernie_client import ErnieClient
|
|
|
from .image_processor import ImageProcessor
|
|
|
from .text_processor import TextProcessor
|
|
|
import json
|
|
|
|
|
|
class AnalysisEngine:
|
|
|
"""
|
|
|
分析引擎类。
|
|
|
负责协调文本和图像分析,处理分析结果,生成无人机打击计划。
|
|
|
"""
|
|
|
|
|
|
def __init__(self):
|
|
|
"""
|
|
|
初始化分析引擎。
|
|
|
创建ERNIE客户端、图像处理器和文本处理器实例。
|
|
|
"""
|
|
|
self.ernie_client = ErnieClient()
|
|
|
self.image_processor = ImageProcessor()
|
|
|
self.text_processor = TextProcessor()
|
|
|
|
|
|
def analyze_text(self, text_content: str, filename: str) -> Dict[str, Any]:
|
|
|
"""
|
|
|
分析文本内容。
|
|
|
参数:
|
|
|
text_content (str): 文本内容。
|
|
|
filename (str): 原始文件名。
|
|
|
返回:
|
|
|
dict: 分析结果。
|
|
|
"""
|
|
|
# 保存文本
|
|
|
text_path = self.text_processor.save_uploaded_text(text_content, filename)
|
|
|
if not text_path:
|
|
|
return {"error": "文本保存失败"}
|
|
|
# 提取关键信息(可选)
|
|
|
key_info = self.text_processor.extract_key_info(text_content)
|
|
|
# 调用文心大模型进行深入分析
|
|
|
ernie_result = self.ernie_client.analyze_text(text_content)
|
|
|
# 合并结果
|
|
|
result = {
|
|
|
"type": "text",
|
|
|
"file_path": text_path,
|
|
|
"key_info": key_info,
|
|
|
"ernie_analysis": self._validate_ernie_result(ernie_result),
|
|
|
"timestamp": datetime.now().isoformat()
|
|
|
}
|
|
|
return result
|
|
|
|
|
|
def analyze_image(self, image_data: bytes, filename: str) -> Dict[str, Any]:
|
|
|
"""
|
|
|
分析图像内容。
|
|
|
参数:
|
|
|
image_data (bytes): 图像二进制数据。
|
|
|
filename (str): 原始文件名。
|
|
|
返回:
|
|
|
dict: 分析结果。
|
|
|
"""
|
|
|
# 保存图像
|
|
|
image_path = self.image_processor.save_uploaded_image(image_data, filename)
|
|
|
if not image_path:
|
|
|
return {"error": "图像保存失败"}
|
|
|
# 预处理图像
|
|
|
processed_image = self.image_processor.process_image_for_analysis(image_path)
|
|
|
if not processed_image:
|
|
|
return {"error": "图像处理失败"}
|
|
|
# 调用文心大模型进行深入分析
|
|
|
ernie_result = self.ernie_client.analyze_image(image_path)
|
|
|
# 合并结果
|
|
|
result = {
|
|
|
"type": "image",
|
|
|
"file_path": image_path,
|
|
|
"processed_image": processed_image[:100] + "..." if processed_image else None,
|
|
|
"ernie_analysis": self._validate_ernie_result(ernie_result),
|
|
|
"timestamp": datetime.now().isoformat()
|
|
|
}
|
|
|
return result
|
|
|
|
|
|
def generate_strike_plan(self, analysis_results: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
|
"""
|
|
|
根据分析结果生成无人机打击计划。
|
|
|
参数:
|
|
|
analysis_results (List[Dict[str, Any]]): 分析结果列表。
|
|
|
返回:
|
|
|
dict: 打击计划。
|
|
|
"""
|
|
|
# 构建更结构化的上下文
|
|
|
context_parts = []
|
|
|
for result in analysis_results:
|
|
|
analysis = result.get("ernie_analysis", {})
|
|
|
if isinstance(analysis, dict):
|
|
|
context_parts.append(f"### {result.get('type', 'unknown').upper()} ANALYSIS ###")
|
|
|
for key, value in analysis.items():
|
|
|
context_parts.append(f"{key}: {value}")
|
|
|
else:
|
|
|
context_parts.append(str(analysis))
|
|
|
context = "\n".join(context_parts)
|
|
|
# 构造messages为list,增加前置条件说明
|
|
|
messages = [
|
|
|
{
|
|
|
"role": "user",
|
|
|
"content": (
|
|
|
"前置条件:发动打击的为无人机。\n"
|
|
|
"你是一个军事指挥专家,请根据以下分析结果生成详细的无人机打击计划:\n"
|
|
|
f"{context}\n"
|
|
|
"请严格按照如下结构化JSON格式返回:\n"
|
|
|
"{\n"
|
|
|
" \"打击计划\": {\n"
|
|
|
" \"打击目标优先级\": \"...\",\n"
|
|
|
" \"打击方式选择\": \"...\",\n"
|
|
|
" \"具体打击步骤\": \"...\",\n"
|
|
|
" \"注意事项\": \"...\"\n"
|
|
|
" }\n"
|
|
|
"}\n"
|
|
|
"不要输出任何注释、解释或markdown代码块,只返回纯JSON。请确保每一项内容都尽量详细、专业,便于指挥员直接参考。"
|
|
|
)
|
|
|
}
|
|
|
]
|
|
|
return self.ernie_client._call_ernie(messages)
|
|
|
|
|
|
def _validate_ernie_result(self, result: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
"""
|
|
|
验证并标准化ERNIE返回的结果。
|
|
|
参数:
|
|
|
result (dict): ERNIE原始返回结果。
|
|
|
返回:
|
|
|
dict: 验证后的结果。
|
|
|
"""
|
|
|
if "error" in result:
|
|
|
return result
|
|
|
if "analysis" in result:
|
|
|
# 尝试从文本中提取结构化数据
|
|
|
content = result["analysis"]
|
|
|
if isinstance(content, str) and content.strip().startswith("{") and content.strip().endswith("}"):
|
|
|
try:
|
|
|
return json.loads(content)
|
|
|
except json.JSONDecodeError:
|
|
|
pass
|
|
|
return result
|
|
|
return result
|
|
|
|
|
|
def unused_function_1():
|
|
|
a = 12345
|
|
|
b = '无用字符串'
|
|
|
c = [i for i in range(10)]
|
|
|
return None
|
|
|
|
|
|
class UnusedClassA:
|
|
|
def method_a(self):
|
|
|
pass
|
|
|
def method_b(self):
|
|
|
x = 0
|
|
|
y = 1
|
|
|
return x + y
|
|
|
|
|
|
unused_var_1 = 987654321
|
|
|
unused_list_1 = [None] * 10
|
|
|
|
|
|
def unused_function_7():
|
|
|
x = 0
|
|
|
for i in range(20):
|
|
|
x += i * 2
|
|
|
return x
|
|
|
|
|
|
def unused_function_8():
|
|
|
s = ''
|
|
|
for i in range(10):
|
|
|
s += str(i) + ','
|
|
|
return s
|
|
|
|
|
|
class UnusedClassG:
|
|
|
def __init__(self):
|
|
|
self.data = [i for i in range(10)]
|
|
|
def get_sum(self):
|
|
|
return sum(self.data)
|
|
|
|
|
|
class UnusedClassH:
|
|
|
def __init__(self):
|
|
|
self.flag = True
|
|
|
def toggle(self):
|
|
|
self.flag = not self.flag
|
|
|
def is_active(self):
|
|
|
return self.flag
|
|
|
|
|
|
unused_var_7 = [i*3 for i in range(15)]
|
|
|
unused_var_8 = {i: chr(65+i) for i in range(10)}
|
|
|
unused_var_9 = (1,2,3,4,5)
|
|
|
unused_var_10 = 'analysis_unused'
|
|
|
|
|
|
def unused_function_19():
|
|
|
total = 0
|
|
|
for i in range(100):
|
|
|
total += i
|
|
|
return total
|
|
|
|
|
|
def unused_function_20():
|
|
|
s = ''
|
|
|
for i in range(50):
|
|
|
s += str(i) + '-'
|
|
|
return s
|
|
|
|
|
|
class UnusedClassS:
|
|
|
def __init__(self):
|
|
|
self.values = [i for i in range(50)]
|
|
|
def get_max(self):
|
|
|
return max(self.values)
|
|
|
def get_min(self):
|
|
|
return min(self.values)
|
|
|
|
|
|
class UnusedClassT:
|
|
|
def __init__(self):
|
|
|
self.flag = False
|
|
|
def enable(self):
|
|
|
self.flag = True
|
|
|
def disable(self):
|
|
|
self.flag = False
|
|
|
def status(self):
|
|
|
return self.flag
|
|
|
|
|
|
unused_var_26 = [i for i in range(100)]
|
|
|
unused_var_27 = {i: i*2 for i in range(50)}
|
|
|
unused_var_28 = (i for i in range(30))
|
|
|
unused_var_29 = 'analysis_more_unused' |