File size: 820 Bytes
74a4a8f
dff2458
 
 
 
 
74a4a8f
 
 
 
 
b78a42b
dff2458
 
 
 
 
 
 
 
b78a42b
dff2458
b78a42b
 
dff2458
 
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
import os
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.responses import JSONResponse
from transformers import pipeline

# βœ… Set writable cache location
os.environ["TRANSFORMERS_CACHE"] = "/data/cache"
os.makedirs("/data/cache", exist_ok=True)

# βœ… Load model safely
pipe = pipeline("text2text-generation", model="google/flan-t5-small", max_new_tokens=100)

app = FastAPI()

class LaTeXRequest(BaseModel):
    latex: str

@app.post("/fix")
async def fix_latex(data: LaTeXRequest):
    prompt = f"Fix this malformed LaTeX expression so it's valid:\n{data.latex}"
    try:
        result = pipe(prompt)[0]["generated_text"]
        return JSONResponse(content={"fixed_latex": result.strip()})
    except Exception as e:
        return JSONResponse(content={"error": str(e)}, status_code=500)