Spaces:
Runtime error
Runtime error
Update api.py
Browse files
api.py
CHANGED
@@ -10,37 +10,43 @@ def chat_completions():
|
|
10 |
messages = data.get('messages', [])
|
11 |
max_tokens = data.get('max_tokens', 560)
|
12 |
temperature = data.get('temperature', 0.8)
|
13 |
-
|
14 |
-
if not messages:
|
15 |
-
return jsonify({"error": "messages
|
16 |
-
|
17 |
try:
|
18 |
start_time = time.time()
|
|
|
|
|
19 |
result = generate_chat_completion(
|
20 |
-
|
21 |
max_tokens=max_tokens,
|
22 |
temperature=temperature
|
23 |
)
|
|
|
|
|
|
|
24 |
elapsed = time.time() - start_time
|
25 |
-
|
26 |
return jsonify({
|
27 |
-
"model": "Mistral-7B-Instruct",
|
28 |
"choices": [{
|
29 |
"message": {
|
30 |
"role": "assistant",
|
31 |
-
"content":
|
32 |
}
|
33 |
}],
|
34 |
"usage": {
|
35 |
"generation_time": round(elapsed, 2)
|
36 |
}
|
37 |
})
|
|
|
38 |
except Exception as e:
|
39 |
return jsonify({"error": str(e)}), 500
|
40 |
|
41 |
@app.route('/')
|
42 |
def health_check():
|
43 |
-
return "API is running", 200
|
44 |
|
45 |
if __name__ == '__main__':
|
46 |
-
app.run(host='0.0.0.0', port=8081)
|
|
|
10 |
messages = data.get('messages', [])
|
11 |
max_tokens = data.get('max_tokens', 560)
|
12 |
temperature = data.get('temperature', 0.8)
|
13 |
+
|
14 |
+
if not messages or not isinstance(messages, list):
|
15 |
+
return jsonify({"error": "A valid 'messages' list is required."}), 400
|
16 |
+
|
17 |
try:
|
18 |
start_time = time.time()
|
19 |
+
|
20 |
+
# Expecting plain role-content dicts (not Gradio tuples)
|
21 |
result = generate_chat_completion(
|
22 |
+
message_history=messages,
|
23 |
max_tokens=max_tokens,
|
24 |
temperature=temperature
|
25 |
)
|
26 |
+
|
27 |
+
# Get only the assistant's latest message
|
28 |
+
assistant_msg = result[-1] if isinstance(result, list) else result
|
29 |
elapsed = time.time() - start_time
|
30 |
+
|
31 |
return jsonify({
|
32 |
+
"model": "mistralai/Mistral-7B-Instruct-v0.2",
|
33 |
"choices": [{
|
34 |
"message": {
|
35 |
"role": "assistant",
|
36 |
+
"content": assistant_msg
|
37 |
}
|
38 |
}],
|
39 |
"usage": {
|
40 |
"generation_time": round(elapsed, 2)
|
41 |
}
|
42 |
})
|
43 |
+
|
44 |
except Exception as e:
|
45 |
return jsonify({"error": str(e)}), 500
|
46 |
|
47 |
@app.route('/')
|
48 |
def health_check():
|
49 |
+
return "LLM API is running", 200
|
50 |
|
51 |
if __name__ == '__main__':
|
52 |
+
app.run(host='0.0.0.0', port=8081)
|