-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (55 loc) · 1.94 KB
/
Copy pathmain.py
File metadata and controls
78 lines (55 loc) · 1.94 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
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from dotenv import load_dotenv
from pydantic import BaseModel
from ai import summarise
from payments import (
get_credits,
deduct_credit,
initialize_payment,
verify,
)
load_dotenv()
app = FastAPI(title="VoiceToText")
app.mount("/static", StaticFiles(directory="static"), name="static")
HTML = Path("templates/index.html").read_text()
@app.get("/", response_class=HTMLResponse)
async def index():
return HTMLResponse(content=HTML)
@app.get("/credits")
async def credits(email: str):
if not email:
raise HTTPException(status_code=400, detail="Email is required.")
return {"email": email, "credits": get_credits(email)}
class SummariseRequest(BaseModel):
email: str
transcript: str
@app.post("/summarise")
async def summarise_transcript(body: SummariseRequest):
if not body.transcript.strip():
raise HTTPException(status_code=400, detail="Transcript is empty.")
remaining = get_credits(body.email)
if remaining <= 0:
raise HTTPException(status_code=402, detail="No credits remaining. Please top up.")
summary = summarise(body.transcript)
deduct_credit(body.email)
return {"summary": summary}
class PaymentInitRequest(BaseModel):
email: str
@app.post("/payment/initialize")
async def payment_initialize(body: PaymentInitRequest):
try:
data = initialize_payment(body.email)
return {"authorization_url": data["authorization_url"], "reference": data["reference"]}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
class PaymentVerifyRequest(BaseModel):
email: str
reference: str
@app.post("/payment/verify")
async def payment_verify(body: PaymentVerifyRequest):
success, message = verify(body.email, body.reference)
credits = get_credits(body.email)
return {"success": success, "message": message, "credits": credits}