nitrox commited on
Commit
cf7a28a
·
verified ·
1 Parent(s): 2a69ed4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -26
app.py CHANGED
@@ -1,6 +1,5 @@
1
- from fastapi import FastAPI, HTTPException, Response
2
  from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.middleware.base import BaseHTTPMiddleware
4
  from fastapi.responses import JSONResponse
5
  import os
6
  from dotenv import load_dotenv
@@ -14,15 +13,6 @@ load_dotenv()
14
 
15
  app = FastAPI()
16
 
17
- class ContentTypeMiddleware(BaseHTTPMiddleware):
18
- async def dispatch(self, request, call_next):
19
- response = await call_next(request)
20
- if isinstance(response, JSONResponse):
21
- response.headers["Content-Type"] = "application/json; charset=utf-8"
22
- return response
23
-
24
- app.add_middleware(ContentTypeMiddleware)
25
-
26
  app.add_middleware(
27
  CORSMiddleware,
28
  allow_origins=["*"],
@@ -54,21 +44,11 @@ def clean_assistant_response(text: str) -> str:
54
  text = text[:-3].strip()
55
  return text
56
 
57
- class CustomJSONResponse(JSONResponse):
58
- media_type = "application/json; charset=utf-8"
59
-
60
- def render(self, content: Any) -> bytes:
61
- return json.dumps(
62
- content,
63
- ensure_ascii=False,
64
- allow_nan=False,
65
- indent=None,
66
- separators=(',', ':')
67
- ).encode('utf-8')
68
-
69
  @app.get("/")
70
  async def root():
71
- return CustomJSONResponse({"status": "FastFlowWrapper is running"})
 
 
72
 
73
  @app.get("/v1/models")
74
  async def get_models():
@@ -92,7 +72,9 @@ async def get_models():
92
  "system_fingerprint": "phi4-r1"
93
  })
94
 
95
- return CustomJSONResponse({"object": "list", "data": models})
 
 
96
  except requests.RequestException as e:
97
  raise HTTPException(status_code=500, detail=str(e))
98
 
@@ -130,7 +112,7 @@ async def create_chat_completion(request: ChatCompletionRequest):
130
  # Подсчитываем токены ответа
131
  completion_tokens = count_tokens(assistant_response)
132
 
133
- return CustomJSONResponse({
134
  "id": "chatcmpl-" + os.urandom(12).hex(),
135
  "object": "chat.completion",
136
  "created": int(start_time),
@@ -154,5 +136,7 @@ async def create_chat_completion(request: ChatCompletionRequest):
154
  "stats": {},
155
  "system_fingerprint": "phi4-r1"
156
  })
 
 
157
  except requests.RequestException as e:
158
  raise HTTPException(status_code=500, detail=str(e))
 
1
+ from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
 
3
  from fastapi.responses import JSONResponse
4
  import os
5
  from dotenv import load_dotenv
 
13
 
14
  app = FastAPI()
15
 
 
 
 
 
 
 
 
 
 
16
  app.add_middleware(
17
  CORSMiddleware,
18
  allow_origins=["*"],
 
44
  text = text[:-3].strip()
45
  return text
46
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  @app.get("/")
48
  async def root():
49
+ response = JSONResponse({"status": "FastFlowWrapper is running"})
50
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
51
+ return response
52
 
53
  @app.get("/v1/models")
54
  async def get_models():
 
72
  "system_fingerprint": "phi4-r1"
73
  })
74
 
75
+ response = JSONResponse({"object": "list", "data": models})
76
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
77
+ return response
78
  except requests.RequestException as e:
79
  raise HTTPException(status_code=500, detail=str(e))
80
 
 
112
  # Подсчитываем токены ответа
113
  completion_tokens = count_tokens(assistant_response)
114
 
115
+ response = JSONResponse({
116
  "id": "chatcmpl-" + os.urandom(12).hex(),
117
  "object": "chat.completion",
118
  "created": int(start_time),
 
136
  "stats": {},
137
  "system_fingerprint": "phi4-r1"
138
  })
139
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
140
+ return response
141
  except requests.RequestException as e:
142
  raise HTTPException(status_code=500, detail=str(e))