【赤石科技】自动分析迷惑行为大赏机
houmy
·
·
科技·工程
api_key="你的 DeepSeek api key"
import openai
from colorama import *
import subprocess
import sys
import random
import os
import tqdm
import json
cli=openai.OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
def answer(context):
response = cli.chat.completions.create(
model="deepseek-chat",
messages=context,
stream=True,
temperature=0
)
full_response = ""
# print(Fore.LIGHTBLUE_EX, end="")
for chunk in response:
if chunk.choices[0].delta.content is not None:
content = chunk.choices[0].delta.content
full_response += content
# print(content, end="", flush=True)
# print(Style.RESET_ALL)
return full_response
sys_prompt = """
你是一个从代码中识别一些特殊信息的机器人,能够从代码中识别以下模式并进行相应的标记:
- ccf/csp/noip/noi/ioi/ak/luogu/kkksc03/chen_che/lin_toto/rp/脏话/网络梗/游戏或厂商名等变量名、函数名或宏名
- 注释 freopen/fin/fout 等文件读写,或 freopen 的文件名与题目指定的 .in/.out 文件名不符(不包含正常的文件读写)
- 特判数据并直接输出
- 2333....../6666....../114514/1919810 等特殊数字
- 大段注释、字符画等,或诗歌、歌词、文段等
- 非 C++ 的代码
- 未注释的调试输出
- 以及其它非常规的代码、注释及实现方法
你会被给予一个选手的所有题目的代码文件,并对其进行标注。具体地,对于每段输入的代码,你会输出一个 true/false 标签,表示这段代码是否包含特殊信息,并在后面附加解释。
示例 1 输入:
输入文件名:foo.in
输出文件名:foo.out
// freopen("foo.in", "r", stdin); // 注释
示例 1 输出:
true
代码注释了文件读写,并附加了没有意义的注释。
"""
def format_prompt(file):
filename = file.split("/")[-1]
prompt = f"""
文件名:{filename}
输入文件名:{filename.split(".")[0] + ".in"}
输出文件名:{filename.split(".")[0] + ".out"}
{open(file, "r").read()}
"""
# print(prompt)
return prompt
def build_context(file):
return [{"role": "system", "content": sys_prompt}, {"role": "user", "content": format_prompt(file)}]
def anotate_code(file):
context = build_context(file)
response = answer(context)
return response.split("\n")[0].strip().lower() == "true", response.split("\n")[1].strip()
data = []
for pwd, dirs, files in os.walk("."):
for file in files:
if file.endswith(".cpp"):
detect_result, explanation = anotate_code(os.path.join(pwd, file))
data.append({"file": os.path.join(pwd, file), "detect_result": detect_result, "explanation": explanation})
if detect_result:
print(Fore.GREEN + f"{pwd}/{file}: yes {explanation}" + Style.RESET_ALL)
else:
print(Fore.RED + f"{pwd}/{file}: no {explanation}" + Style.RESET_ALL)
json.dump({"data": data}, open("result.json", "w"))