Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from hugchat import hugchat
|
2 |
+
from hugchat.login import Login
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
import uvicorn
|
6 |
+
|
7 |
+
# 登录 Hugging Face 并授权 huggingchat
|
8 |
+
EMAIL = "your email"
|
9 |
+
PASSWD = "your password"
|
10 |
+
cookie_path_dir = "./cookies/"
|
11 |
+
sign = Login(EMAIL, PASSWD)
|
12 |
+
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
|
13 |
+
|
14 |
+
# 创建 ChatBot
|
15 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
16 |
+
|
17 |
+
class RequestBody(BaseModel):
|
18 |
+
prompt: str
|
19 |
+
max_tokens: int
|
20 |
+
|
21 |
+
class Choice(BaseModel):
|
22 |
+
text: str
|
23 |
+
|
24 |
+
class CompletionResponse(BaseModel):
|
25 |
+
choices: list[Choice]
|
26 |
+
|
27 |
+
app = FastAPI()
|
28 |
+
|
29 |
+
@app.post("/v1/chat/completions", response_model=CompletionResponse)
|
30 |
+
async def completions(body: RequestBody):
|
31 |
+
try:
|
32 |
+
response = chatbot.chat(body.prompt, max_length=body.max_tokens).wait_until_done()
|
33 |
+
return CompletionResponse(choices=[Choice(text=response)])
|
34 |
+
except Exception as e:
|
35 |
+
raise HTTPException(status_code=500, detail=str(e))
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|