-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.py
More file actions
620 lines (509 loc) · 18.2 KB
/
api.py
File metadata and controls
620 lines (509 loc) · 18.2 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
"""
OpenKeywords API - FastAPI Application
RESTful API wrapper for the keyword generation pipeline.
Usage:
uvicorn api:app --reload --port 8001
API Docs:
- Swagger UI: http://localhost:8001/docs
- ReDoc: http://localhost:8001/redoc
- OpenAPI JSON: http://localhost:8001/openapi.json
"""
import asyncio
import os
import threading
import uuid
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Dict, List, Optional
from fastapi import BackgroundTasks, FastAPI, HTTPException, Path, Query
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, HttpUrl, field_validator
from run_pipeline import run_pipeline
# =============================================================================
# Pydantic Models for API
# =============================================================================
class JobStatus(str, Enum):
"""Job status enumeration."""
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
FAILED = "failed"
class KeywordRequest(BaseModel):
"""Request model for keyword generation."""
company_name: str = Field(
...,
min_length=1,
max_length=200,
description="Company name",
examples=["Stripe"],
)
company_url: Optional[HttpUrl] = Field(
default=None,
description="Company website URL for deep analysis",
examples=["https://stripe.com"],
)
target_count: int = Field(
default=50,
ge=10,
le=500,
description="Target number of keywords to generate",
)
language: str = Field(
default="en",
min_length=2,
max_length=5,
description="Target language code",
)
region: str = Field(
default="us",
min_length=2,
max_length=5,
description="Target region/market code",
)
enable_research: bool = Field(
default=False,
description="Enable deep research (Reddit, Quora, forums)",
)
min_score: int = Field(
default=40,
ge=0,
le=100,
description="Minimum company-fit score",
)
cluster_count: int = Field(
default=6,
ge=2,
le=20,
description="Number of keyword clusters to create",
)
@field_validator("company_name")
@classmethod
def validate_company_name(cls, v: str) -> str:
if not v or not v.strip():
raise ValueError("Company name cannot be empty")
return v.strip()
class KeywordResult(BaseModel):
"""Individual keyword in results."""
keyword: str
intent: str
score: int
cluster_name: Optional[str] = None
is_question: bool = False
source: str = "ai_generated"
class ClusterResult(BaseModel):
"""Keyword cluster in results."""
name: str
keywords: List[str]
count: int
class StatisticsResult(BaseModel):
"""Statistics about generated keywords."""
total: int
avg_score: float
intent_breakdown: Dict[str, int] = {}
source_breakdown: Dict[str, int] = {}
class GenerationResponse(BaseModel):
"""Response model for keyword generation."""
keywords: List[KeywordResult]
clusters: List[ClusterResult]
statistics: StatisticsResult
processing_time_seconds: float
class JobResponse(BaseModel):
"""Response model for job status."""
job_id: str
status: JobStatus
created_at: str
completed_at: Optional[str] = None
progress: Optional[Dict[str, Any]] = None
result: Optional[GenerationResponse] = None
error: Optional[str] = None
class HealthResponse(BaseModel):
"""Health check response."""
status: str = "healthy"
version: str = "2.0.0"
gemini_configured: bool = False
timestamp: str
# =============================================================================
# Job Store (In-Memory)
# =============================================================================
class JobStore:
"""Thread-safe in-memory job store."""
def __init__(self):
self._jobs: Dict[str, Dict[str, Any]] = {}
self._lock = threading.Lock()
def create(self, job_id: str, request: KeywordRequest) -> Dict[str, Any]:
job = {
"job_id": job_id,
"status": JobStatus.PENDING,
"created_at": datetime.now(timezone.utc).isoformat(),
"completed_at": None,
"request": request.model_dump(),
"progress": None,
"result": None,
"error": None,
}
with self._lock:
self._jobs[job_id] = job
return job
def get(self, job_id: str) -> Optional[Dict[str, Any]]:
with self._lock:
return self._jobs.get(job_id)
def update(self, job_id: str, **kwargs) -> bool:
with self._lock:
if job_id not in self._jobs:
return False
self._jobs[job_id].update(kwargs)
return True
def delete(self, job_id: str) -> bool:
with self._lock:
if job_id in self._jobs:
del self._jobs[job_id]
return True
return False
def list_all(self) -> List[Dict[str, Any]]:
with self._lock:
return list(self._jobs.values())
def cleanup_old_jobs(self, max_age_hours: int = 24, max_jobs: int = 1000) -> int:
"""Remove old jobs to prevent memory leak."""
from datetime import timedelta
cutoff = datetime.now(timezone.utc) - timedelta(hours=max_age_hours)
removed = 0
with self._lock:
old_jobs = [
job_id for job_id, job in self._jobs.items()
if datetime.fromisoformat(job["created_at"]) < cutoff
]
for job_id in old_jobs:
del self._jobs[job_id]
removed += 1
if len(self._jobs) > max_jobs:
sorted_jobs = sorted(
self._jobs.items(),
key=lambda x: x[1]["created_at"]
)
excess = len(self._jobs) - max_jobs
for job_id, _ in sorted_jobs[:excess]:
del self._jobs[job_id]
removed += 1
return removed
def _validate_job_id(job_id: str) -> str:
"""Validate job_id is a valid UUID."""
try:
uuid.UUID(job_id)
return job_id
except ValueError:
raise HTTPException(status_code=400, detail="Invalid job_id format (must be UUID)")
def _sanitize_csv_value(value: Any) -> str:
"""Sanitize value for CSV to prevent formula injection."""
str_val = str(value)
if str_val and str_val[0] in ('=', '+', '-', '@', '\t', '\r'):
return f"'{str_val}"
return str_val
# Global job store
job_store = JobStore()
# =============================================================================
# FastAPI Application
# =============================================================================
app = FastAPI(
title="OpenKeywords API",
description="AI-powered SEO keyword generation using 5-stage pipeline",
version="2.0.0",
docs_url="/docs",
redoc_url="/redoc",
)
# =============================================================================
# Health Endpoints
# =============================================================================
@app.get("/", response_model=HealthResponse, tags=["Health"])
@app.get("/health", response_model=HealthResponse, tags=["Health"])
async def health_check():
"""Check API health status."""
return HealthResponse(
status="healthy",
version="2.0.0",
gemini_configured=bool(os.getenv("GEMINI_API_KEY")),
timestamp=datetime.now(timezone.utc).isoformat(),
)
# =============================================================================
# Job Endpoints
# =============================================================================
@app.post(
"/api/v1/jobs",
response_model=JobResponse,
status_code=201,
tags=["Jobs"],
summary="Create keyword generation job",
)
async def create_job(request: KeywordRequest, background_tasks: BackgroundTasks):
"""
Create a new keyword generation job.
The job runs asynchronously in the background. Poll GET /api/v1/jobs/{job_id}
to check status and retrieve results when completed.
"""
if not os.getenv("GEMINI_API_KEY"):
raise HTTPException(
status_code=500,
detail="GEMINI_API_KEY not configured. Set environment variable.",
)
job_id = str(uuid.uuid4())
job = job_store.create(job_id, request)
background_tasks.add_task(_run_generation_job, job_id, request)
return JobResponse(
job_id=job_id,
status=JobStatus.PENDING,
created_at=job["created_at"],
)
async def _run_generation_job(job_id: str, request: KeywordRequest):
"""Background task to run keyword generation."""
try:
job_store.update(job_id, status=JobStatus.RUNNING)
# Run the new pipeline
result = await run_pipeline(
company_url=str(request.company_url) if request.company_url else f"https://{request.company_name.lower().replace(' ', '')}.com",
company_name=request.company_name,
target_count=request.target_count,
language=request.language,
region=request.region,
enable_research=request.enable_research,
enable_clustering=True,
min_score=request.min_score,
cluster_count=request.cluster_count,
)
# Convert pipeline result to API response format
keywords = [
KeywordResult(
keyword=kw.get("keyword", ""),
intent=kw.get("intent", "informational"),
score=kw.get("score", 0),
cluster_name=kw.get("cluster_name"),
is_question=kw.get("is_question", False),
source=kw.get("source", "ai_generated"),
)
for kw in result.get("keywords", [])
]
clusters = [
ClusterResult(
name=c.get("name", ""),
keywords=c.get("keywords", []),
count=len(c.get("keywords", [])),
)
for c in result.get("clusters", [])
]
# Calculate statistics
intent_breakdown = {}
source_breakdown = {}
for kw in keywords:
intent_breakdown[kw.intent] = intent_breakdown.get(kw.intent, 0) + 1
source_breakdown[kw.source] = source_breakdown.get(kw.source, 0) + 1
response = GenerationResponse(
keywords=keywords,
clusters=clusters,
statistics=StatisticsResult(
total=len(keywords),
avg_score=result.get("statistics", {}).get("avg_score", 0),
intent_breakdown=intent_breakdown,
source_breakdown=source_breakdown,
),
processing_time_seconds=result.get("statistics", {}).get("duration_seconds", 0),
)
job_store.update(
job_id,
status=JobStatus.COMPLETED,
completed_at=datetime.now(timezone.utc).isoformat(),
result=response.model_dump(),
progress={"keywords_generated": len(keywords), "target_count": request.target_count},
)
except Exception as e:
job_store.update(
job_id,
status=JobStatus.FAILED,
completed_at=datetime.now(timezone.utc).isoformat(),
error=str(e),
)
@app.get(
"/api/v1/jobs",
response_model=List[JobResponse],
tags=["Jobs"],
summary="List all jobs",
)
async def list_jobs():
"""List all keyword generation jobs."""
jobs = job_store.list_all()
return [
JobResponse(
job_id=j["job_id"],
status=j["status"],
created_at=j["created_at"],
completed_at=j.get("completed_at"),
progress=j.get("progress"),
error=j.get("error"),
)
for j in jobs
]
@app.get(
"/api/v1/jobs/{job_id}",
response_model=JobResponse,
tags=["Jobs"],
summary="Get job status",
)
async def get_job(job_id: str = Path(..., description="Job UUID")):
"""Get the status and result of a keyword generation job."""
_validate_job_id(job_id)
job = job_store.get(job_id)
if not job:
raise HTTPException(status_code=404, detail=f"Job {job_id} not found")
return JobResponse(
job_id=job["job_id"],
status=job["status"],
created_at=job["created_at"],
completed_at=job.get("completed_at"),
progress=job.get("progress"),
result=job.get("result"),
error=job.get("error"),
)
@app.delete(
"/api/v1/jobs/{job_id}",
status_code=204,
tags=["Jobs"],
summary="Delete a job",
)
async def delete_job(job_id: str = Path(..., description="Job UUID")):
"""Delete a job and its results."""
_validate_job_id(job_id)
if not job_store.delete(job_id):
raise HTTPException(status_code=404, detail=f"Job {job_id} not found")
return None
# =============================================================================
# Export Endpoints
# =============================================================================
@app.get(
"/api/v1/jobs/{job_id}/export/json",
tags=["Export"],
summary="Export keywords as JSON",
)
async def export_json(job_id: str = Path(..., description="Job UUID")):
"""Export keywords from a completed job as JSON."""
_validate_job_id(job_id)
job = job_store.get(job_id)
if not job:
raise HTTPException(status_code=404, detail=f"Job {job_id} not found")
if job["status"] != JobStatus.COMPLETED:
raise HTTPException(status_code=400, detail="Job not completed")
return JSONResponse(
content=job["result"],
headers={"Content-Disposition": f"attachment; filename=keywords_{job_id}.json"},
)
@app.get(
"/api/v1/jobs/{job_id}/export/csv",
tags=["Export"],
summary="Export keywords as CSV",
)
async def export_csv(job_id: str = Path(..., description="Job UUID")):
"""Export keywords from a completed job as CSV."""
import csv
import io
_validate_job_id(job_id)
job = job_store.get(job_id)
if not job:
raise HTTPException(status_code=404, detail=f"Job {job_id} not found")
if job["status"] != JobStatus.COMPLETED:
raise HTTPException(status_code=400, detail="Job not completed")
output = io.StringIO()
writer = csv.writer(output)
# Header
headers = ["keyword", "intent", "score", "cluster_name", "is_question", "source"]
writer.writerow(headers)
# Data (sanitized to prevent CSV formula injection)
for kw in job["result"]["keywords"]:
writer.writerow([
_sanitize_csv_value(kw.get("keyword", "")),
_sanitize_csv_value(kw.get("intent", "")),
kw.get("score", 0),
_sanitize_csv_value(kw.get("cluster_name", "")),
kw.get("is_question", False),
_sanitize_csv_value(kw.get("source", "")),
])
csv_content = output.getvalue()
output.close()
return JSONResponse(
content={"csv": csv_content},
headers={"Content-Disposition": f"attachment; filename=keywords_{job_id}.csv"},
)
# =============================================================================
# Sync Generation Endpoint (for small requests)
# =============================================================================
@app.post(
"/api/v1/generate",
response_model=GenerationResponse,
tags=["Generate"],
summary="Generate keywords (sync)",
)
async def generate_sync(request: KeywordRequest):
"""
Generate keywords synchronously.
For small requests (≤100 keywords). Use /api/v1/jobs for larger requests.
"""
if not os.getenv("GEMINI_API_KEY"):
raise HTTPException(
status_code=500,
detail="GEMINI_API_KEY not configured. Set environment variable.",
)
if request.target_count > 100:
raise HTTPException(
status_code=400,
detail="Use /api/v1/jobs for requests > 100 keywords",
)
try:
result = await run_pipeline(
company_url=str(request.company_url) if request.company_url else f"https://{request.company_name.lower().replace(' ', '')}.com",
company_name=request.company_name,
target_count=request.target_count,
language=request.language,
region=request.region,
enable_research=request.enable_research,
enable_clustering=True,
min_score=request.min_score,
cluster_count=request.cluster_count,
)
keywords = [
KeywordResult(
keyword=kw.get("keyword", ""),
intent=kw.get("intent", "informational"),
score=kw.get("score", 0),
cluster_name=kw.get("cluster_name"),
is_question=kw.get("is_question", False),
source=kw.get("source", "ai_generated"),
)
for kw in result.get("keywords", [])
]
clusters = [
ClusterResult(
name=c.get("name", ""),
keywords=c.get("keywords", []),
count=len(c.get("keywords", [])),
)
for c in result.get("clusters", [])
]
intent_breakdown = {}
source_breakdown = {}
for kw in keywords:
intent_breakdown[kw.intent] = intent_breakdown.get(kw.intent, 0) + 1
source_breakdown[kw.source] = source_breakdown.get(kw.source, 0) + 1
return GenerationResponse(
keywords=keywords,
clusters=clusters,
statistics=StatisticsResult(
total=len(keywords),
avg_score=result.get("statistics", {}).get("avg_score", 0),
intent_breakdown=intent_breakdown,
source_breakdown=source_breakdown,
),
processing_time_seconds=result.get("statistics", {}).get("duration_seconds", 0),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# =============================================================================
# Main
# =============================================================================
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8001)