File size: 4,455 Bytes
73dd6e8
 
 
 
 
cab993a
73dd6e8
 
 
 
 
 
 
 
 
bb65ea5
73dd6e8
 
 
 
 
bb65ea5
 
73dd6e8
 
 
 
 
 
bb65ea5
73dd6e8
 
 
 
bb65ea5
73dd6e8
 
 
 
 
 
 
bb65ea5
73dd6e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b36655e
 
73dd6e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import openai
import tiktoken
from multiprocessing.pool import ThreadPool

enc = tiktoken.get_encoding("cl100k_base")

MODES = {
    "Short summary": "Succintly summarize the following meeting transcript in a single paragraph.",
    "Detailed summary": "Summarize the following meeting transcript. The summary should include all the important points discussed in the meeting.",
    "Action points": "Summarize the following meeting transcript in form of action points.",
    "Further actions": "Who and what should be done next? Summarize the following meeting transcript in form of action points.",
    "Custom": "",
}

SUMMARY_PROMPT = "Summarize the following meeting in very great detail, in English. The summary should include all the important points discussed in the meeting."

def summarize_part(text, api_key):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
        { "role": "system", "content": f"You are a meeting organizer. You want to succintly summarize a meeting. {SUMMARY_PROMPT}" },
        { "role": "user", "content": "Summarize the following transcript in English: " + text },
        ],
        api_key=api_key,
    )
    return response["choices"][0]["message"]["content"]

def shorten_text(text, api_key):
    """ Split text into chunks of 3000 tokens and summarize each chunk. """
    chunks = []
    words = text.split()
    for i in range(0, len(words), 1500):
        chunk = ""
        while len(enc.encode(chunk)) < 3000 and i < len(words):
            chunk += words[i] + " "
            i += 1
        chunks.append(chunk)

    with ThreadPool(4) as pool:
        shortened = pool.starmap(summarize_part, zip(chunks, [api_key]*len(chunks)))
    
    return ". ".join(shortened)

def modify_text(text, api_key, command, custom_command=None):
    if command == "Custom":
        prompt = custom_command
    else:
        prompt = MODES[command]

    if len(enc.encode(text)) < 4096:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
            { "role": "system", "content": f"You are a meeting organizer. You want to summarize a meeting. You are given the following transcript of the meeting. {prompt}" },
            { "role": "user", "content": text },
            ],
            api_key=api_key,
        )
        return response["choices"][0]["message"]["content"]
    else:
        prompt = prompt.replace("meeting transcript", "meeting parts")
        shortened = text
        while len(enc.encode(shortened)) > 4096:
            shortened = shorten_text(shortened, api_key)
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
            { "role": "system", "content": f"You are a meeting organizer. You want to summarize a meeting. You are given the following summary of the meeting parts. {prompt}" },
            { "role": "user", "content": shortened },
            ],
            api_key=api_key,
        )
        return response["choices"][0]["message"]["content"]

with gr.Blocks() as demo:
    gr.Markdown("# Meeting Summary")
    gr.Markdown("This app uses OpenAI's GPT-3 to summarize a meeting transcript. You can either use the default commands or enter your own one. The app will automatically split the transcript into parts if it is too long for GPT-3 to handle.")
    with gr.Row():
        with gr.Column():
            api_key = gr.Textbox(lines=1, label="OpenAI API Key")
            input_text = gr.Textbox(lines=15, label="Meeting Transcript")
        with gr.Column():
            command = gr.Dropdown(list(MODES.keys()), label="Command", value="Short summary")
            custom_command = gr.Textbox(lines=2, label="Custom command", visible=False, value="Summarize the following meeting transcript in a single paragraph. The summary should include all the important points discussed in the meeting.")
            output_text = gr.Textbox(lines=10, label="Summary")


    def show_command(command):
        if command == "Custom":
            return {custom_command: gr.update(visible=True)}
        else:
            return {custom_command: gr.update(visible=False)}
        
    command.change(show_command, command, custom_command)

    button = gr.Button(label="Process")
    button.click(modify_text, [input_text, api_key, command, custom_command], output_text)

demo.title = "Meeting Summary"
demo.launch()