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()
相关推荐
whaosoft-14334 分钟前
51c自动驾驶~合集7
人工智能
刘晓倩4 小时前
Coze智能体开发实战-多Agent综合实战
人工智能·coze
石迹耿千秋5 小时前
迁移学习--基于torchvision中VGG16模型的实战
人工智能·pytorch·机器学习·迁移学习
路人蛃8 小时前
通过国内扣子(Coze)搭建智能体并接入discord机器人
人工智能·python·ubuntu·ai·aigc·个人开发
CV-杨帆8 小时前
论文阅读:arxiv 2025 A Survey of Large Language Model Agents for Question Answering
论文阅读·人工智能·语言模型
绝顶大聪明8 小时前
【深度学习】神经网络-part2
人工智能·深度学习·神经网络
加百力8 小时前
AI助手竞争白热化,微软Copilot面临ChatGPT的9亿下载挑战
人工智能·microsoft·copilot
Danceful_YJ9 小时前
16.使用ResNet网络进行Fashion-Mnist分类
人工智能·深度学习·神经网络·resnet
香蕉可乐荷包蛋9 小时前
AI算法之图像识别与分类
人工智能·学习·算法
张较瘦_10 小时前
[论文阅读] 人工智能 + 软件工程 | 当LLMs遇上顺序API调用:StateGen与StateEval如何破解测试难题?
论文阅读·人工智能