File size: 2,029 Bytes
b25b8f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from pydantic import BaseModel
from typing import Optional, Dict
import uvicorn

app = FastAPI(title="Input Receiver Service", description="Accepts and routes math problems in text or image format.")

class TextInputRequest(BaseModel):
    text: str
    metadata: Optional[Dict] = {}

@app.get("/health")
async def health_check():
    """Returns the health status of the Input Receiver service."""
    return {"status": "healthy", "service": "input-receiver"}

@app.post("/receive/text")
async def receive_text(request: TextInputRequest):
    """

    Accepts a math problem in text format and returns it in a standardized JSON payload.

    This payload will subsequently be sent to the Preprocessing Service.

    """
    if not request.text.strip():
        raise HTTPException(status_code=400, detail="Text cannot be empty.")
    
    return {
        "status": "success",
        "input_type": "text",
        "data": request.text,
        "metadata": request.metadata
    }

@app.post("/receive/image")
async def receive_image(

    file: UploadFile = File(...),

    metadata: Optional[str] = Form("{}")

):
    """

    Accepts a math problem as an image upload and returns a confirmation JSON payload.

    The image bytes will subsequently be sent to the OCR Service.

    """
    if file.content_type not in ["image/jpeg", "image/png"]:
        raise HTTPException(status_code=400, detail="Invalid image format. Use JPEG or PNG.")
    
    # In a real microservice, we might save this to an S3 bucket or shared volume and pass the URL/path.
    # For MVP, we return a success format. The actual forwarding will be handled by the API Gateway.
    return {
        "status": "success",
        "input_type": "image",
        "filename": file.filename,
        "content_type": file.content_type,
        "metadata": metadata
    }

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)