Add application file
Browse files- Dockerfile +14 -0
- main.py +72 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["gunicorn","-b", "0.0.0.0:7860", "main:app"]
|
main.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, abort
|
2 |
+
import os
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
from linebot.v3 import (
|
6 |
+
WebhookHandler
|
7 |
+
)
|
8 |
+
|
9 |
+
from linebot.v3.exceptions import (
|
10 |
+
InvalidSignatureError
|
11 |
+
)
|
12 |
+
|
13 |
+
from linebot.v3.messaging import (
|
14 |
+
Configuration,
|
15 |
+
ApiClient,
|
16 |
+
MessagingApi,
|
17 |
+
ReplyMessageRequest,
|
18 |
+
TextMessage
|
19 |
+
)
|
20 |
+
|
21 |
+
from linebot.v3.webhooks import (
|
22 |
+
MessageEvent,
|
23 |
+
TextMessageContent
|
24 |
+
)
|
25 |
+
|
26 |
+
app = Flask(__name__)
|
27 |
+
|
28 |
+
configuration = Configuration(access_token=os.environ.get("ACCESS_TOKEN"))
|
29 |
+
handler = WebhookHandler(os.environ.get("CHANNEL_SECRET"))
|
30 |
+
|
31 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
32 |
+
model = genai.GenerativeModel('gemini-pro')
|
33 |
+
|
34 |
+
|
35 |
+
@app.route("/")
|
36 |
+
def home():
|
37 |
+
return {"message": "Line Webhook Server"}
|
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 |
+
|
59 |
+
@handler.add(MessageEvent, message=TextMessageContent)
|
60 |
+
def handle_message(event):
|
61 |
+
response = model.generate_content(event.message.text)
|
62 |
+
print(f"UserID: {event.source.userId}")
|
63 |
+
print(f"Q: {event.message.text}")
|
64 |
+
print(f"A: {response.text}")
|
65 |
+
with ApiClient(configuration) as api_client:
|
66 |
+
line_bot_api = MessagingApi(api_client)
|
67 |
+
line_bot_api.reply_message_with_http_info(
|
68 |
+
ReplyMessageRequest(
|
69 |
+
reply_token=event.reply_token,
|
70 |
+
messages=[TextMessage(text=response.text)]
|
71 |
+
)
|
72 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask
|
2 |
+
google-generativeai
|
3 |
+
gunicorn
|
4 |
+
line-bot-sdk
|