mindus-tk commited on
Commit
6721178
1 Parent(s): 402dbe6
Files changed (1) hide show
  1. app.py +16 -35
app.py CHANGED
@@ -1,44 +1,25 @@
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():
21
- signature = request.headers['X-Line-Signature']
22
- body = request.get_data(as_text=True)
23
 
24
- try:
25
- handler.handle(body, signature)
26
- except InvalidSignatureError:
27
- abort(400)
28
-
29
- return 'OK'
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()
 
1
+ # Hugging FaceのSpaces側のコード
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from flask import Flask, request, jsonify
4
+ import torch
 
 
5
 
6
  app = Flask(__name__)
7
 
8
+ model_name = "youri-7b-chat"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
11
 
12
+ @app.route("/api", methods=['POST'])
13
+ def chat():
14
+ input_data = request.json
15
+ input_text = input_data['input']
16
 
17
+ # 推論処理
18
+ inputs = tokenizer.encode(input_text, return_tensors='pt')
19
+ outputs = model.generate(inputs, max_length=50, num_return_sequences=1)
20
+ response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
 
22
+ return jsonify({"output": response_text})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  if __name__ == "__main__":
25
  app.run()