edwinspace / app.py
edwinxx's picture
Update app.py
b4611bd
Raw
History Blame Contribute Delete
3.17 kB
import gradio as gr
import openai
from transformers import AutoTokenizer
import os
openai.api_key = os.environ.get("OPENAI_API_KEY")
def openai_summarize(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": "You are a helpful assistant that summarizes the emails between 2 parties in chronological order."}, {"role": "user", "content": f"summarize, maintaining the correct time sequence and make sure to include all the dates and at the end of the summary, add a Chinese sentence to sum up both parties' intents: {prompt}"}],
temperature = 0.1,
max_tokens=2000,
)
return response['choices'][0]['message']['content'].strip()
def generate_reply(prompt, context, system_role):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"You are {system_role} that generates English email replies based on given prompts and context."},
{"role": "user", "content": context},
{"role": "user", "content": prompt},
],
temperature = 0.7,
max_tokens=1900,
)
return response['choices'][0]['message']['content'].strip()
def count_tokens(tokenizer, text):
tokens = tokenizer.encode(text)
return len(tokens)
def summarize_chunks(text):
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-2.7B", model_max_length=4096)
tokens = tokenizer.encode(text)
chunk_size = 2000
chunks = [tokens[i:i + chunk_size] for i in range(0, len(tokens), chunk_size)]
summaries = []
for chunk in chunks:
text_chunk = tokenizer.decode(chunk)
summary = openai_summarize(text_chunk)
summaries.append(summary)
# Reverse the list of summaries
summaries.reverse()
return "\n".join(summaries)
'''
def gradio_interface(text, prompt, system_role):
summary = summarize_chunks(text)
reply = generate_reply(prompt, summary, system_role)
return summary, reply
'''
# 历史邮件内容超过2000才压缩,2000以内则直接作为语料
def gradio_interface(text, prompt, system_role):
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-2.7B", model_max_length=4096)
token_count = count_tokens(tokenizer, text)
if token_count > 2000:
summary = summarize_chunks(text)
else:
summary = text
reply = generate_reply(prompt, summary, system_role)
return summary, reply
inputs = [
gr.inputs.Textbox(lines=5, label="所有过往邮件"),
gr.inputs.Textbox(label="按以下提示生成回复内容"),
gr.inputs.Radio(["helpful assistant", "customer service at maxfull hair company", "customer service at mhot hair company", "social marketing specialist at maxfull hair company","social marketing specialist at mhot hair company"], label="设置AI角色"),
]
outputs = [
gr.outputs.Textbox(label="总结过往邮件(短邮件不总结)"),
gr.outputs.Textbox(label="邮件回复"),
]
iface = gr.Interface(fn=gradio_interface, inputs=inputs, outputs=outputs, title="AI回复邮件")
iface.launch()