Spaces:
Paused
Paused
from hugchat import hugchat | |
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
import uvicorn | |
import os | |
import requests | |
# 从环境变量中读取 cookies | |
cookies_str = os.getenv("COOKIES_STR") | |
if not cookies_str: | |
raise Exception("COOKIES_STR environment variable is not set") | |
# 打印调试信息 | |
print(f"COOKIES_STR: {cookies_str}") | |
def parse_cookies(cookies_str): | |
cookies = {} | |
for cookie in cookies_str.split('; '): | |
name, value = cookie.split('=', 1) | |
cookies[name] = value | |
return cookies | |
try: | |
cookies = parse_cookies(cookies_str) | |
print("Successfully parsed COOKIES_STR") | |
except Exception as e: | |
raise Exception(f"Failed to parse COOKIES_STR: {e}") | |
try: | |
print("Attempting to initialize ChatBot with provided cookies...") | |
chatbot = hugchat.ChatBot(cookies=cookies) | |
print("ChatBot initialized successfully!") | |
except Exception as e: | |
print(f"Failed to initialize ChatBot: {e}") | |
raise | |
class RequestBody(BaseModel): | |
prompt: str | |
max_tokens: int | |
class Choice(BaseModel): | |
text: str | |
class CompletionResponse(BaseModel): | |
choices: list[Choice] | |
app = FastAPI() | |
async def completions(body: RequestBody): | |
try: | |
response = chatbot.chat(body.prompt, max_length=body.max_tokens).wait_until_done() | |
return CompletionResponse(choices=[Choice(text=response)]) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
if __name__ == "__main__": | |
uvicorn.run(app, host="0.0.0.0", port=8000) | |