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()