chrisvnz's picture
Update app.py
52ba5a4
import gradio as gr
import openai
import tiktoken
from multiprocessing.pool import ThreadPool
enc = tiktoken.get_encoding("cl100k_base")
MODES = {
"Short summary": "Succinctly 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": "",
}
def summarize_part(text, api_key, model, topic):
SUMMARY_PROMPT = f"Summarize the following meeting on {topic} in very great detail. The summary should include all the important points discussed in the meeting."
response = openai.ChatCompletion.create(
model=model,
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. {SUMMARY_PROMPT}" },
{ "role": "user", "content": text },
],
api_key=api_key,
)
return response["choices"][0]["message"]["content"]
def shorten_text(text, api_key, model, topic):
chunks = []
words = text.split()
for i in range(0, len(words), 1500):
chunk = ""
while len(enc.encode(chunk)) < 4000 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), [model]*len(chunks), [topic]*len(chunks)))
return "".join(shortened)
def modify_text(file, api_key, command, model, topic, custom_command=None):
text = file.read().decode("utf-8")
if command == "Custom":
prompt = custom_command
else:
prompt = MODES[command]
if len(enc.encode(text)) < 4096:
response = openai.ChatCompletion.create(
model=model,
messages=[
{ "role": "system", "content": f"You are a meeting organizer. You want to summarize a meeting on {topic}. 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, model, topic)
response = openai.ChatCompletion.create(
model=model,
messages=[
{ "role": "system", "content": f"You are a meeting organizer. You want to summarize a meeting on {topic}. 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"]
def show_command(command):
if command == "Custom":
return {custom_command: gr.update(visible=True)}
else:
return {custom_command: gr.update(visible=False)}
with gr.Blocks() as demo:
gr.Markdown("# Meeting Summarizer")
gr.Markdown("#### Short Summary | Detailed Summary | Action points | Further Actions | Custom Command")
with gr.Row():
with gr.Column():
api_key = gr.Textbox(lines=1,type='password', label="OpenAI API Key")
file = gr.inputs.File(label="Upload Meeting Transcript")
# model = gr.Dropdown(options=['gpt-3.5-turbo', 'gpt-4'], label="OpenAI Model", default='gpt-3.5-turbo')
model = gr.Dropdown(["gpt-3.5-turbo", "gpt-4"], label="Model", info="OpenAI LLM")
topic = gr.Textbox(lines=1, label="Meeting Topic")
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")
command.change(show_command, command, custom_command)
button = gr.Button(label="Process")
button.click(modify_text, [file, api_key, command, model, topic, custom_command], output_text)
demo.title = "Meeting Summarizer-Demo"
demo.launch()