-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptiongem_api.py
More file actions
32 lines (27 loc) · 1.1 KB
/
captiongem_api.py
File metadata and controls
32 lines (27 loc) · 1.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
from fastapi import FastAPI, HTTPException
from captiongem import generate_caption, generate_hashtags
# from mangum import Mangum
app = FastAPI()
# handler = Mangum(app) #Handler function that Lambda will envoke that will reroute input to each get we wrote
MAX_INPUT_LENGTH = 32
@app.get("/generate_caption")
async def generate_caption_api(prompt: str):
caption = generate_caption(prompt)
return {"caption": caption}
@app.get("/generate_hashtags")
async def generate_hashtags_api(prompt: str):
hashtags = generate_hashtags(prompt)
return {"hashtags": hashtags}
@app.get("/generate_caption_and_hashtags")
async def generate_hashtags_api(prompt: str):
validate_input_length(prompt)
caption = generate_caption(prompt)
hashtags = generate_hashtags(prompt)
return {"caption": caption, "hashtags": hashtags}
def validate_input_length(prompt: str):
if len(prompt) >= MAX_INPUT_LENGTH:
raise HTTPException(
status_code=400,
detail=f"Input length is too long. Must be under {MAX_INPUT_LENGTH} characters.",
)
#python3 -m uvicorn captiongem_api:app --reload