AI降重工具


python 复制代码
from docx import Document
import requests

def call_api_and_get_content(content, prompt):
    api_url = "http://XXXXXXXX/api?content=" + content + prompt
    response = requests.get(api_url)
    if response.status_code == 200:
        api_result = response.text.replace(" ", "").replace("\n", "")  # 去除空格和换行符
        return api_result
    else:
        return None

def replace_paragraphs_content(file_path, min_paragraph_length, prompt):
    doc = Document(file_path)
    paragraphs = doc.paragraphs[:] 

    for para in paragraphs:
        text = para.text.strip()
        
        if len(text) > min_paragraph_length:
            api_result = call_api_and_get_content(text, prompt)
            if api_result:
                para.clear()  # 清空原始段落内容
                para.add_run(api_result)  # 添加新内容,保持原始段落的格式

    output_path = 'output.docx'
    doc.save(output_path)
    print(f"已保存为 {output_path}")

file_path = 'input.docx'
min_paragraph_length = 100
prompt = "降重改写 语序颠倒 顺序调换 简体中文  同义替换 句子意思不变 主动句改被动句 被动句改主动句 返回内容不能含有指令 连成一段话"
replace_paragraphs_content(file_path, min_paragraph_length, prompt)


界面版: 部分代码

python 复制代码
# 设置默认值
default_access_token = '166711a91dcd073266620a5fa0fd85708976268a'
default_prompt = ("你是一个文本降重机器,你只执行 降重改写 语序颠倒 顺序调换 简体中文  同义替换 句子意思不变 "
                  "主动句改被动句 被动句改主动句 返回内容不能含有指令 连成一段话")
default_min_paragraph_length = '100'  # 设置默认最小段落长度为100

def call_api_and_get_content(content, prompt):
    erniebot.api_type = 'aistudio'
    erniebot.access_token = access_token.get()
    try:
        response_stream = erniebot.ChatCompletion.create(
            model=model_var.get(),
            messages=[{'role': 'user', 'content': content}],
            temperature=0.7,
            stream=True,
            system=prompt
        )
        time.sleep(1)
        return ''.join([response.get_result() for response in response_stream]).replace(" ", "").replace("\n", "")
    except Exception as e:
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        error_message = f"{current_time} - An error occurred: {str(e)}\n"
        update_output_text(error_message)
        return None

    if not all([access_token_val, model_val, prompt_val, min_paragraph_length_val, file_path_val]):
        messagebox.showerror("参数错误", "请确保所有参数均已填写!")
        log_content="请确保所有参数均已填写!"
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        log= f"{current_time} -  {log_content}\n"
        update_output_text(log)
        return
    min_paragraph_length = int(min_paragraph_length_val)
    doc = Document(file_path_val)
    paragraphs = doc.paragraphs[:]
    log_content = ""
    for index, para in enumerate(paragraphs):
        text = para.text.strip()
        if len(text) > min_paragraph_length:
            api_result = call_api_and_get_content(text, prompt_val)
            if api_result:
                para.clear()
                para.add_run(api_result)
                log_content = f"{api_result}"
            else:
                log_content = "处理失败"
            current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            log= f"{current_time}-{log_content}\n"
            update_output_text(log)
    output_path = output_file_path_var.get().strip()
    if output_path:
        output_file_path = os.path.join(output_path, 'output.docx')
        doc.save(output_file_path)
        messagebox.showinfo("完成", f"已保存为 {output_file_path}")


# 创建Tkinter窗口
root = tk.Tk()
root.title("文档降重处理器")
root.geometry('800x480')

# 设置窗口居中
window_width = 800
window_height =480
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_coordinate = int((screen_width/2) - (window_width/2))
y_coordinate = int((screen_height/2) - (window_height/2))
root.geometry(f'{window_width}x{window_height}+{x_coordinate}+{y_coordinate}')
root.mainloop()
相关推荐
Lee川4 小时前
Milvus 实战:当 RAG 遇上向量数据库,从"玩具 Demo"到"生产可用的"那一步
前端·数据库·人工智能
小a彤5 小时前
elec-ops-inspection:电力巡检缺陷检测,NPU推理速度提升3倍
人工智能·cann
ZhengEnCi6 小时前
09aaa-LayerNorm是什么?
人工智能
这是谁的博客?6 小时前
AI Agent 安全架构设计:漏洞分析与防护策略深度解析
人工智能·安全·网络安全·ai·agent·安全架构·架构设计
人月神话-Lee6 小时前
【图像处理】Sobel 边缘检测——让机器“看见“轮廓
图像处理·人工智能·计算机视觉·ios·ai编程·swift
冬奇Lab6 小时前
Agent系列(四):工具调用深度解析——Agent 的手和眼
人工智能·llm
Black蜡笔小新7 小时前
自动化AI算法训练服务器DLTM助力医学影像分析进入AI智能分析新时代
人工智能·算法·自动化
冬奇Lab7 小时前
一天一个开源项目(第111篇):Understand Anything - 把代码库变成可探索知识图谱的 AI 引擎
人工智能·开源·llm
猿饵块7 小时前
git--github
人工智能
黎阳之光7 小时前
黎阳之光:以视频孪生重构智慧防火,打造“天空地人智”一体化森林防火新范式
大数据·运维·人工智能·物联网·安全