Sontran0108 commited on
Commit
31a1d92
·
1 Parent(s): da1f2ac

Add API endpoints for external inference calls

Browse files

- Add Flask API with /predict, /health, and /info endpoints
- Add CORS support for cross-origin requests
- Update requirements.txt with flask-cors dependency
- Update README with comprehensive API documentation
- Include Python example for easy integration

Files changed (3) hide show
  1. README.md +43 -3
  2. app.py +97 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -26,12 +26,52 @@ This is a custom handler for the Grammarly CoEdIT model, providing grammar corre
26
 
27
  ## API Usage
28
 
29
- The Space provides a REST API endpoint at `/predict`:
30
 
 
31
  ```bash
32
- curl -X POST "https://your-space-url.hf.space/predict" \
33
  -H "Content-Type: application/json" \
34
- -d '{"inputs": ["Fix the grammar: When I grow up, I start to understand what he said is quite right."]}'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  ```
36
 
37
  ## Response Format
 
26
 
27
  ## API Usage
28
 
29
+ The Space provides multiple REST API endpoints:
30
 
31
+ ### Main Inference Endpoint
32
  ```bash
33
+ curl -X POST "https://sxtran-grammar-corrector-app.hf.space/predict" \
34
  -H "Content-Type: application/json" \
35
+ -d '{
36
+ "inputs": ["Fix the grammar: When I grow up, I start to understand what he said is quite right."],
37
+ "parameters": {
38
+ "num_return_sequences": 1,
39
+ "temperature": 1.0
40
+ }
41
+ }'
42
+ ```
43
+
44
+ ### Health Check
45
+ ```bash
46
+ curl -X GET "https://sxtran-grammar-corrector-app.hf.space/health"
47
+ ```
48
+
49
+ ### API Information
50
+ ```bash
51
+ curl -X GET "https://sxtran-grammar-corrector-app.hf.space/info"
52
+ ```
53
+
54
+ ### Python Example
55
+ ```python
56
+ import requests
57
+
58
+ # API endpoint
59
+ url = "https://sxtran-grammar-corrector-app.hf.space/predict"
60
+
61
+ # Request data
62
+ data = {
63
+ "inputs": ["Fix the grammar: When I grow up, I start to understand what he said is quite right."],
64
+ "parameters": {
65
+ "num_return_sequences": 1,
66
+ "temperature": 1.0
67
+ }
68
+ }
69
+
70
+ # Make request
71
+ response = requests.post(url, json=data)
72
+ result = response.json()
73
+
74
+ print(result)
75
  ```
76
 
77
  ## Response Format
app.py CHANGED
@@ -6,6 +6,8 @@ import gradio as gr
6
  import sys
7
  import os
8
  import json
 
 
9
 
10
  # Add current directory to path so we can import handler
11
  sys.path.append(os.path.dirname(os.path.abspath(__file__)))
@@ -138,7 +140,102 @@ def create_interface():
138
 
139
  return demo
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  # Create the interface
142
  if __name__ == "__main__":
143
  demo = create_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
6
  import sys
7
  import os
8
  import json
9
+ from flask import Flask, request, jsonify
10
+ from flask_cors import CORS
11
 
12
  # Add current directory to path so we can import handler
13
  sys.path.append(os.path.dirname(os.path.abspath(__file__)))
 
140
 
141
  return demo
142
 
143
+ # Create Flask app for API endpoints
144
+ app = Flask(__name__)
145
+ CORS(app) # Enable CORS for cross-origin requests
146
+
147
+ @app.route('/predict', methods=['POST'])
148
+ def api_predict():
149
+ """API endpoint for inference calls from external applications"""
150
+ try:
151
+ # Get JSON data from request
152
+ data = request.get_json()
153
+
154
+ if not data:
155
+ return jsonify({
156
+ "success": False,
157
+ "error": "No JSON data provided"
158
+ }), 400
159
+
160
+ # Extract inputs and parameters
161
+ inputs = data.get('inputs', [])
162
+ parameters = data.get('parameters', {})
163
+
164
+ # If inputs is a single string, wrap it in a list
165
+ if isinstance(inputs, str):
166
+ inputs = [inputs]
167
+
168
+ if not inputs:
169
+ return jsonify({
170
+ "success": False,
171
+ "error": "No inputs provided"
172
+ }), 400
173
+
174
+ # Process through handler
175
+ result = handler({
176
+ "inputs": inputs,
177
+ "parameters": parameters
178
+ })
179
+
180
+ return jsonify(result)
181
+
182
+ except Exception as e:
183
+ return jsonify({
184
+ "success": False,
185
+ "error": f"Error processing request: {str(e)}"
186
+ }), 500
187
+
188
+ @app.route('/health', methods=['GET'])
189
+ def health_check():
190
+ """Health check endpoint"""
191
+ return jsonify({
192
+ "status": "healthy",
193
+ "handler_initialized": handler is not None
194
+ })
195
+
196
+ @app.route('/info', methods=['GET'])
197
+ def api_info():
198
+ """API information endpoint"""
199
+ return jsonify({
200
+ "name": "CoEdIT Grammar Corrector API",
201
+ "version": "1.0.0",
202
+ "description": "API for grammar correction and text enhancement using Grammarly CoEdIT model",
203
+ "endpoints": {
204
+ "/predict": "POST - Main inference endpoint",
205
+ "/health": "GET - Health check",
206
+ "/info": "GET - API information"
207
+ },
208
+ "input_format": {
209
+ "inputs": "List of strings or single string to process",
210
+ "parameters": {
211
+ "num_return_sequences": "Number of variations to generate (default: 1)",
212
+ "temperature": "Sampling temperature (default: 1.0)"
213
+ }
214
+ },
215
+ "example_request": {
216
+ "inputs": ["Fix the grammar: When I grow up, I start to understand what he said is quite right."],
217
+ "parameters": {
218
+ "num_return_sequences": 1,
219
+ "temperature": 1.0
220
+ }
221
+ }
222
+ })
223
+
224
  # Create the interface
225
  if __name__ == "__main__":
226
  demo = create_interface()
227
+
228
+ # Launch both Gradio and Flask
229
+ # Gradio will run on port 7860, Flask on port 7861
230
+ import threading
231
+
232
+ def run_flask():
233
+ app.run(host="0.0.0.0", port=7861, debug=False)
234
+
235
+ # Start Flask in a separate thread
236
+ flask_thread = threading.Thread(target=run_flask)
237
+ flask_thread.daemon = True
238
+ flask_thread.start()
239
+
240
+ # Launch Gradio
241
  demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt CHANGED
@@ -1,6 +1,7 @@
1
  torch>=2.0.0
2
  transformers>=4.30.0
3
  flask>=2.3.0
 
4
  requests>=2.31.0
5
  safetensors>=0.3.0
6
  gradio>=4.0.0
 
1
  torch>=2.0.0
2
  transformers>=4.30.0
3
  flask>=2.3.0
4
+ flask-cors>=4.0.0
5
  requests>=2.31.0
6
  safetensors>=0.3.0
7
  gradio>=4.0.0