mindus-tk
commited on
Commit
•
6721178
1
Parent(s):
402dbe6
fix
Browse files
app.py
CHANGED
@@ -1,44 +1,25 @@
|
|
1 |
-
#
|
2 |
-
from
|
3 |
-
from
|
4 |
-
|
5 |
-
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
6 |
-
import requests
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
|
14 |
-
handler = WebhookHandler(LINE_CHANNEL_SECRET)
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
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()
|