File size: 4,040 Bytes
092d5c3
 
96f24e7
8544ad2
 
 
 
f739b4d
8544ad2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d48ccdd
 
 
 
8544ad2
 
 
 
 
 
 
 
 
 
 
 
 
f739b4d
 
 
 
 
 
 
 
 
 
 
 
 
 
8544ad2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
os.system('pip install openai')
os.system('pip install python-docx')
import openai
from openai.error import RateLimitError


openai.api_key = ""
prompt = """
我需要你帮助我完成会议记录的总结和梳理。我会给你提供一长段会议记录,其中包含多个参会者的对话记录。你需要完成如下的任务:
1. 提取出会议主要讨论的几个主题或者要点。
2. 针对会议的每一个主题,请你找到所有相关的会议记录,并将它们梳理总结,为我生成有条理的摘要。

在输出时,请注意以下几点:
1. 输出内容中避免口语化内容
2. 输出格式是层级列表
"""

def get_chatgpt_reply(query, context=[]):
    context += [query]
    try:
        completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", 
            messages=[{"role": "user", "content": '\n\n'.join(context)[:4096]}]
        )
        response = completion.choices[0].message["content"]
    except RateLimitError:
        response = "服务器请求次数过多,请稍后重新尝试"
    context += [response]
    responses = [(u,b) for u,b in zip(context[::2], context[1::2])]
    return responses, context


def get_chatgpt_summary(content):
    try:
        completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", 
            messages=[{"role": "system", "content": prompt}, 
                      {"role": "user", "content": content}], 
            temperature=0.5
        )
        response = completion.choices[0].message["content"]
    except RateLimitError:
        response = "服务器请求次数过多,请稍后重新尝试"
    return response


import gradio as gr
from docx import Document


def upload_file(file):
    doc = Document(file.name)
    content = ""
    for para in doc.paragraphs:
        content += para.text
        content += '\n'
    return content

def download_file(content):
    doc = Document()
    doc.add_paragraph(content)
    doc.save("会议纪要.docx")
    return None

def set_api_key(api_key):
    openai.api_key = api_key
    return None


with gr.Blocks(theme=gr.themes.Default(text_size='lg', radius_size='sm')) as demo:
    with gr.Column():
        # 产品介绍
        title = gr.Markdown("# <center>ChatMeeting</center>")
        desc = gr.Markdown("<center>让AI帮你整理会议纪要\n\n支持.docx文件</center>")
    
    with gr.Column():
        # 文件上传
        file_input = gr.File(file_types=[".docx"], label="原始文稿", interactive=True)
        upload_btn = gr.Button(value="上传")

    with gr.Row():
        with gr.Column():
            # api key
            api_input = gr.Textbox(label="API Key", placeholder="请输入API Key", type="password")
            api_btn = gr.Button(value="设置")
            api_btn.click(fn=set_api_key, inputs=api_input, outputs=None)
            # 文字展示
            with gr.Tab("原文"):
                # 原文
                content_box = gr.Textbox(label="文稿内容")
                download_btn = gr.Button(value="下载")
            with gr.Tab("总结"):
                # 总结
                summary_box = gr.Textbox(label="总结内容")
                download_btn = gr.Button(value="下载")
        with gr.Column():
            # 对话交互
            chatbot = gr.Chatbot(label="对话内容").style(height=400)
            state = gr.State([])
            txt = gr.Textbox(label="用户", placeholder="请输入内容")
            with gr.Row():
                summary = gr.Button(value="一键梳理")
                clear = gr.Button(value="清空")
            
            summary.click(fn=get_chatgpt_summary, inputs=content_box, outputs=summary_box)
            txt.submit(get_chatgpt_reply, [txt, state], [chatbot, state])
            clear.click(lambda: None, None, chatbot, queue=False)

    upload_btn.click(fn=upload_file, inputs=file_input, outputs=content_box)
    download_btn.click(fn=download_file, inputs=content_box, outputs=None)


demo.launch()