import gradio as gr
import random
import os
import sys
import importlib
from sample.aipass_client import execute

def generate_text(message):
    svcID = 's6fb9d652'
    data_md = importlib.import_module(svcID)
    data_md.request_data['header']['app_id'] = data_md.APPId

    queryMsg_path = data_md.request_data['payload']['text']['text']
    print("queryMsg_path ", queryMsg_path)
    with open(queryMsg_path, 'w+') as f:
        f.write(message)

    return execute(data_md.request_url, data_md.request_data, "POST", data_md.APPId, data_md.APIKey, data_md.APISecret)


def respond(message, chat_history):
    msg = generate_text(message=message)
    chat_history.append([message, msg])
    return "", chat_history
  

def main ():
    with gr.Blocks() as demo:
        with gr.Column(min_width=800):
            chatbot = gr.Chatbot()
            with gr.Row():
                message = gr.Textbox(min_width=730)
            clear = gr.ClearButton([message, chatbot])

            message.submit(respond, [message, chatbot], [message, chatbot])
    
    demo.queue().launch( server_name="0.0.0.0")