alanchen1115 commited on
Commit
2cf7795
Β·
verified Β·
1 Parent(s): c6abe61

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +29 -18
main.py CHANGED
@@ -1,4 +1,9 @@
1
- from fastapi import FastAPI, Request, BackgroundTasks, Header
 
 
 
 
 
2
  from linebot import (
3
  LineBotApi, WebhookHandler
4
  )
@@ -8,23 +13,24 @@ from linebot.exceptions import (
8
  from linebot.models import (
9
  MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
10
  )
11
- from starlette.exceptions import HTTPException
12
- import os
13
 
14
- app = FastAPI()
15
- # line_bot_api = LineBotApi(os.environ["LINE_CHANNEL_ACCESS_TOKEN"])
16
- # line_handler = WebhookHandler(os.environ["LINE_CHANNEL_SECRET"])
17
  line_bot_api = LineBotApi("ZwnEWwK46AUmRDOFXJOwUh1TqAELahP+kxHY0iBevF8JiEUe0CBQpkW2Gpo3C38LOaIX7hpW4IuTOSOjVof+DX9XgZg8MTeh5Iz+NDLlw6KKMtqNsyR6zt9CMnsso21ieifC/S8dcgXx3iKlhdS7UAdB04t89/1O/w1cDnyilFU=")
18
  line_handler = WebhookHandler("7b9984bbc5c62445a79b95b15e34b727")
19
 
20
- @app.get("/")
21
- def root():
22
- return {"title": "Echo Bot"}
23
-
24
- @app.post("/webhook")
25
- async def webhook(
26
- request: Request,
27
- ):
 
 
 
 
 
 
28
  # get X-Line-Signature header value
29
  signature = request.headers['X-Line-Signature']
30
  # get request body as text
@@ -36,10 +42,11 @@ async def webhook(
36
  except InvalidSignatureError:
37
  abort(400)
38
  return 'OK'
39
-
40
  @line_handler.add(MessageEvent, message=TextMessage)
41
  def handle_message(event):
42
  global working_status
 
43
  if event.message.text == "再見":
44
  working_status = True
45
  line_bot_api.reply_message(
@@ -48,10 +55,14 @@ def handle_message(event):
48
  return
49
 
50
  if working_status:
51
-
52
- out = event.message.text
 
53
  if not out:
54
  out = "δΈζ‡‚ζ‚¨ηš„ζ„ζ€!"
55
  line_bot_api.reply_message(
56
  event.reply_token,
57
- TextSendMessage(text=out))
 
 
 
 
1
+ from flask import Flask, request, abort
2
+ from flask_ngrok import run_with_ngrok # colab
3
+ from llama_cpp import Llama
4
+ import json
5
+ import os
6
+
7
  from linebot import (
8
  LineBotApi, WebhookHandler
9
  )
 
13
  from linebot.models import (
14
  MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
15
  )
 
 
16
 
 
 
 
17
  line_bot_api = LineBotApi("ZwnEWwK46AUmRDOFXJOwUh1TqAELahP+kxHY0iBevF8JiEUe0CBQpkW2Gpo3C38LOaIX7hpW4IuTOSOjVof+DX9XgZg8MTeh5Iz+NDLlw6KKMtqNsyR6zt9CMnsso21ieifC/S8dcgXx3iKlhdS7UAdB04t89/1O/w1cDnyilFU=")
18
  line_handler = WebhookHandler("7b9984bbc5c62445a79b95b15e34b727")
19
 
20
+ working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
21
+
22
+ app = Flask(__name__)
23
+
24
+ llm = Llama(model_path="Breeze-7B-Instruct-v0.1-Q8_0.gguf", n_gpu_layers=32)
25
+ line_handler = WebhookHandler(YOUR_CHANNEL_SECRET)
26
+ line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
27
+
28
+ @app.route('/')
29
+ def main():
30
+ return '您ε₯½'
31
+
32
+ @app.route("/webhook", methods=['POST'])
33
+ def result():
34
  # get X-Line-Signature header value
35
  signature = request.headers['X-Line-Signature']
36
  # get request body as text
 
42
  except InvalidSignatureError:
43
  abort(400)
44
  return 'OK'
45
+
46
  @line_handler.add(MessageEvent, message=TextMessage)
47
  def handle_message(event):
48
  global working_status
49
+
50
  if event.message.text == "再見":
51
  working_status = True
52
  line_bot_api.reply_message(
 
55
  return
56
 
57
  if working_status:
58
+ QA = f"HUMAN: {event.message.text}? ASSISTANT: "
59
+ out = llm(QA, max_tokens=512, temperature = 0.5, echo=True)
60
+ out = out['choices'][0]['text'].split('ASSISTANT:')[1]
61
  if not out:
62
  out = "δΈζ‡‚ζ‚¨ηš„ζ„ζ€!"
63
  line_bot_api.reply_message(
64
  event.reply_token,
65
+ TextSendMessage(text=out))
66
+
67
+ if __name__ == '__main__':
68
+ app.run()