Update app.py
Browse files
app.py
CHANGED
@@ -37,6 +37,52 @@ print(f"Loaded {len(train_texts)} examples from {data_file}")
|
|
37 |
# Model save directory
|
38 |
model_save_dir = "./results/model"
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
@app.route('/adapt', methods=['POST'])
|
41 |
def adapt_model():
|
42 |
try:
|
@@ -104,11 +150,33 @@ def adapt_model():
|
|
104 |
return jsonify({
|
105 |
'input': user_input,
|
106 |
'self_edit': self_edit,
|
107 |
-
'response': response
|
|
|
|
|
108 |
})
|
109 |
|
110 |
except Exception as e:
|
111 |
return jsonify({'error': str(e)}), 500
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
if __name__ == '__main__':
|
114 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
37 |
# Model save directory
|
38 |
model_save_dir = "./results/model"
|
39 |
|
40 |
+
@app.route('/')
|
41 |
+
def home():
|
42 |
+
"""Root endpoint to show API status and usage"""
|
43 |
+
return jsonify({
|
44 |
+
'status': 'SEAL Framework API is running',
|
45 |
+
'version': '1.0.0',
|
46 |
+
'model': model_name,
|
47 |
+
'device': str(device),
|
48 |
+
'training_examples': len(train_texts),
|
49 |
+
'endpoints': {
|
50 |
+
'/': 'GET - API status and information',
|
51 |
+
'/adapt': 'POST - Adaptive model training and response',
|
52 |
+
'/health': 'GET - Health check'
|
53 |
+
},
|
54 |
+
'usage': {
|
55 |
+
'adapt_endpoint': {
|
56 |
+
'method': 'POST',
|
57 |
+
'content_type': 'application/json',
|
58 |
+
'body': {'text': 'Your input text here'},
|
59 |
+
'example': 'curl -X POST -H "Content-Type: application/json" -d \'{"text":"Hello world"}\' /adapt'
|
60 |
+
}
|
61 |
+
}
|
62 |
+
})
|
63 |
+
|
64 |
+
@app.route('/health')
|
65 |
+
def health():
|
66 |
+
"""Health check endpoint"""
|
67 |
+
try:
|
68 |
+
# Simple model test
|
69 |
+
test_input = "Health check"
|
70 |
+
inputs = tokenizer(test_input, return_tensors="pt", truncation=True, max_length=32).to(device)
|
71 |
+
with torch.no_grad():
|
72 |
+
outputs = model.generate(**inputs, max_length=40, num_return_sequences=1, do_sample=False)
|
73 |
+
|
74 |
+
return jsonify({
|
75 |
+
'status': 'healthy',
|
76 |
+
'model_loaded': True,
|
77 |
+
'device': str(device),
|
78 |
+
'training_examples': len(train_texts)
|
79 |
+
})
|
80 |
+
except Exception as e:
|
81 |
+
return jsonify({
|
82 |
+
'status': 'unhealthy',
|
83 |
+
'error': str(e)
|
84 |
+
}), 500
|
85 |
+
|
86 |
@app.route('/adapt', methods=['POST'])
|
87 |
def adapt_model():
|
88 |
try:
|
|
|
150 |
return jsonify({
|
151 |
'input': user_input,
|
152 |
'self_edit': self_edit,
|
153 |
+
'response': response,
|
154 |
+
'training_examples': len(train_texts),
|
155 |
+
'status': 'Model adapted successfully'
|
156 |
})
|
157 |
|
158 |
except Exception as e:
|
159 |
return jsonify({'error': str(e)}), 500
|
160 |
|
161 |
+
@app.errorhandler(404)
|
162 |
+
def not_found(error):
|
163 |
+
"""Custom 404 handler"""
|
164 |
+
return jsonify({
|
165 |
+
'error': 'Endpoint not found',
|
166 |
+
'available_endpoints': {
|
167 |
+
'/': 'GET - API information',
|
168 |
+
'/health': 'GET - Health check',
|
169 |
+
'/adapt': 'POST - Adaptive model training'
|
170 |
+
}
|
171 |
+
}), 404
|
172 |
+
|
173 |
+
@app.errorhandler(500)
|
174 |
+
def internal_error(error):
|
175 |
+
"""Custom 500 handler"""
|
176 |
+
return jsonify({
|
177 |
+
'error': 'Internal server error',
|
178 |
+
'message': 'Please check the server logs for more details'
|
179 |
+
}), 500
|
180 |
+
|
181 |
if __name__ == '__main__':
|
182 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|