CoolFace
Apppublic

AITECHPRODUCTS/githubtest

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
server.py50 linesDownload Raw Back to root
1import argparse2 3import gradio as gr4from loguru import logger5 6from chat_completion import ChatCompletion7 8parser = argparse.ArgumentParser()9parser.add_argument('--api_key_path', type=str, default='./openai_api_key')10parser.add_argument('--log_path', type=str, default='./log.txt')11parser.add_argument('--share', action='store_true', default=False)12parser.add_argument('--welcome', type=str, default='Say something to ChatGPT here ...')13parser.add_argument('--title', type=str, default='ChatGPT')14parser.add_argument('--setting', type=str, default=None)15args = parser.parse_args()16 17bot = ChatCompletion(api_key_path=args.api_key_path)18logger.add(args.log_path)19 20with gr.Blocks(title=args.title) as demo:21    chatbot = gr.Chatbot(show_label=False)22    msg = gr.TextArea(show_label=False, placeholder=args.welcome)23    send_btn = gr.Button('Send')24    retry_btn = gr.Button('Retry')25    reset_btn = gr.Button('Reset')26 27    def send(user_message, history):28        if not user_message:29            return '', history30 31        logger.info(f'[MSG] {user_message}')32        response = bot(user_message, setting=args.setting) if user_message != 'retry' else bot.retry()33        logger.info(f'[ANS] {response}')34        return '', history + [[user_message, response]]35 36    def reset():37        bot.reset()38        logger.info('[RESET]')39        return None, [[None, None]]40 41    def retry(history):42        return send('retry', history)43 44    send_btn.click(send, inputs=[msg, chatbot], outputs=[msg, chatbot], show_progress=True)45    reset_btn.click(reset, inputs=None, outputs=[msg, chatbot])46    retry_btn.click(retry, inputs=chatbot, outputs=[msg, chatbot])47 48 49demo.launch(share=args.share)50