tianlong12 commited on
Commit
31853b5
·
verified ·
1 Parent(s): 531dd91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -27
app.py CHANGED
@@ -5,7 +5,7 @@ import json
5
  import logging
6
 
7
  app = Flask(__name__)
8
- logging.basicConfig(level=logging.INFO)
9
 
10
  DEEPINFRA_API_URL = "https://api.deepinfra.com/v1/openai/chat/completions"
11
  API_KEY = os.environ.get("API_KEY")
@@ -19,10 +19,19 @@ def authenticate():
19
 
20
  @app.route('/hf/v1/chat/completions', methods=['POST'])
21
  def chat_completions():
 
 
 
22
  if not authenticate():
 
23
  return jsonify({"error": "Unauthorized"}), 401
24
 
25
- openai_request = request.json
 
 
 
 
 
26
  logging.info(f"Received request: {openai_request}")
27
 
28
  deepinfra_request = {
@@ -41,6 +50,8 @@ def chat_completions():
41
  try:
42
  response = requests.post(DEEPINFRA_API_URL, json=deepinfra_request, headers=headers, stream=deepinfra_request["stream"])
43
  response.raise_for_status()
 
 
44
  except requests.RequestException as e:
45
  logging.error(f"Error calling DeepInfra API: {str(e)}")
46
  return jsonify({"error": "Failed to call DeepInfra API"}), 500
@@ -49,31 +60,43 @@ def chat_completions():
49
  def generate():
50
  full_content = ""
51
  for line in response.iter_lines():
52
- if line:
53
- data = json.loads(line.decode('utf-8').split('data: ')[1])
54
- if data == "[DONE]":
55
- yield f"data: [DONE]\n\n"
56
- break
57
-
58
- delta_content = data['choices'][0]['delta'].get('content', '')
59
- full_content += delta_content
60
-
61
- openai_format = {
62
- "id": data['id'],
63
- "object": "chat.completion.chunk",
64
- "created": data['created'],
65
- "model": data['model'],
66
- "choices": [
67
- {
68
- "index": 0,
69
- "delta": {
70
- "content": delta_content
71
- },
72
- "finish_reason": data['choices'][0].get('finish_reason')
73
- }
74
- ]
75
- }
76
- yield f"data: {json.dumps(openai_format)}\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  # Send the final usage information
79
  if 'usage' in data:
@@ -121,5 +144,9 @@ def chat_completions():
121
  logging.error(f"Error processing DeepInfra response: {str(e)}")
122
  return jsonify({"error": "Failed to process DeepInfra response"}), 500
123
 
 
 
 
 
124
  if __name__ == '__main__':
125
  app.run(host='0.0.0.0', port=7860)
 
5
  import logging
6
 
7
  app = Flask(__name__)
8
+ logging.basicConfig(level=logging.DEBUG)
9
 
10
  DEEPINFRA_API_URL = "https://api.deepinfra.com/v1/openai/chat/completions"
11
  API_KEY = os.environ.get("API_KEY")
 
19
 
20
  @app.route('/hf/v1/chat/completions', methods=['POST'])
21
  def chat_completions():
22
+ logging.debug(f"Received headers: {request.headers}")
23
+ logging.debug(f"Received body: {request.get_data(as_text=True)}")
24
+
25
  if not authenticate():
26
+ logging.warning("Unauthorized access attempt")
27
  return jsonify({"error": "Unauthorized"}), 401
28
 
29
+ try:
30
+ openai_request = request.json
31
+ except json.JSONDecodeError:
32
+ logging.error("Invalid JSON in request body")
33
+ return jsonify({"error": "Invalid JSON in request body"}), 400
34
+
35
  logging.info(f"Received request: {openai_request}")
36
 
37
  deepinfra_request = {
 
50
  try:
51
  response = requests.post(DEEPINFRA_API_URL, json=deepinfra_request, headers=headers, stream=deepinfra_request["stream"])
52
  response.raise_for_status()
53
+ logging.debug(f"DeepInfra API response status: {response.status_code}")
54
+ logging.debug(f"DeepInfra API response headers: {response.headers}")
55
  except requests.RequestException as e:
56
  logging.error(f"Error calling DeepInfra API: {str(e)}")
57
  return jsonify({"error": "Failed to call DeepInfra API"}), 500
 
60
  def generate():
61
  full_content = ""
62
  for line in response.iter_lines():
63
+ if not line:
64
+ logging.warning("Received empty line from DeepInfra API")
65
+ continue
66
+ try:
67
+ line_text = line.decode('utf-8')
68
+ if line_text.startswith('data: '):
69
+ data_text = line_text.split('data: ', 1)[1]
70
+ if data_text == "[DONE]":
71
+ yield f"data: [DONE]\n\n"
72
+ break
73
+ data = json.loads(data_text)
74
+
75
+ delta_content = data['choices'][0]['delta'].get('content', '')
76
+ full_content += delta_content
77
+
78
+ openai_format = {
79
+ "id": data['id'],
80
+ "object": "chat.completion.chunk",
81
+ "created": data['created'],
82
+ "model": data['model'],
83
+ "choices": [
84
+ {
85
+ "index": 0,
86
+ "delta": {
87
+ "content": delta_content
88
+ },
89
+ "finish_reason": data['choices'][0].get('finish_reason')
90
+ }
91
+ ]
92
+ }
93
+ yield f"data: {json.dumps(openai_format)}\n\n"
94
+ except json.JSONDecodeError as e:
95
+ logging.error(f"JSON decode error: {e}. Raw line: {line}")
96
+ continue
97
+ except Exception as e:
98
+ logging.error(f"Error processing line: {e}. Raw line: {line}")
99
+ continue
100
 
101
  # Send the final usage information
102
  if 'usage' in data:
 
144
  logging.error(f"Error processing DeepInfra response: {str(e)}")
145
  return jsonify({"error": "Failed to process DeepInfra response"}), 500
146
 
147
+ @app.route('/')
148
+ def home():
149
+ return "Welcome to the API proxy server. Please use the /hf/v1/chat/completions endpoint for chat completions."
150
+
151
  if __name__ == '__main__':
152
  app.run(host='0.0.0.0', port=7860)