Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| import openai | |
| import datetime | |
| today = datetime.date.today() | |
| class OpenAIUtils: | |
| def use_openai_chatgpt_base(cls, prompt): | |
| """ | |
| Use OpenAI's GPT-3 model, Davinci, to generate text based on the given prompt | |
| """ | |
| openai.api_key = os.getenv('OPENAI_API_KEY') | |
| try: | |
| res = openai.ChatCompletion.create( | |
| # res = openai.Completion.create( | |
| # model="gpt-3.5-turbo", | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": f"""あなたは日本人の有能なアシスタントでメール返信の代行をしています。 | |
| 事前情報:必要に応じて利用ください。 | |
| ・今日は{today} | |
| ・面談日程の提案をする場合は箇条書きで、MM/DD(曜日)hh:mm〜hh:mmで書くこと(デフォルトは1時間) | |
| """}, | |
| {"role": "user", "content": f"{prompt}"}, | |
| ] | |
| ) | |
| string = res.choices[0]['message']['content'] | |
| except openai.error.InvalidRequestError as e: | |
| print(f"Error: {e}") | |
| return None | |
| return string | |
| # @classmethod | |
| # def use_openai_davinci_base(cls, prompt): | |
| # """ | |
| # Use OpenAI's GPT-3 model, Davinci, to generate text based on the given prompt | |
| # """ | |
| # openai.api_key = os.getenv('OPENAI_API_KEY') | |
| # try: | |
| # res = openai.Completion.create( | |
| # model="text-davinci-003", | |
| # prompt=prompt, | |
| # max_tokens=1024, | |
| # temperature=0.5 | |
| # ) | |
| # string = res.choices[0].text | |
| # except openai.error.InvalidRequestError as e: | |
| # print(f"Error: {e}") | |
| # return None | |
| # return string | |
| class CalqMail: | |
| def generate_reply_mail_prompt(cls, mail, reply, company, name): | |
| self_intro = f"{company}の{name}(と申します or です)。" | |
| prompt = f""" | |
| {mail} | |
| 上記の[返信したいメール文章]に対して、以下のメール文面を参考に返信してください。 | |
| メール文面はじまり | |
| [返信したいメール文章の会社名] | |
| [返信したいメール文章の名前]様 | |
| {self_intro} | |
| [いい感じの返信文] + {reply}をいい感じの返答ぶんにする | |
| どうぞよろしくお願いいたします。 | |
| {name} | |
| メール文面終わり | |
| """ | |
| # print(prompt) | |
| return OpenAIUtils.use_openai_chatgpt_base(prompt) | |
| def post_process_mail_reply(mail_reply): | |
| remove_list = ["敬具", "尊敬する", "メール文面はじまり", "メール文面終わり", | |
| "[返信したいメール文章]", "[返信したいメール文章の会社名]", "[返信したいメール文章の名前]", "[いい感じの返信文]", "【返信文】"] | |
| for item in remove_list: | |
| mail_reply = mail_reply.replace(item, "") | |
| return mail_reply | |
| def greet(mail, reply, company, name): | |
| mail_reply = CalqMail.generate_reply_mail_prompt(mail, reply, company, name) | |
| mail_reply = CalqMail.post_process_mail_reply(mail_reply) | |
| return mail_reply | |
| input_mail = gr.Textbox(label="返信したいメール文章を添付ください") | |
| input_reply = gr.Textbox(label="あなたから返信したい内容を書いてください(オプション:具体例、3月中旬日程を3つほど提案したい)") | |
| input_company = gr.Textbox(label="あなたの会社名(オプション)") | |
| input_name = gr.Textbox(label="あなたの名字(オプション)") | |
| output_mail = gr.Textbox(label="送信メール提案文章(最大1分ほどお待ちください)") | |
| description=""" | |
| """ | |
| article = """ | |
| """ | |
| iface = gr.Interface( | |
| fn=greet, | |
| inputs=[input_mail, input_reply, input_company, input_name], | |
| outputs=[output_mail], | |
| title="", | |
| description=description, | |
| article=article | |
| ) | |
| iface.launch() | |