File size: 2,875 Bytes
8e21496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e5e795
 
 
 
 
 
8e21496
8e5e795
8e21496
8e5e795
 
8e21496
 
 
 
 
 
 
 
 
b080112
 
8e21496
 
 
f292630
 
 
 
 
 
8e21496
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
import requests
import json
    
def upload_text_to_server(text, url, fname):
    try:
        # テキストを送信
        payload = {"content": text, "filename": fname}
        response = requests.post(url, data=payload)

        # HTTPステータスコードを確認
        response.raise_for_status()  # エラーがあれば例外を発生させる
        return response.status_code, response.text

    except requests.exceptions.RequestException as e:
        # エラー処理
        print(f"Error occurred during the request: {e}")
        return None, str(e)

# 自然言語にする関数
def json_to_text(outname, param_array, tablename):
    parms = eval(param_array)
    # Fetch JSON data from the API
    dataid = requests.get("https://www.ryhintl.com/dbjson/getjson?sqlcmd=select * from "+tablename)

    # Decode the JSON response
    data_str = dataid.content.decode('utf-8')
    data = json.loads(data_str)

    final_context = ""
    for item in data:
        text = ""
        # Dynamically fetch values based on the parameter array
        for param in parms:
            if param in item:  # Ensure the key exists in the item
                text += f"{param}: {item[param]}\n"
            else:
                text += f"{param}: [値が見つかりません]\n"
        final_context += text + "\n"  # Append each item's data to final_context

    # Save the final_context to a file
    file_name = outname
    with open(file_name, "w", encoding="utf-8") as file:
        file.write(final_context)

    # Server URL for upload
    server_url = "https://www.ryhintl.com/company_matters.php"

    # Upload the final_context to the server
    status, response_text = upload_text_to_server(final_context, server_url, outname)

    data = json.loads(response_text)

    # メッセージを取得
    message = data.get("message", "")

    print(f"アップロードのステータス: {status}")
    print(f"レスポンス: {message}")
    
    #return response_text
    return message


# Gradioの関数
def search(outname,params,tablename):
    result = json_to_text(outname,params,tablename)
    return result

# Gradioインターフェース
interface = gr.Interface(
    css="footer {visibility: hidden;}",
    theme=gr.themes.Glass(),
    fn=search,
    inputs=[gr.Textbox(label="出力ファイル名",value="cm_output.html"), gr.Textbox(label="パラメーター",value="['caption','title','content','date']"), gr.Textbox(label="テーブル名",value="company_matters")],
    outputs=gr.Textbox(label="結果"),
    title="DB HTML",
    description="MySQLのテーブルからデータを取得し、Agent用のHTMLを生成します。",
    submit_btn="実行",
    stop_btn="中止",
    clear_btn="クリア",
    flagging_mode="never"
)

# アプリの実行
if __name__ == "__main__":
    interface.launch()