-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
387 lines (327 loc) · 12.3 KB
/
app.py
File metadata and controls
387 lines (327 loc) · 12.3 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
"""
FastAPI Text Intelligence Starter - Backend Server
This is a simple FastAPI server that provides a text intelligence API endpoint
powered by Deepgram's Text Intelligence service. It's designed to be easily
modified and extended for your own projects.
Key Features:
- Contract-compliant API endpoint: POST /api/text-intelligence
- Accepts text or URL in JSON body
- Supports multiple intelligence features: summarization, topics, sentiment, intents
- JWT session auth for API protection
- Async/await for better performance
- Automatic OpenAPI docs at /docs
"""
import os
import secrets
import time
from typing import Optional
import jwt
from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from deepgram import DeepgramClient
from dotenv import load_dotenv
import toml
# Load .env without overriding existing env vars
load_dotenv(override=False)
CONFIG = {
"port": int(os.environ.get("PORT", 8081)),
"host": os.environ.get("HOST", "0.0.0.0"),
}
# ============================================================================
# SESSION AUTH - JWT tokens for API protection
# ============================================================================
SESSION_SECRET = os.environ.get("SESSION_SECRET") or secrets.token_hex(32)
JWT_EXPIRY = 3600 # 1 hour
# Read frontend/dist/index.html for serving
_index_html_template = None
try:
with open(os.path.join(os.path.dirname(__file__), "frontend", "dist", "index.html")) as f:
_index_html_template = f.read()
except FileNotFoundError:
pass # No built frontend (dev mode)
def require_session(authorization: str = Header(None)):
"""FastAPI dependency for JWT session validation."""
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(
status_code=401,
detail={
"error": {
"type": "AuthenticationError",
"code": "MISSING_TOKEN",
"message": "Authorization header with Bearer token is required",
}
}
)
token = authorization[7:]
try:
jwt.decode(token, SESSION_SECRET, algorithms=["HS256"])
except jwt.ExpiredSignatureError:
raise HTTPException(
status_code=401,
detail={
"error": {
"type": "AuthenticationError",
"code": "INVALID_TOKEN",
"message": "Session expired, please refresh the page",
}
}
)
except jwt.InvalidTokenError:
raise HTTPException(
status_code=401,
detail={
"error": {
"type": "AuthenticationError",
"code": "INVALID_TOKEN",
"message": "Invalid session token",
}
}
)
# ============================================================================
# API KEY LOADING
# ============================================================================
def load_api_key():
"""Loads the Deepgram API key from environment variables"""
api_key = os.environ.get("DEEPGRAM_API_KEY")
if not api_key:
print("\n❌ ERROR: Deepgram API key not found!\n")
print("Please set your API key using one of these methods:\n")
print("1. Create a .env file (recommended):")
print(" DEEPGRAM_API_KEY=your_api_key_here\n")
print("2. Environment variable:")
print(" export DEEPGRAM_API_KEY=your_api_key_here\n")
print("Get your API key at: https://console.deepgram.com\n")
raise ValueError("DEEPGRAM_API_KEY environment variable is required")
return api_key
api_key = load_api_key()
# ============================================================================
# SETUP
# ============================================================================
deepgram = DeepgramClient(api_key=api_key)
app = FastAPI(
title="Deepgram Text Intelligence API",
description="Text analysis powered by Deepgram",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ============================================================================
# PYDANTIC MODELS
# ============================================================================
class TextInput(BaseModel):
text: Optional[str] = None
url: Optional[str] = None
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
def validate_text_input(body: TextInput):
"""Validates that JSON body has exactly one of text or url"""
if not body.text and not body.url:
return None, "Request must contain either 'text' or 'url' field"
if body.text and body.url:
return None, "Request must contain either 'text' or 'url', not both"
if body.url:
if not body.url.startswith(('http://', 'https://')):
return None, "Invalid URL format"
return {"url": body.url}, None
else:
if not body.text.strip():
return None, "Text content cannot be empty"
return {"text": body.text}, None
def build_deepgram_options(
language: str = "en",
summarize: Optional[str] = None,
topics: Optional[str] = None,
sentiment: Optional[str] = None,
intents: Optional[str] = None
):
"""Converts query parameters to SDK keyword arguments"""
options = {"language": language}
if summarize == "true":
options["summarize"] = True
elif summarize == "v2":
options["summarize"] = "v2"
elif summarize == "v1":
return None, "Summarization v1 is no longer supported. Please use v2 or true."
if topics == "true":
options["topics"] = True
if sentiment == "true":
options["sentiment"] = True
if intents == "true":
options["intents"] = True
return options, None
# ============================================================================
# SESSION ROUTES - Auth endpoints (unprotected)
# ============================================================================
@app.get("/", response_class=HTMLResponse)
async def serve_index():
"""Serve index.html."""
if not _index_html_template:
raise HTTPException(status_code=404, detail="Frontend not built. Run make build first.")
return HTMLResponse(content=_index_html_template)
@app.get("/api/session")
async def get_session():
"""Issues a JWT session token."""
token = jwt.encode(
{"iat": int(time.time()), "exp": int(time.time()) + JWT_EXPIRY},
SESSION_SECRET,
algorithm="HS256",
)
return JSONResponse(content={"token": token})
# ============================================================================
# API ROUTES
# ============================================================================
@app.post("/api/text-intelligence")
async def analyze(
body: TextInput,
language: str = "en",
summarize: Optional[str] = None,
topics: Optional[str] = None,
sentiment: Optional[str] = None,
intents: Optional[str] = None,
_auth=Depends(require_session)
):
"""
POST /api/text-intelligence
Contract-compliant text intelligence endpoint.
Accepts JSON body with either text or url field.
Query parameters: summarize, topics, sentiment, intents, language
"""
try:
# Validate text input
request_dict, error_msg = validate_text_input(body)
if error_msg:
error_code = "INVALID_TEXT" if "text" in error_msg.lower() else "INVALID_URL"
response = JSONResponse(
status_code=400,
content={
"error": {
"type": "validation_error",
"code": error_code,
"message": error_msg,
"details": {}
}
}
)
return response
# Build Deepgram options
options, error_msg = build_deepgram_options(
language, summarize, topics, sentiment, intents
)
if error_msg:
response = JSONResponse(
status_code=400,
content={
"error": {
"type": "validation_error",
"code": "INVALID_TEXT",
"message": error_msg,
"details": {}
}
}
)
return response
# Call Deepgram API
response_data = deepgram.read.v1.text.analyze(
request=request_dict,
**options
)
# Convert Pydantic model to dict
if hasattr(response_data, 'to_dict'):
result = {"results": response_data.results.to_dict() if hasattr(response_data.results, 'to_dict') else {}}
elif hasattr(response_data, 'model_dump'):
result_data = response_data.model_dump()
result = {"results": result_data.get('results', {})}
else:
result = {"results": dict(response_data.results) if hasattr(response_data, 'results') else {}}
return JSONResponse(status_code=200, content=result)
except Exception as e:
print(f"Text Intelligence Error: {e}")
error_code = "INVALID_TEXT"
error_message = str(e)
status_code = 500
if "text" in str(e).lower():
error_code = "INVALID_TEXT"
status_code = 400
elif "url" in str(e).lower():
error_code = "INVALID_URL"
status_code = 400
elif "too long" in str(e).lower():
error_code = "TEXT_TOO_LONG"
status_code = 400
return JSONResponse(
status_code=status_code,
content={
"error": {
"type": "processing_error",
"code": error_code,
"message": error_message if status_code == 400 else "Text processing failed",
"details": {}
}
}
)
@app.get("/health")
async def health():
"""Health check endpoint"""
return {"status": "ok", "service": "text-intelligence"}
@app.get("/api/metadata")
async def get_metadata():
"""
GET /api/metadata
Returns metadata about this starter application from deepgram.toml
"""
try:
with open('deepgram.toml', 'r') as f:
config = toml.load(f)
if 'meta' not in config:
raise HTTPException(
status_code=500,
detail={
'error': 'INTERNAL_SERVER_ERROR',
'message': 'Missing [meta] section in deepgram.toml'
}
)
return JSONResponse(content=config['meta'], status_code=200)
except FileNotFoundError:
raise HTTPException(
status_code=500,
detail={
'error': 'INTERNAL_SERVER_ERROR',
'message': 'deepgram.toml file not found'
}
)
except Exception as e:
print(f"Error reading metadata: {e}")
raise HTTPException(
status_code=500,
detail={
'error': 'INTERNAL_SERVER_ERROR',
'message': f'Failed to read metadata from deepgram.toml: {str(e)}'
}
)
# ============================================================================
# FRONTEND SERVING
# ============================================================================
# ============================================================================
# SERVER START
# ============================================================================
if __name__ == "__main__":
import uvicorn
print("\n" + "=" * 70)
print(f"🚀 FastAPI Text Intelligence Server running at http://localhost:{CONFIG['port']}")
print("=" * 70)
print("\nAvailable routes:")
print(f" GET /api/session")
print(f" POST /api/text-intelligence (auth required)")
print(f" GET /api/metadata")
print(f" GET /health")
print(f" GET /docs (OpenAPI documentation)")
print("=" * 70 + "\n")
uvicorn.run(app, host=CONFIG["host"], port=CONFIG["port"])