mindus-tk commited on
Commit
8888132
·
1 Parent(s): c4ab332
Files changed (2) hide show
  1. app.py +53 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
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 torch
7
+
8
+ # Flask アプリケーションの初期化
9
+ app = Flask(__name__)
10
+
11
+ # LINE Botの設定(これらの値はLINE Developer Portalから取得し、適切に置き換えてください)
12
+ LINE_CHANNEL_ACCESS_TOKEN = 'YOUR_CHANNEL_ACCESS_TOKEN'
13
+ LINE_CHANNEL_SECRET = 'YOUR_CHANNEL_SECRET'
14
+ line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
15
+ handler = WebhookHandler(LINE_CHANNEL_SECRET)
16
+
17
+ # Hugging Faceモデルのロード
18
+ model_name = "youri-7b-chat"
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
20
+ model = AutoModelForCausalLM.from_pretrained(model_name)
21
+
22
+ def generate_response(input_text):
23
+ # ユーザーのメッセージをモデルに入力し、応答を生成
24
+ inputs = tokenizer.encode(input_text, return_tensors='pt')
25
+ outputs = model.generate(inputs, max_length=50, num_return_sequences=1)
26
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
27
+
28
+ @app.route("/callback", methods=['POST'])
29
+ def callback():
30
+ signature = request.headers['X-Line-Signature']
31
+ body = request.get_data(as_text=True)
32
+
33
+ try:
34
+ handler.handle(body, signature)
35
+ except InvalidSignatureError:
36
+ abort(400)
37
+
38
+ return 'OK'
39
+
40
+ @handler.add(MessageEvent, message=TextMessage)
41
+ def handle_message(event):
42
+ # LINEからのメッセージを受け取り、モデルで応答を生成
43
+ input_text = event.message.text
44
+ response_message = generate_response(input_text)
45
+
46
+ # LINEユーザーに応答を返信
47
+ line_bot_api.reply_message(
48
+ event.reply_token,
49
+ TextSendMessage(text=response_message)
50
+ )
51
+
52
+ if __name__ == "__main__":
53
+ app.run()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ flask==3.0.0
2
+ line-bot-sdk==3.7.0
3
+ torch==2.1.2
4
+ pip