-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.py
64 lines (51 loc) · 2.26 KB
/
server.py
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
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
import json
from fastapi import FastAPI, Request, status
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from starlette.responses import RedirectResponse
import uvicorn
from uvicorn import Config, Server
from __init__ import ChatGPT
host_address = 'UNSPECIFIED'
limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/alisa-request")
@limiter.limit("50/minute")
async def handle_alisa_request(response: str, request: Request):
data = await request.json()
question = str(data["request"]["original_utterance"])
if question:
chat = ChatGPT.Chat(email="EMAIL", password="PASS")
answer = chat.ask(question)
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"session": data["session"],
"version": data["version"],
"response": {"end_session": False,
"text": answer} })
else:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"session": data["session"],
"version": data["version"],
"response": {"end_session": False,
"text": "Привет, я GPT-bot, который работает на основе нейронной сети от OpenAI. Я могу фантазировать, делать технические расчеты - быть твоим умным собеседником и другом. Можешь ознакомиться с кодом в телеграм канале: @keystoner"} })
if __name__ == '__main__':
uvicorn.run("server:app",
port=80,
reload=True)