Update main.py
Browse files
main.py
CHANGED
@@ -14,9 +14,14 @@ from pydantic import BaseModel
|
|
14 |
from starlette.middleware.cors import CORSMiddleware
|
15 |
from starlette.responses import StreamingResponse, Response
|
16 |
|
|
|
|
|
|
|
|
|
17 |
# Configure logging
|
18 |
logging.basicConfig(
|
19 |
-
level=logging.
|
|
|
20 |
)
|
21 |
logger = logging.getLogger(__name__)
|
22 |
|
@@ -67,7 +72,10 @@ app.add_middleware(
|
|
67 |
# Security configuration
|
68 |
security = HTTPBearer()
|
69 |
|
70 |
-
#
|
|
|
|
|
|
|
71 |
class Message(BaseModel):
|
72 |
role: str
|
73 |
content: str
|
@@ -77,8 +85,12 @@ class ChatRequest(BaseModel):
|
|
77 |
messages: List[Message]
|
78 |
stream: Optional[bool] = False
|
79 |
|
80 |
-
#
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
return {
|
83 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
84 |
"object": "chat.completion.chunk",
|
@@ -94,7 +106,8 @@ def simulate_data(content, model):
|
|
94 |
"usage": None,
|
95 |
}
|
96 |
|
97 |
-
def stop_data(content, model):
|
|
|
98 |
return {
|
99 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
100 |
"object": "chat.completion.chunk",
|
@@ -111,6 +124,7 @@ def stop_data(content, model):
|
|
111 |
}
|
112 |
|
113 |
def create_chat_completion_data(content: str, model: str, finish_reason: Optional[str] = None) -> Dict[str, Any]:
|
|
|
114 |
return {
|
115 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
116 |
"object": "chat.completion.chunk",
|
@@ -126,19 +140,23 @@ def create_chat_completion_data(content: str, model: str, finish_reason: Optiona
|
|
126 |
"usage": None,
|
127 |
}
|
128 |
|
129 |
-
def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
|
|
130 |
if credentials.credentials != APP_SECRET:
|
131 |
raise HTTPException(status_code=403, detail="Invalid APP_SECRET")
|
132 |
return credentials.credentials
|
133 |
|
134 |
-
# Utility function to replace escaped newlines
|
135 |
def replace_escaped_newlines(input_string: str) -> str:
|
|
|
136 |
return input_string.replace("\\n", "\n")
|
137 |
|
|
|
138 |
# API Endpoints
|
|
|
139 |
|
140 |
@app.options("/hf/v1/chat/completions")
|
141 |
async def chat_completions_options():
|
|
|
142 |
return Response(
|
143 |
status_code=200,
|
144 |
headers={
|
@@ -150,19 +168,22 @@ async def chat_completions_options():
|
|
150 |
|
151 |
@app.get("/hf/v1/models")
|
152 |
async def list_models():
|
|
|
153 |
return {"object": "list", "data": ALLOWED_MODELS}
|
154 |
|
155 |
@app.post("/hf/v1/chat/completions")
|
156 |
async def chat_completions(
|
157 |
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
158 |
):
|
|
|
159 |
logger.info(f"Received chat completion request for model: {request.model}")
|
160 |
|
161 |
# Validate model
|
162 |
-
|
|
|
163 |
raise HTTPException(
|
164 |
status_code=400,
|
165 |
-
detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(
|
166 |
)
|
167 |
|
168 |
# Generate a UUID
|
@@ -183,8 +204,8 @@ async def chat_completions(
|
|
183 |
'tz_name': 'Asia/Karachi',
|
184 |
'cid': 'C092SEMXM9BJ',
|
185 |
'model': request.model,
|
186 |
-
'search': False, #
|
187 |
-
'auto_search': False, #
|
188 |
'filter_search_history': False,
|
189 |
'from': 'chat',
|
190 |
'group_id': 'default',
|
@@ -198,9 +219,9 @@ async def chat_completions(
|
|
198 |
},
|
199 |
'tools': {
|
200 |
'auto': [
|
|
|
201 |
'text_to_image',
|
202 |
'data_analysis',
|
203 |
-
# 'search' has been removed to disable search functionality
|
204 |
],
|
205 |
},
|
206 |
'extra_info': {
|
@@ -216,26 +237,43 @@ async def chat_completions(
|
|
216 |
async with client.stream('POST', 'https://sider.ai/api/v3/completion/text', headers=headers, json=json_data, timeout=120.0) as response:
|
217 |
response.raise_for_status()
|
218 |
async for line in response.aiter_lines():
|
|
|
|
|
219 |
if line and ("[DONE]" not in line):
|
220 |
-
# Assuming the line starts with some prefix before JSON, e.g., "data: "
|
221 |
-
# Adjust if necessary based on actual response format
|
222 |
try:
|
223 |
-
# Remove
|
224 |
if line.startswith("data: "):
|
225 |
line_content = line[6:]
|
226 |
else:
|
227 |
line_content = line
|
228 |
|
229 |
-
#
|
230 |
-
content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
# Yield the formatted data
|
233 |
-
yield f"data: {json.dumps(create_chat_completion_data(
|
234 |
except json.JSONDecodeError as e:
|
235 |
-
logger.error(f"JSON decode error: {e}")
|
236 |
continue
|
237 |
else:
|
238 |
-
# Signal the end of the stream
|
239 |
if line and "[DONE]" in line:
|
240 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
|
241 |
yield "data: [DONE]\n\n"
|
@@ -277,6 +315,9 @@ async def chat_completions(
|
|
277 |
"usage": None,
|
278 |
}
|
279 |
|
280 |
-
#
|
|
|
|
|
|
|
281 |
if __name__ == "__main__":
|
282 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
14 |
from starlette.middleware.cors import CORSMiddleware
|
15 |
from starlette.responses import StreamingResponse, Response
|
16 |
|
17 |
+
# ==============================
|
18 |
+
# Configuration and Setup
|
19 |
+
# ==============================
|
20 |
+
|
21 |
# Configure logging
|
22 |
logging.basicConfig(
|
23 |
+
level=logging.DEBUG, # Set to DEBUG for detailed logs
|
24 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
25 |
)
|
26 |
logger = logging.getLogger(__name__)
|
27 |
|
|
|
72 |
# Security configuration
|
73 |
security = HTTPBearer()
|
74 |
|
75 |
+
# ==============================
|
76 |
+
# Pydantic Models
|
77 |
+
# ==============================
|
78 |
+
|
79 |
class Message(BaseModel):
|
80 |
role: str
|
81 |
content: str
|
|
|
85 |
messages: List[Message]
|
86 |
stream: Optional[bool] = False
|
87 |
|
88 |
+
# ==============================
|
89 |
+
# Helper Functions
|
90 |
+
# ==============================
|
91 |
+
|
92 |
+
def simulate_data(content: str, model: str) -> Dict[str, Any]:
|
93 |
+
"""Simulate chunked response data."""
|
94 |
return {
|
95 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
96 |
"object": "chat.completion.chunk",
|
|
|
106 |
"usage": None,
|
107 |
}
|
108 |
|
109 |
+
def stop_data(content: str, model: str) -> Dict[str, Any]:
|
110 |
+
"""Simulate the final chunk indicating the end of the response."""
|
111 |
return {
|
112 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
113 |
"object": "chat.completion.chunk",
|
|
|
124 |
}
|
125 |
|
126 |
def create_chat_completion_data(content: str, model: str, finish_reason: Optional[str] = None) -> Dict[str, Any]:
|
127 |
+
"""Create a structured chat completion data chunk."""
|
128 |
return {
|
129 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
130 |
"object": "chat.completion.chunk",
|
|
|
140 |
"usage": None,
|
141 |
}
|
142 |
|
143 |
+
def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(security)) -> str:
|
144 |
+
"""Verify the provided APP_SECRET."""
|
145 |
if credentials.credentials != APP_SECRET:
|
146 |
raise HTTPException(status_code=403, detail="Invalid APP_SECRET")
|
147 |
return credentials.credentials
|
148 |
|
|
|
149 |
def replace_escaped_newlines(input_string: str) -> str:
|
150 |
+
"""Replace escaped newline characters with actual newlines."""
|
151 |
return input_string.replace("\\n", "\n")
|
152 |
|
153 |
+
# ==============================
|
154 |
# API Endpoints
|
155 |
+
# ==============================
|
156 |
|
157 |
@app.options("/hf/v1/chat/completions")
|
158 |
async def chat_completions_options():
|
159 |
+
"""Handle CORS preflight requests."""
|
160 |
return Response(
|
161 |
status_code=200,
|
162 |
headers={
|
|
|
168 |
|
169 |
@app.get("/hf/v1/models")
|
170 |
async def list_models():
|
171 |
+
"""List all allowed models."""
|
172 |
return {"object": "list", "data": ALLOWED_MODELS}
|
173 |
|
174 |
@app.post("/hf/v1/chat/completions")
|
175 |
async def chat_completions(
|
176 |
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
177 |
):
|
178 |
+
"""Handle chat completion requests."""
|
179 |
logger.info(f"Received chat completion request for model: {request.model}")
|
180 |
|
181 |
# Validate model
|
182 |
+
allowed_model_ids = [model['id'] for model in ALLOWED_MODELS]
|
183 |
+
if request.model not in allowed_model_ids:
|
184 |
raise HTTPException(
|
185 |
status_code=400,
|
186 |
+
detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(allowed_model_ids)}",
|
187 |
)
|
188 |
|
189 |
# Generate a UUID
|
|
|
204 |
'tz_name': 'Asia/Karachi',
|
205 |
'cid': 'C092SEMXM9BJ',
|
206 |
'model': request.model,
|
207 |
+
'search': False, # Disable search
|
208 |
+
'auto_search': False, # Disable auto_search
|
209 |
'filter_search_history': False,
|
210 |
'from': 'chat',
|
211 |
'group_id': 'default',
|
|
|
219 |
},
|
220 |
'tools': {
|
221 |
'auto': [
|
222 |
+
'search', # Re-add search to maintain API expectations
|
223 |
'text_to_image',
|
224 |
'data_analysis',
|
|
|
225 |
],
|
226 |
},
|
227 |
'extra_info': {
|
|
|
237 |
async with client.stream('POST', 'https://sider.ai/api/v3/completion/text', headers=headers, json=json_data, timeout=120.0) as response:
|
238 |
response.raise_for_status()
|
239 |
async for line in response.aiter_lines():
|
240 |
+
if line:
|
241 |
+
logger.debug(f"Raw line received: {line}") # Log raw line
|
242 |
if line and ("[DONE]" not in line):
|
|
|
|
|
243 |
try:
|
244 |
+
# Remove 'data: ' prefix if present
|
245 |
if line.startswith("data: "):
|
246 |
line_content = line[6:]
|
247 |
else:
|
248 |
line_content = line
|
249 |
|
250 |
+
# Log the content before parsing
|
251 |
+
logger.debug(f"Line content to parse: {line_content}")
|
252 |
+
|
253 |
+
# Check if the line is not empty after stripping
|
254 |
+
if not line_content.strip():
|
255 |
+
logger.warning("Received an empty line, skipping.")
|
256 |
+
continue
|
257 |
+
|
258 |
+
# Attempt to parse JSON
|
259 |
+
parsed_json = json.loads(line_content)
|
260 |
+
|
261 |
+
# Ensure 'data' key exists
|
262 |
+
if "data" not in parsed_json:
|
263 |
+
logger.error(f"'data' key not found in the response: {parsed_json}")
|
264 |
+
continue
|
265 |
+
|
266 |
+
content_data = parsed_json["data"]
|
267 |
+
|
268 |
+
# Extract text content if available
|
269 |
+
text_content = content_data.get("text", "")
|
270 |
|
271 |
# Yield the formatted data
|
272 |
+
yield f"data: {json.dumps(create_chat_completion_data(text_content, request.model))}\n\n"
|
273 |
except json.JSONDecodeError as e:
|
274 |
+
logger.error(f"JSON decode error: {e} | Line: {line_content}")
|
275 |
continue
|
276 |
else:
|
|
|
277 |
if line and "[DONE]" in line:
|
278 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
|
279 |
yield "data: [DONE]\n\n"
|
|
|
315 |
"usage": None,
|
316 |
}
|
317 |
|
318 |
+
# ==============================
|
319 |
+
# Entry Point
|
320 |
+
# ==============================
|
321 |
+
|
322 |
if __name__ == "__main__":
|
323 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|