* Serving Flask app 'app'
 * Debug mode: on
/usr/local/lib/python3.10/site-packages/gradio/components/chatbot.py:242: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style dictionaries with 'role' and 'content' keys.
  warnings.warn(
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:7860
 * Running on http://10.28.66.128:7860
Press CTRL+C to quit
 * Restarting with stat
/usr/local/lib/python3.10/site-packages/gradio/node_server.py:136: UserWarning: Unexpected error while starting Node server: signal only works in main thread of the main interpreter. Trying next port...
  warnings.warn(
/usr/local/lib/python3.10/site-packages/gradio/components/chatbot.py:242: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style dictionaries with 'role' and 'content' keys.
  warnings.warn(
 * Debugger is active!
 * Debugger PIN: 304-500-340
/usr/local/lib/python3.10/site-packages/gradio/node_server.py:136: UserWarning: Unexpected error while starting Node server: signal only works in main thread of the main interpreter. Trying next port...
  warnings.warn(
Cannot start Node server on any port in the range 7861-7960.
Please install Node 20 or higher and set the environment variable GRADIO_NODE_PATH to the path of your Node executable.
You can explicitly specify a port by setting the environment variable GRADIO_NODE_PORT.
Exception in thread Thread-2 (run_gradio):
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/home/user/app/app.py", line 89, in run_gradio
    gr.Interface(fn=chat_with_model, inputs=["text", "slider", "slider", "number", "chatbot"], outputs=["chatbot", "number", "text", "text"]).launch(share=True, inline=False)
  File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 2515, in launch
    ) = http_server.start_server(
  File "/usr/local/lib/python3.10/site-packages/gradio/http_server.py", line 156, in start_server
    raise OSError(
OSError: Cannot find empty port in range: 7860-7860. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the `server_port` parameter to `launch()`.
Cannot start Node server on any port in the range 7861-7960.
Please install Node 20 or higher and set the environment variable GRADIO_NODE_PATH to the path of your Node executable.
You can explicitly specify a port by setting the environment variable GRADIO_NODE_PORT.
Exception in thread Thread-2 (run_gradio):
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/home/user/app/app.py", line 89, in run_gradio
    gr.Interface(fn=chat_with_model, inputs=["text", "slider", "slider", "number", "chatbot"], outputs=["chatbot", "number", "text", "text"]).launch(share=True, inline=False)
  File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 2515, in launch
    ) = http_server.start_server(
  File "/usr/local/lib/python3.10/site-packages/gradio/http_server.py", line 156, in start_server
    raise OSError(
OSError: Cannot find empty port in range: 7860-7860. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the `server_port` parameter to `launch()`.
 


import gradio as gr
from flask import Flask, request, jsonify
from gradio_client import Client

# Flask アプリケーションを作成
app = Flask(__name__)

# Gradio のチャット機能
def chat_with_model(inputs, top_p, temperature, chat_counter, chatbot):
    client = Client("yuntian-deng/ChatGPT4")
    result = client.predict(
        inputs=inputs,
        top_p=top_p,
        temperature=temperature,
        chat_counter=chat_counter,
        chatbot=chatbot,
        api_name="/predict_1"
    )
    return result[0], result[1], result[2], result[3]

# GradioのUI定義
def chatbot_component_ui():
    with gr.Blocks() as demo:
        with gr.Row():
            gr.Markdown("# ChatGPT4 Chatbot using /predict_1 API")
        
        with gr.Row():
            user_input = gr.Textbox(label="Type an input and press Enter", placeholder="Enter your message here...", lines=2)
        
        with gr.Row():
            top_p_slider = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.1, maximum=1.0, value=1.0, step=0.01)
            temperature_slider = gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
        
        with gr.Row():
            chat_counter_box = gr.Number(label="Chat Counter", value=0, precision=0)
            chatbot_history = gr.Chatbot(label="Chat History", value=[])
        
        with gr.Row():
            submit_button = gr.Button("Submit")
        
        with gr.Row():
            chatbot_output = gr.Chatbot(label="Chatbot Response")
            chat_counter_output = gr.Number(label="Updated Chat Counter")
            status_code_output = gr.Textbox(label="Status Code from OpenAI Server", lines=1)
            user_input_output = gr.Textbox(label="Echoed User Input", lines=2)
        
        submit_button.click(
            fn=chat_with_model,
            inputs=[user_input, top_p_slider, temperature_slider, chat_counter_box, chatbot_history],
            outputs=[chatbot_output, chat_counter_output, status_code_output, user_input_output]
        )

    return demo

# Flask のエンドポイント設定
@app.route('/v1/chat/completions', methods=['POST'])
def chat_completions():
    # リクエストからJSONデータを受け取る
    data = request.json
    
    user_input = data.get('inputs')
    top_p = data.get('top_p', 1.0)
    temperature = data.get('temperature', 1.0)
    chat_counter = data.get('chat_counter', 0)
    chatbot_history = data.get('chatbot', [])
    
    # モデルを呼び出して結果を取得
    result = chat_with_model(user_input, top_p, temperature, chat_counter, chatbot_history)
    
    # 結果をJSON形式で返す
    response = {
        "choices": [
            {
                "message": {"role": "assistant", "content": result[0]},
                "finish_reason": "stop"
            }
        ],
        "usage": {
            "total_tokens": result[1],
            "prompt_tokens": result[2],
            "completion_tokens": result[3]
        }
    }
    
    return jsonify(response)

# Gradio のインターフェースをFlaskサーバーで動かす
def run_gradio():
    gr.Interface(fn=chat_with_model, inputs=["text", "slider", "slider", "number", "chatbot"], outputs=["chatbot", "number", "text", "text"]).launch(share=True, inline=False)

if __name__ == '__main__':
    # Gradioインターフェースは別スレッドで起動
    import threading
    gr_thread = threading.Thread(target=run_gradio)
    gr_thread.start()

    # Flaskアプリケーションを起動
    app.run(debug=True, port=7860, host="0.0.0.0")