File size: 3,010 Bytes
a47415e 5888c6a 1bc09e1 5888c6a 8e34b26 5888c6a a47415e 5888c6a a47415e 5888c6a a47415e 5888c6a a47415e 5888c6a a47415e 5888c6a a47415e 5888c6a a47415e 5888c6a 8e34b26 1d1a783 a47415e 5888c6a 1d1a783 3dba58b 1d1a783 a47415e 1bc09e1 a47415e |
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 |
from fastapi import FastAPI, Request, UploadFile, File, Form
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import os
# New β
(safe temp directory on Hugging Face)
UPLOAD_FOLDER = "/tmp/uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True) # β Move this AFTER defining UPLOAD_FOLDER
from utils.image_processor import extract_text_from_image
from utils.xlnet_model import get_model_prediction
from utils.xlnet_model import get_similarity_score
from werkzeug.utils import secure_filename
import shutil
app = FastAPI()
# Static & Templates
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.get("/", response_class=HTMLResponse)
def serve_home(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/evaluate")
async def evaluate(
request: Request,
question: UploadFile = File(...),
student_answer: UploadFile = File(...),
reference_answer: UploadFile = File(...)
):
try:
files = {"question": question, "student": student_answer, "reference": reference_answer}
paths = {}
for key, file in files.items():
if not allowed_file(file.filename):
return {"error": f"Invalid file type: {file.filename}"}
filename = secure_filename(file.filename)
file_path = os.path.join(UPLOAD_FOLDER, filename)
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
paths[key] = file_path
question_text = extract_text_from_image(paths["question"])
student_text = extract_text_from_image(paths["student"])
reference_text = extract_text_from_image(paths["reference"])
score = get_model_prediction(question_text, student_text, reference_text)
# π― Bonus adjustment
if score >= 75:
score += 20
elif 70 <= score < 75:
score += 18
elif 60 <= score < 65:
score += 16
else:
score -= 10
score = max(0, min(score, 100))
# β
Print extracted values for debugging
print("π Question:", question_text)
print("π§ Student Answer:", student_text)
print("π Reference Answer:", reference_text)
print("π― Raw Score:", score)
return {
"success": True,
"score": score,
"question_text": question_text,
"student_answer_text": student_text,
"reference_answer_text": reference_text
}
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|