File size: 2,779 Bytes
32519eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel
from typing import List, Optional
import sys
import os
import logging

# Add src directory to Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from generation.medical_generator import MedicalTextGenerator

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="Synthex Medical Text Generator API",
    description="API for generating synthetic medical records",
    version="1.0.0"
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allows all origins
    allow_credentials=True,
    allow_methods=["*"],  # Allows all methods
    allow_headers=["*"],  # Allows all headers
)

# Mount static files
app.mount("/static", StaticFiles(directory="src/web"), name="static")

# Initialize generator
generator = MedicalTextGenerator()

class GenerationRequest(BaseModel):
    record_type: str
    quantity: int = 1
    use_gemini: bool = False
    include_metadata: bool = True

class MedicalRecord(BaseModel):
    id: str
    type: str
    text: str
    timestamp: str
    source: str

class GenerationResponse(BaseModel):
    records: List[MedicalRecord]
    total_generated: int

@app.get("/")
async def read_root():
    """Serve the HTML interface"""
    return FileResponse("src/web/index.html")

@app.get("/record-types")
async def get_record_types():
    """Get available record types"""
    return {"record_types": list(generator.templates.keys())}

@app.post("/generate", response_model=GenerationResponse)
async def generate_records(request: GenerationRequest):
    """Generate synthetic medical records"""
    try:
        if request.record_type not in generator.templates:
            raise HTTPException(status_code=400, detail=f"Invalid record type. Available types: {list(generator.templates.keys())}")
        
        if request.quantity < 1 or request.quantity > 10:
            raise HTTPException(status_code=400, detail="Quantity must be between 1 and 10")
        
        records = generator.batch_generate(
            record_type=request.record_type,
            count=request.quantity,
            use_gemini=request.use_gemini
        )
        
        return {
            "records": records,
            "total_generated": len(records)
        }
        
    except Exception as e:
        logger.error(f"Error generating records: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

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