Witold Wydmański commited on
Commit
73dd6e8
·
1 Parent(s): 7922b6f
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import tiktoken
4
+ from multiprocessing.pool import ThreadPool
5
+
6
+ enc = tiktoken.encoding_for_model('gpt-3.5-turbo')
7
+
8
+ MODES = {
9
+ "Short summary": "Succintly summarize the following meeting transcript in a single paragraph.",
10
+ "Detailed summary": "Summarize the following meeting transcript. The summary should include all the important points discussed in the meeting.",
11
+ "Action points": "Summarize the following meeting transcript in form of action points.",
12
+ "Further actions": "Who and what should be done next? Summarize the following meeting transcript in form of action points.",
13
+ "Custom": "",
14
+ }
15
+
16
+ SUMMARY_PROMPT = "Summarize the following meeting in very great detail. The summary should include all the important points discussed in the meeting."
17
+
18
+ def summarize_part(text, api_key):
19
+ response = openai.ChatCompletion.create(
20
+ model="gpt-3.5-turbo",
21
+ messages=[
22
+ { "role": "system", "content": f"You are a meeting organizer. You want to summarize a meeting. You are given the following transcript of the meeting. {SUMMARY_PROMPT}" },
23
+ { "role": "user", "content": text },
24
+ ],
25
+ api_key=api_key,
26
+ )
27
+ return response["choices"][0]["message"]["content"]
28
+
29
+ def shorten_text(text, api_key):
30
+ # Split into chunks so that each chunk is less than 3000 words (not characters!)
31
+ # Overlap by halves.
32
+ chunks = []
33
+ words = text.split()
34
+ for i in range(0, len(words), 1500):
35
+ chunk = ""
36
+ while len(enc.encode(chunk)) < 4000 and i < len(words):
37
+ chunk += words[i] + " "
38
+ i += 1
39
+ chunks.append(chunk)
40
+
41
+ with ThreadPool(4) as pool:
42
+ shortened = pool.starmap(summarize_part, zip(chunks, [api_key]*len(chunks)))
43
+
44
+ return "".join(shortened)
45
+
46
+ def modify_text(text, api_key, command, custom_command=None):
47
+ if command == "Custom":
48
+ prompt = custom_command
49
+ else:
50
+ prompt = MODES[command]
51
+
52
+ if len(enc.encode(text)) < 4096:
53
+ response = openai.ChatCompletion.create(
54
+ model="gpt-3.5-turbo",
55
+ messages=[
56
+ { "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}" },
57
+ { "role": "user", "content": text },
58
+ ],
59
+ api_key=api_key,
60
+ )
61
+ return response["choices"][0]["message"]["content"]
62
+ else:
63
+ prompt = prompt.replace("meeting transcript", "meeting parts")
64
+ shortened = text
65
+ while len(enc.encode(shortened)) > 4096:
66
+ shortened = shorten_text(shortened, api_key)
67
+ response = openai.ChatCompletion.create(
68
+ model="gpt-3.5-turbo",
69
+ messages=[
70
+ { "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}" },
71
+ { "role": "user", "content": shortened },
72
+ ],
73
+ api_key=api_key,
74
+ )
75
+ return response["choices"][0]["message"]["content"]
76
+
77
+ with gr.Blocks() as demo:
78
+ with gr.Row():
79
+ with gr.Column():
80
+ api_key = gr.Textbox(lines=1, label="OpenAI API Key")
81
+ input_text = gr.Textbox(lines=15, label="Meeting Transcript")
82
+ with gr.Column():
83
+ command = gr.Dropdown(list(MODES.keys()), label="Command", value="Short summary")
84
+ 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.")
85
+ output_text = gr.Textbox(lines=10, label="Summary")
86
+
87
+
88
+ def show_command(command):
89
+ if command == "Custom":
90
+ return {custom_command: gr.update(visible=True)}
91
+ else:
92
+ return {custom_command: gr.update(visible=False)}
93
+
94
+ command.change(show_command, command, custom_command)
95
+
96
+ button = gr.Button(label="Process")
97
+ button.click(modify_text, [input_text, api_key, command, custom_command], output_text)
98
+
99
+ demo.title = "Meeting Summary"
100
+ demo.launch()