Spaces:
Paused
Paused
Upload main (1).py
Browse files- main (1).py +68 -0
main (1).py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import Dict, Any, Generator
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from dsk.api import DeepSeekAPI, AuthenticationError, RateLimitError, NetworkError, APIError
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Initialize the API with your auth token (store in environment variable for security)
|
10 |
+
API_KEY = os.getenv("DEEPSEEK_API_KEY", "your-api-key")
|
11 |
+
api = DeepSeekAPI(API_KEY)
|
12 |
+
|
13 |
+
|
14 |
+
class ChatRequest(BaseModel):
|
15 |
+
prompt: str
|
16 |
+
thinking_enabled: bool = True
|
17 |
+
search_enabled: bool = False
|
18 |
+
|
19 |
+
|
20 |
+
def process_response(chunks: Generator[Dict[str, Any], None, None]) -> Dict[str, Any]:
|
21 |
+
"""Helper function to process response chunks"""
|
22 |
+
thinking_lines = []
|
23 |
+
text_content = []
|
24 |
+
|
25 |
+
for chunk in chunks:
|
26 |
+
if chunk['type'] == 'thinking':
|
27 |
+
if chunk['content'] and chunk['content'] not in thinking_lines:
|
28 |
+
thinking_lines.append(chunk['content'])
|
29 |
+
elif chunk['type'] == 'text':
|
30 |
+
text_content.append(chunk['content'])
|
31 |
+
|
32 |
+
return {
|
33 |
+
"thinking": thinking_lines,
|
34 |
+
"response": ''.join(text_content)
|
35 |
+
}
|
36 |
+
|
37 |
+
|
38 |
+
@app.post("/chat_completion")
|
39 |
+
def chat_completion(request: ChatRequest):
|
40 |
+
try:
|
41 |
+
session = api.create_chat_session()
|
42 |
+
chunks = api.chat_completion(
|
43 |
+
session,
|
44 |
+
request.prompt,
|
45 |
+
thinking_enabled=request.thinking_enabled,
|
46 |
+
search_enabled=request.search_enabled
|
47 |
+
)
|
48 |
+
return process_response(chunks)
|
49 |
+
except AuthenticationError:
|
50 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
51 |
+
except RateLimitError:
|
52 |
+
raise HTTPException(status_code=429, detail="Rate limit exceeded")
|
53 |
+
except NetworkError:
|
54 |
+
raise HTTPException(status_code=503, detail="Network error. Try again later")
|
55 |
+
except APIError as e:
|
56 |
+
raise HTTPException(status_code=e.status_code or 500, detail="API error occurred")
|
57 |
+
except Exception as e:
|
58 |
+
raise HTTPException(status_code=500, detail=str(e))
|
59 |
+
|
60 |
+
|
61 |
+
@app.get("/")
|
62 |
+
@app.get("/status")
|
63 |
+
def status():
|
64 |
+
return {"status": "ok", "message": "FastAPI DeepSeekAPI is running"}
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
import uvicorn
|
68 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)
|