Spaces:
Runtime error
Runtime error
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() |