Spaces:
Sleeping
Sleeping
Commit
·
7305727
1
Parent(s):
66444d9
again
Browse files
main.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, abort
|
2 |
+
|
3 |
+
import markdown
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
|
6 |
+
from linebot.v3 import (
|
7 |
+
WebhookHandler
|
8 |
+
)
|
9 |
+
from linebot.v3.exceptions import (
|
10 |
+
InvalidSignatureError
|
11 |
+
)
|
12 |
+
from linebot.v3.messaging import (
|
13 |
+
Configuration,
|
14 |
+
ApiClient,
|
15 |
+
MessagingApi,
|
16 |
+
ReplyMessageRequest,
|
17 |
+
TextMessage
|
18 |
+
)
|
19 |
+
from linebot.v3.webhooks import (
|
20 |
+
MessageEvent,
|
21 |
+
TextMessageContent
|
22 |
+
)
|
23 |
+
|
24 |
+
import os
|
25 |
+
import requests
|
26 |
+
|
27 |
+
HF_TOKEN = os.environ.get('HF_TOKEN')
|
28 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
29 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
|
30 |
+
def query(payload):
|
31 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
32 |
+
return response.json()
|
33 |
+
|
34 |
+
app = Flask(__name__)
|
35 |
+
|
36 |
+
configuration = Configuration(access_token='YOUR_CHANNEL_ACCESS_TOKEN')
|
37 |
+
handler = WebhookHandler('YOUR_CHANNEL_SECRET')
|
38 |
+
|
39 |
+
|
40 |
+
@app.route("/callback", methods=['POST'])
|
41 |
+
def callback():
|
42 |
+
# get X-Line-Signature header value
|
43 |
+
signature = request.headers['X-Line-Signature']
|
44 |
+
|
45 |
+
# get request body as text
|
46 |
+
body = request.get_data(as_text=True)
|
47 |
+
app.logger.info("Request body: " + body)
|
48 |
+
|
49 |
+
# handle webhook body
|
50 |
+
try:
|
51 |
+
handler.handle(body, signature)
|
52 |
+
except InvalidSignatureError:
|
53 |
+
app.logger.info("Invalid signature. Please check your channel access token/channel secret.")
|
54 |
+
abort(400)
|
55 |
+
|
56 |
+
return 'OK'
|
57 |
+
|
58 |
+
@handler.add(MessageEvent, message=TextMessageContent)
|
59 |
+
def handle_message(event):
|
60 |
+
with ApiClient(configuration) as api_client:
|
61 |
+
line_bot_api = MessagingApi(api_client)
|
62 |
+
response = query({"inputs": input, "parameters":{"return_full_text":False, "max_length":1024}})
|
63 |
+
html_msg = markdown.markdown(response)
|
64 |
+
soup = BeautifulSoup(html_msg, 'html.parser')
|
65 |
+
line_bot_api.reply_message(
|
66 |
+
ReplyMessageRequest(
|
67 |
+
reply_token=event.reply_token,
|
68 |
+
messages=[TextMessage(text=soup.get_text())]
|
69 |
+
)
|
70 |
+
)
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
app.run()
|