fudii0921 commited on
Commit
8e21496
·
verified ·
1 Parent(s): 5c56f50

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+ def upload_text_to_server(text, url, fname):
6
+ try:
7
+ # テキストを送信
8
+ payload = {"content": text, "filename": fname}
9
+ response = requests.post(url, data=payload)
10
+
11
+ # HTTPステータスコードを確認
12
+ response.raise_for_status() # エラーがあれば例外を発生させる
13
+ return response.status_code, response.text
14
+
15
+ except requests.exceptions.RequestException as e:
16
+ # エラー処理
17
+ print(f"Error occurred during the request: {e}")
18
+ return None, str(e)
19
+
20
+ # 自然言語にする関数
21
+ def json_to_text(outname, param_array, tablename):
22
+ parms = eval(param_array)
23
+ # Fetch JSON data from the API
24
+ dataid = requests.get("https://www.ryhintl.com/dbjson/getjson?sqlcmd=select * from "+tablename)
25
+
26
+ # Decode the JSON response
27
+ data_str = dataid.content.decode('utf-8')
28
+ data = json.loads(data_str)
29
+
30
+ final_context = ""
31
+ for item in data:
32
+ text = ""
33
+ # Dynamically fetch values based on the parameter array
34
+ for param in parms:
35
+ if param in item: # Ensure the key exists in the item
36
+ text += f"{param}: {item[param]}\n"
37
+ else:
38
+ text += f"{param}: [値が見つかりません]\n"
39
+ final_context += text + "\n" # Append each item's data to final_context
40
+
41
+ # Save the final_context to a file
42
+ file_name = outname
43
+ with open(file_name, "w", encoding="utf-8") as file:
44
+ file.write(final_context)
45
+
46
+ # Server URL for upload
47
+ server_url = "https://www.ryhintl.com/company_matters.php"
48
+
49
+ # Upload the final_context to the server
50
+ status, response_text = upload_text_to_server(final_context, server_url, outname)
51
+ print(f"アップロードのステータス: {status}")
52
+ print(f"レスポンス: {response_text}")
53
+
54
+ return response_text
55
+
56
+
57
+ # Gradioの関数
58
+ def search(outname,params,tablename):
59
+ result = json_to_text(outname,params,tablename)
60
+ return result
61
+
62
+ # Gradioインターフェース
63
+ interface = gr.Interface(
64
+ fn=search,
65
+ inputs=[gr.Textbox(label="出力ファイル名",value="cm_output.html"), gr.Textbox(label="パラメーター",value="['caption','title','content','date']"), gr.Textbox(label="テーブル名",value="company_matters")],
66
+ outputs=gr.Textbox(label="結果"),
67
+ title="DB HTML"
68
+ )
69
+
70
+ # アプリの実行
71
+ if __name__ == "__main__":
72
+ interface.launch()