Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,15 @@ import json
|
|
| 2 |
import requests
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
-
from flask import Flask, request, Response, stream_with_context
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
ONYX_API_KEY = os.getenv("
|
| 11 |
ONYX_URL = "https://cloud.onyx.app/api/chat/send-chat-message"
|
| 12 |
|
| 13 |
def transform_to_openai_chunk(content, model_name, finish_reason=None):
|
| 14 |
-
"""Encapsulates content into OpenAI's SSE format."""
|
| 15 |
chunk = {
|
| 16 |
"id": f"chatcmpl-{int(time.time())}",
|
| 17 |
"object": "chat.completion.chunk",
|
|
@@ -28,14 +27,14 @@ def transform_to_openai_chunk(content, model_name, finish_reason=None):
|
|
| 28 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 29 |
def chat_proxy():
|
| 30 |
data = request.json
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
raw_model = data.get("model", "OpenAI / gpt-4o")
|
| 35 |
if "/" in raw_model:
|
| 36 |
provider, version = [part.strip() for part in raw_model.split("/", 1)]
|
| 37 |
else:
|
| 38 |
-
# Fallback if the user doesn't use a slash
|
| 39 |
provider, version = "OpenAI", raw_model.strip()
|
| 40 |
|
| 41 |
messages = data.get("messages", [])
|
|
@@ -48,7 +47,7 @@ def chat_proxy():
|
|
| 48 |
"model_version": version,
|
| 49 |
"temperature": data.get("temperature", 0.7)
|
| 50 |
},
|
| 51 |
-
"stream": True, #
|
| 52 |
"include_citations": True,
|
| 53 |
"deep_research": False,
|
| 54 |
"parent_message_id": -1,
|
|
@@ -63,17 +62,22 @@ def chat_proxy():
|
|
| 63 |
def generate():
|
| 64 |
try:
|
| 65 |
with requests.post(ONYX_URL, json=onyx_payload, headers=headers, stream=True) as r:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
for line in r.iter_lines():
|
| 67 |
if not line:
|
| 68 |
continue
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
# Finalize the stream for the OpenAI SDK
|
| 77 |
yield transform_to_openai_chunk(None, raw_model, finish_reason="stop")
|
| 78 |
yield "data: [DONE]\n\n"
|
| 79 |
except Exception as e:
|
|
@@ -82,117 +86,9 @@ def chat_proxy():
|
|
| 82 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
| 83 |
|
| 84 |
@app.route('/')
|
| 85 |
-
def
|
| 86 |
-
return "
|
| 87 |
-
|
| 88 |
-
if __name__ == '__main__':
|
| 89 |
-
# HF.co listens on 7860
|
| 90 |
-
app.run(host='0.0.0.0', port=7860)
|
| 91 |
-
if not data or 'messages' not in data:
|
| 92 |
-
return jsonify({"error": "No messages provided"}), 400
|
| 93 |
-
|
| 94 |
-
# 🔒 Client controls routing directly
|
| 95 |
-
model_provider = data.get("model_provider", "OpenAI")
|
| 96 |
-
model = data.get("model", "gpt-5.2")
|
| 97 |
-
temperature = float(data.get("temperature", 0.7))
|
| 98 |
-
client_stream = bool(data.get("stream", False))
|
| 99 |
-
|
| 100 |
-
user_input = data["messages"][-1]["content"]
|
| 101 |
-
|
| 102 |
-
payload = {
|
| 103 |
-
"message": user_input,
|
| 104 |
-
|
| 105 |
-
# persona_id present but NOT used
|
| 106 |
-
"chat_session_info": {
|
| 107 |
-
"persona_id": 0
|
| 108 |
-
},
|
| 109 |
-
|
| 110 |
-
# 🔥 DIRECT client routing
|
| 111 |
-
"llm_override": {
|
| 112 |
-
"model_provider": model_provider,
|
| 113 |
-
"model_version": model,
|
| 114 |
-
"temperature": temperature
|
| 115 |
-
},
|
| 116 |
-
|
| 117 |
-
"stream": True,
|
| 118 |
-
"include_citations": True
|
| 119 |
-
}
|
| 120 |
-
|
| 121 |
-
headers = {
|
| 122 |
-
"Authorization": f"Bearer {API_KEY}",
|
| 123 |
-
"Content-Type": "application/json"
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
response = requests.post(
|
| 127 |
-
ONYX_URL,
|
| 128 |
-
json=payload,
|
| 129 |
-
headers=headers,
|
| 130 |
-
stream=True
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
# ===================== STREAMING =====================
|
| 134 |
-
if client_stream:
|
| 135 |
-
def event_stream():
|
| 136 |
-
chat_id = f"chatcmpl-{uuid.uuid4()}"
|
| 137 |
-
sent_any = False
|
| 138 |
-
|
| 139 |
-
for line in response.iter_lines(decode_unicode=True):
|
| 140 |
-
if not line:
|
| 141 |
-
continue
|
| 142 |
-
try:
|
| 143 |
-
packet = json.loads(line)
|
| 144 |
-
if "user_message_id" in packet:
|
| 145 |
-
continue
|
| 146 |
-
|
| 147 |
-
obj = packet.get("obj", {})
|
| 148 |
-
if obj.get("type") == "message_delta":
|
| 149 |
-
sent_any = True
|
| 150 |
-
yield f"data: {json.dumps({'id': chat_id,'object':'chat.completion.chunk','choices':[{'delta':{'content':obj.get('content','')}}]})}\n\n"
|
| 151 |
-
|
| 152 |
-
elif obj.get("type") == "stop":
|
| 153 |
-
if not sent_any:
|
| 154 |
-
yield f"data: {json.dumps({'id': chat_id,'object':'chat.completion.chunk','choices':[{'delta':{'content':'(No response from model)'}}]})}\n\n"
|
| 155 |
-
yield "data: [DONE]\n\n"
|
| 156 |
-
break
|
| 157 |
-
except:
|
| 158 |
-
continue
|
| 159 |
-
|
| 160 |
-
return Response(event_stream(), mimetype="text/event-stream")
|
| 161 |
-
|
| 162 |
-
# ===================== NON-STREAM =====================
|
| 163 |
-
full_text = []
|
| 164 |
-
for line in response.iter_lines(decode_unicode=True):
|
| 165 |
-
if not line:
|
| 166 |
-
continue
|
| 167 |
-
try:
|
| 168 |
-
packet = json.loads(line)
|
| 169 |
-
if "user_message_id" in packet:
|
| 170 |
-
continue
|
| 171 |
-
obj = packet.get("obj", {})
|
| 172 |
-
if obj.get("type") == "message_delta":
|
| 173 |
-
full_text.append(obj.get("content", ""))
|
| 174 |
-
elif obj.get("type") == "stop":
|
| 175 |
-
break
|
| 176 |
-
except:
|
| 177 |
-
continue
|
| 178 |
-
|
| 179 |
-
if not full_text:
|
| 180 |
-
full_text = ["(No response from model)"]
|
| 181 |
-
|
| 182 |
-
return jsonify({
|
| 183 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 184 |
-
"object": "chat.completion",
|
| 185 |
-
"created": int(time.time()),
|
| 186 |
-
"model": model,
|
| 187 |
-
"choices": [{
|
| 188 |
-
"index": 0,
|
| 189 |
-
"message": {
|
| 190 |
-
"role": "assistant",
|
| 191 |
-
"content": "".join(full_text)
|
| 192 |
-
},
|
| 193 |
-
"finish_reason": "stop"
|
| 194 |
-
}]
|
| 195 |
-
})
|
| 196 |
|
| 197 |
if __name__ == '__main__':
|
|
|
|
| 198 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 2 |
import requests
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
+
from flask import Flask, request, Response, stream_with_context, jsonify
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# Config from Hugging Face Secrets
|
| 10 |
+
ONYX_API_KEY = os.getenv("ONYX_API_KEY")
|
| 11 |
ONYX_URL = "https://cloud.onyx.app/api/chat/send-chat-message"
|
| 12 |
|
| 13 |
def transform_to_openai_chunk(content, model_name, finish_reason=None):
|
|
|
|
| 14 |
chunk = {
|
| 15 |
"id": f"chatcmpl-{int(time.time())}",
|
| 16 |
"object": "chat.completion.chunk",
|
|
|
|
| 27 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 28 |
def chat_proxy():
|
| 29 |
data = request.json
|
| 30 |
+
if not data or "messages" not in data:
|
| 31 |
+
return jsonify({"error": "No messages provided"}), 400
|
| 32 |
+
|
| 33 |
+
# Handle "Provider / Model ID" with gaps and caps
|
| 34 |
raw_model = data.get("model", "OpenAI / gpt-4o")
|
| 35 |
if "/" in raw_model:
|
| 36 |
provider, version = [part.strip() for part in raw_model.split("/", 1)]
|
| 37 |
else:
|
|
|
|
| 38 |
provider, version = "OpenAI", raw_model.strip()
|
| 39 |
|
| 40 |
messages = data.get("messages", [])
|
|
|
|
| 47 |
"model_version": version,
|
| 48 |
"temperature": data.get("temperature", 0.7)
|
| 49 |
},
|
| 50 |
+
"stream": True, # Forced ON
|
| 51 |
"include_citations": True,
|
| 52 |
"deep_research": False,
|
| 53 |
"parent_message_id": -1,
|
|
|
|
| 62 |
def generate():
|
| 63 |
try:
|
| 64 |
with requests.post(ONYX_URL, json=onyx_payload, headers=headers, stream=True) as r:
|
| 65 |
+
# Basic error handling for the Onyx API response
|
| 66 |
+
if r.status_code != 200:
|
| 67 |
+
yield f"data: {json.dumps({'error': 'Onyx API error', 'details': r.text})}\n\n"
|
| 68 |
+
return
|
| 69 |
+
|
| 70 |
for line in r.iter_lines():
|
| 71 |
if not line:
|
| 72 |
continue
|
| 73 |
|
| 74 |
+
try:
|
| 75 |
+
packet = json.loads(line.decode('utf-8'))
|
| 76 |
+
if packet.get("type") == "message_delta":
|
| 77 |
+
yield transform_to_openai_chunk(packet.get("delta", ""), raw_model)
|
| 78 |
+
except:
|
| 79 |
+
continue
|
| 80 |
|
|
|
|
| 81 |
yield transform_to_openai_chunk(None, raw_model, finish_reason="stop")
|
| 82 |
yield "data: [DONE]\n\n"
|
| 83 |
except Exception as e:
|
|
|
|
| 86 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
| 87 |
|
| 88 |
@app.route('/')
|
| 89 |
+
def health():
|
| 90 |
+
return jsonify({"status": "running", "port": 7860}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
if __name__ == '__main__':
|
| 93 |
+
# HF Spaces requires port 7860
|
| 94 |
app.run(host='0.0.0.0', port=7860)
|