-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_launcher.py
More file actions
328 lines (271 loc) · 10.1 KB
/
api_launcher.py
File metadata and controls
328 lines (271 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# api_launcher.py
"""
Simple launcher for any Python library API
Just run: python api_launcher.py <library_name>
"""
import os
import sys
import yaml
import json
import argparse
from typing import Dict, Any
class SimpleAPILauncher:
"""Dead simple API launcher for any library"""
# One-line configurations for common libraries
SIMPLE_CONFIGS = {
# Computer Vision
'yolo': {
'install': 'pip install ultralytics',
'import': 'from ultralytics import YOLO',
'init': 'model = YOLO("yolov8n.pt")',
'endpoints': {
'detect': 'lambda img: model(img)',
'track': 'lambda img: model.track(img, persist=True)'
}
},
# LLMs
'gpt2': {
'install': 'pip install transformers torch',
'import': 'from transformers import pipeline',
'init': 'model = pipeline("text-generation", model="gpt2")',
'endpoints': {
'generate': 'lambda text: model(text, max_length=100)'
}
},
'phi2': {
'install': 'pip install transformers torch',
'import': 'from transformers import AutoModelForCausalLM, AutoTokenizer',
'init': '''
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", trust_remote_code=True)
''',
'endpoints': {
'generate': '''lambda prompt: tokenizer.decode(
model.generate(
tokenizer(prompt, return_tensors="pt").input_ids,
max_length=100
)[0]
)'''
}
},
# MLX (Apple Silicon)
'mlx_whisper': {
'install': 'pip install mlx-whisper',
'import': 'import mlx_whisper',
'init': '# MLX Whisper loads models on demand',
'endpoints': {
'transcribe': 'lambda audio: mlx_whisper.transcribe(audio, path_or_hf_repo="tiny")'
}
},
'mlx_stable_diffusion': {
'install': 'pip install mlx-stable-diffusion',
'import': 'from mlx_stable_diffusion import StableDiffusion',
'init': 'model = StableDiffusion()',
'endpoints': {
'generate': 'lambda prompt: model.generate_image(prompt)'
}
},
# Data Science
'pandas': {
'install': 'pip install pandas',
'import': 'import pandas as pd',
'init': '# Pandas is ready',
'endpoints': {
'read_csv': 'pd.read_csv',
'read_excel': 'pd.read_excel',
'describe': 'lambda df: df.describe().to_dict()'
}
},
'sklearn': {
'install': 'pip install scikit-learn',
'import': 'from sklearn.ensemble import RandomForestClassifier',
'init': 'model = RandomForestClassifier()',
'endpoints': {
'fit': 'lambda X, y: model.fit(X, y)',
'predict': 'lambda X: model.predict(X).tolist()'
}
},
# Image Processing
'opencv': {
'install': 'pip install opencv-python',
'import': 'import cv2',
'init': '# OpenCV ready',
'endpoints': {
'resize': 'lambda img, size: cv2.resize(img, size)',
'blur': 'lambda img: cv2.GaussianBlur(img, (5,5), 0)',
'edge_detect': 'lambda img: cv2.Canny(img, 100, 200)'
}
},
'pillow': {
'install': 'pip install Pillow',
'import': 'from PIL import Image, ImageFilter',
'init': '# PIL ready',
'endpoints': {
'open': 'Image.open',
'resize': 'lambda img, size: img.resize(size)',
'filter': 'lambda img, f: img.filter(getattr(ImageFilter, f))'
}
}
}
@classmethod
def create_simple_api(cls, library: str, port: int = 8000):
"""Create API from simple config"""
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
import uvicorn
if library not in cls.SIMPLE_CONFIGS:
print(f"❌ Unknown library: {library}")
print(f"Available: {', '.join(cls.SIMPLE_CONFIGS.keys())}")
return
config = cls.SIMPLE_CONFIGS[library]
# Check if library is installed
try:
exec(config['import'])
except ImportError:
print(f"📦 {library} not installed. Install with:")
print(f" {config['install']}")
return
# Create FastAPI app
app = FastAPI(
title=f"{library.upper()} API",
description=f"Auto-generated API for {library}"
)
# Initialize model/library
exec(config['import'], globals())
exec(config['init'], globals())
# Create endpoints
for endpoint_name, endpoint_code in config['endpoints'].items():
# Create the function
if endpoint_code.startswith('lambda'):
func = eval(endpoint_code)
else:
func = eval(endpoint_code)
# Create FastAPI endpoint
def create_endpoint(name, fn):
def endpoint(**kwargs):
try:
result = fn(**kwargs)
# Convert numpy arrays and other types
if hasattr(result, 'tolist'):
result = result.tolist()
elif hasattr(result, '__dict__'):
result = result.__dict__
return {"result": result, "success": True}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
endpoint.__name__ = f"{name}_endpoint"
return endpoint
# Add to FastAPI
app.post(f"/{endpoint_name}")(create_endpoint(endpoint_name, func))
# Add health check
@app.get("/")
def root():
return {
"library": library,
"endpoints": [f"/{name}" for name in config['endpoints'].keys()],
"status": "ready"
}
# Run the server
print(f"\n🚀 Starting {library.upper()} API")
print(f"📍 URL: http://localhost:{port}")
print(f"📚 Docs: http://localhost:{port}/docs")
print(f"\nEndpoints:")
for ep in config['endpoints'].keys():
print(f" POST /{ep}")
print("\nPress Ctrl+C to stop\n")
uvicorn.run(app, host="0.0.0.0", port=port)
# ============= Even Simpler: One Command =============
def one_command_api():
"""
Super simple API creation
Usage: python -c "from api_launcher import *; api('yolo')"
"""
def api(library: str, port: int = 8000):
SimpleAPILauncher.create_simple_api(library, port)
return api
# Make it available globally
api = one_command_api()
# ============= Auto-installer =============
class AutoInstaller:
"""Automatically install and configure libraries"""
@staticmethod
def setup_library(library: str):
"""Auto-install and configure a library"""
configs = SimpleAPILauncher.SIMPLE_CONFIGS
if library not in configs:
print(f"Unknown library: {library}")
return False
config = configs[library]
# Try import
try:
exec(config['import'])
print(f"✅ {library} is already installed")
return True
except ImportError:
print(f"📦 Installing {library}...")
os.system(config['install'])
# Verify installation
try:
exec(config['import'])
print(f"✅ {library} installed successfully")
return True
except ImportError:
print(f"❌ Failed to install {library}")
return False
# ============= CLI =============
def main():
parser = argparse.ArgumentParser(
description="Universal API Launcher - Turn any Python library into an API instantly"
)
parser.add_argument(
"library",
help="Library name (yolo, gpt2, pandas, sklearn, opencv, etc.)"
)
parser.add_argument(
"--port", "-p",
type=int,
default=8000,
help="Port to run the API on"
)
parser.add_argument(
"--install", "-i",
action="store_true",
help="Auto-install the library if not found"
)
parser.add_argument(
"--list", "-l",
action="store_true",
help="List all available libraries"
)
args = parser.parse_args()
if args.list:
print("\n📚 Available Libraries:")
for lib, config in SimpleAPILauncher.SIMPLE_CONFIGS.items():
print(f"\n{lib}:")
print(f" Install: {config['install']}")
print(f" Endpoints: {', '.join(config['endpoints'].keys())}")
return
if args.install:
AutoInstaller.setup_library(args.library)
SimpleAPILauncher.create_simple_api(args.library, args.port)
if __name__ == "__main__":
main()
# ============= Ultra Simple Usage =============
"""
USAGE EXAMPLES:
1. One-liner to start any API:
python api_launcher.py yolo
2. With custom port:
python api_launcher.py gpt2 --port 8080
3. Auto-install if needed:
python api_launcher.py pandas --install
4. List available libraries:
python api_launcher.py --list
5. In Python script:
from api_launcher import api
api('yolo', port=8001)
6. Test with curl:
curl -X POST http://localhost:8000/detect \
-H "Content-Type: application/json" \
-d '{"img": "path/to/image.jpg"}'
"""