Spaces:
Running
Running
File size: 1,989 Bytes
55b1be9 34287b9 aa5eb5d 55b1be9 2cf7795 76eaf5a 4db0db1 3fe66d7 55b1be9 df5bf2a 55b1be9 7a8d7b0 9a45992 0385c62 2cf7795 55b1be9 6482098 55b1be9 2cf7795 6482098 34287b9 0a52d39 3a2e91a 34287b9 3a2e91a 34287b9 0385c62 2cf7795 4a7f8d2 3a2e91a 4a7f8d2 2cf7795 4a7f8d2 3a2e91a 4a7f8d2 5e2a1d8 4a7f8d2 619c607 3bf6983 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
from gradio_client import Client
import json
import os
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
)
client = Client("alanchen1115/Gemini")
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
@app.get("/")
def root():
return {"title": "Echo Bot"}
@app.post("/webhook")
async def webhook(
request: Request,
background_tasks: BackgroundTasks,
x_line_signature=Header(None),
):
body = await request.body()
try:
background_tasks.add_task(
line_handler.handle, body.decode("utf-8"), x_line_signature
)
except InvalidSignatureError:
raise HTTPException(status_code=400, detail="Invalid signature")
return "ok"
@line_handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
global working_status
if event.message.text == "再見":
working_status = True
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text="Bye!"))
return
if working_status:
out = client.predict(event.message.text, [[event.message.text, ]], api_name='/user')
if not out:
out = "不懂您的意思!"
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=out))
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True) |