mindus-tk
commited on
Commit
·
402dbe6
1
Parent(s):
5cedfb3
fix
Browse files
app.py
CHANGED
@@ -1,30 +1,20 @@
|
|
1 |
-
|
2 |
from flask import Flask, request, abort
|
3 |
from linebot import LineBotApi, WebhookHandler
|
4 |
from linebot.exceptions import InvalidSignatureError
|
5 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
6 |
-
import
|
7 |
|
8 |
-
|
9 |
-
# Flask アプリケーションの初期化
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
-
# LINE Bot
|
13 |
-
LINE_CHANNEL_ACCESS_TOKEN = '
|
14 |
-
LINE_CHANNEL_SECRET = '
|
15 |
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
|
16 |
handler = WebhookHandler(LINE_CHANNEL_SECRET)
|
17 |
|
18 |
-
# Hugging Face
|
19 |
-
|
20 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
21 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
22 |
-
|
23 |
-
def generate_response(input_text):
|
24 |
-
# ユーザーのメッセージをモデルに入力し、応答を生成
|
25 |
-
inputs = tokenizer.encode(input_text, return_tensors='pt')
|
26 |
-
outputs = model.generate(inputs, max_length=50, num_return_sequences=1)
|
27 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
|
29 |
@app.route("/callback", methods=['POST'])
|
30 |
def callback():
|
@@ -40,15 +30,15 @@ def callback():
|
|
40 |
|
41 |
@handler.add(MessageEvent, message=TextMessage)
|
42 |
def handle_message(event):
|
43 |
-
# LINEからのメッセージを受け取り、モデルで応答を生成
|
44 |
input_text = event.message.text
|
45 |
-
response_message = generate_response(input_text)
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
52 |
|
53 |
if __name__ == "__main__":
|
54 |
app.run()
|
|
|
1 |
+
# Heroku側のコード
|
2 |
from flask import Flask, request, abort
|
3 |
from linebot import LineBotApi, WebhookHandler
|
4 |
from linebot.exceptions import InvalidSignatureError
|
5 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
6 |
+
import requests
|
7 |
|
|
|
|
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
+
# LINE Botの設定
|
11 |
+
LINE_CHANNEL_ACCESS_TOKEN = 'YOUR_CHANNEL_ACCESS_TOKEN'
|
12 |
+
LINE_CHANNEL_SECRET = 'YOUR_CHANNEL_SECRET'
|
13 |
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
|
14 |
handler = WebhookHandler(LINE_CHANNEL_SECRET)
|
15 |
|
16 |
+
# Hugging Face Spaces APIのエンドポイントURL(自分で設定する必要があります)
|
17 |
+
HF_SPACES_API_URL = "https://your-huggingface-spaces-url"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
@app.route("/callback", methods=['POST'])
|
20 |
def callback():
|
|
|
30 |
|
31 |
@handler.add(MessageEvent, message=TextMessage)
|
32 |
def handle_message(event):
|
|
|
33 |
input_text = event.message.text
|
|
|
34 |
|
35 |
+
# Hugging Face SpacesのAPIにメッセージを転送
|
36 |
+
response = requests.post(HF_SPACES_API_URL, json={"input": input_text})
|
37 |
+
if response.status_code == 200:
|
38 |
+
reply_message = response.json()['output']
|
39 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=reply_message))
|
40 |
+
else:
|
41 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="エラーが発生しました"))
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
app.run()
|