yuripeyamashita commited on
Commit
6a17841
1 Parent(s): ca51d48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -56
app.py CHANGED
@@ -1,67 +1,63 @@
 
1
  from flask import Flask, request
2
- from gradio_client import Client
3
- from uuid import uuid4
4
- import os, json, requests, hashlib
5
 
6
  app = Flask(__name__)
7
 
8
- secret = os.environ.get("auth")
9
- headers = {'Authorization': os.environ.get("auth")}
10
-
11
- res_dict = {}
12
-
13
  @app.route('/', methods=['GET'])
14
  def index():
15
  return {}, 200
16
 
17
- @app.route('/queue', methods=['POST'])
18
- def queue():
19
- if request.json.get('secret') != secret:
20
- return {}, 400
21
-
22
- uuid = str(uuid4())
23
- res_dict[uuid] = request.json
24
-
25
- return { 'uuid': uuid }, 200
26
-
27
-
28
- @app.route('/6asr', methods=['POST'])
29
- def sixasr():
30
- data = res_dict.get(request.json.get('uuid'))
31
- if not data:
32
- return {}, 400
33
-
34
- base64 = request.json.get('base64')
35
- dialectCode = request.json.get('dialectCode')
36
- if not base64 or not dialectCode:
37
- return {}, 400
38
-
39
- if hashlib.sha256(base64.encode('utf-8')).hexdigest() != data.get("base64Hash"):
40
- return {}, 400
41
-
42
- URL = 'https://nuu-lab-gradio-6asr-whisper.hf.space/run/predict'
43
- body = {'data': ["whisper-large-v3", f"htia_{dialectCode}", None, base64, '']}
44
-
45
- res = requests.post(URL, json=body, headers=headers)
46
- if res.status_code == 200:
47
- res_json = res.json()
48
-
49
- callbackURL = data.get('callbackURL')
50
- callback_body = data.get('callbackBody')
51
-
52
- if res_json.get('data'):
53
- callback_body['length'] = len(res_json['data'][0])
54
- if res_json.get('srt'):
55
- callback_body['srt'] = res_json['srt'][0]
56
-
57
- requests.post(callbackURL, json=callback_body)
58
-
59
- print(callback_body)
60
- print(res_json)
61
-
62
- return res_json
63
-
64
- return {'error': '請求失敗'}, 500
 
 
 
65
 
66
 
67
  if __name__ == "__main__":
 
1
+ import os, json, requests
2
  from flask import Flask, request
 
 
 
3
 
4
  app = Flask(__name__)
5
 
 
 
 
 
 
6
  @app.route('/', methods=['GET'])
7
  def index():
8
  return {}, 200
9
 
10
+ LINE_REPLY_URL = "https://api.line.me/v2/bot/message/reply"
11
+ LINE_CHANNEL_ACCESS_TOKEN = os.environ.get("LINE_CHANNEL_ACCESS_TOKEN")
12
+
13
+ LINE_REPLY_HEADERS = {
14
+ "Content-Type": "application/json; charset=UTF-8",
15
+ "Authorization": "Bearer " + LINE_CHANNEL_ACCESS_TOKEN
16
+ }
17
+
18
+ @app.route("/api/", methods=["POST"])
19
+ def api():
20
+ try:
21
+ payload = get_payload_dict(request.get_json())
22
+ print(payload)
23
+ # res_text = opai.private_translate_api(payload.get("msg_text"))
24
+ # send_text(payload.get("token"), res_text)
25
+ except:
26
+ pass
27
+ return "", 200
28
+
29
+
30
+ def get_payload_dict(raw_payload):
31
+ print(raw_payload)
32
+ token = raw_payload.get("events", [{}])[0].get("replyToken")
33
+ sender_type = raw_payload.get("events", [{}])[0].get("source", {}).get("type", "user")
34
+ sender_id = raw_payload.get("events", [{}])[0].get("source", {}).get(str(sender_type+"Id"))
35
+ msg_type = raw_payload.get("events", [{}])[0].get("message", {}).get("type")
36
+ msg_id = raw_payload.get("events", [{}])[0].get("message", {}).get("id")
37
+ msg_text = raw_payload.get("events", [{}])[0].get("message", {}).get("text")
38
+ postback_data = raw_payload.get("events", [{}])[0].get("postback", {}).get("data")
39
+
40
+ payload = {"token": token,
41
+ "sender_type": sender_type,
42
+ "sender_id": sender_id,
43
+ "msg_type": msg_type,
44
+ "msg_id": msg_id,
45
+ "msg_text": msg_text,
46
+ "postback_data": postback_data
47
+ }
48
+ return payload
49
+
50
+
51
+ def send_text(token, text):
52
+ res = requests.post(LINE_REPLY_URL, headers=LINE_REPLY_HEADERS, json={
53
+ "replyToken": token,
54
+ "messages": [
55
+ {
56
+ "type": "text",
57
+ "text": text
58
+ }
59
+ ]
60
+ })
61
 
62
 
63
  if __name__ == "__main__":