Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 2 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 3 |
+
import shutil
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
from io import StringIO
|
| 7 |
+
import contextlib
|
| 8 |
+
|
| 9 |
+
# ඔයාගේ original ෆයිල් දෙක මෙතනට import කරනවා
|
| 10 |
+
import math_solver
|
| 11 |
+
import scan # අපි ඔයාගේ app.py එක scan.py කියලා rename කළානේ
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
@app.get("/")
|
| 16 |
+
def home():
|
| 17 |
+
return {"message": "Cipher-MD Python Engine is Running! 🚀"}
|
| 18 |
+
|
| 19 |
+
# =================================================================
|
| 20 |
+
# 1️⃣ MATH SOLVER ENDPOINT
|
| 21 |
+
# =================================================================
|
| 22 |
+
@app.post("/math")
|
| 23 |
+
async def solve_math(image: UploadFile = File(...), prompt: str = Form("")):
|
| 24 |
+
temp_filename = f"temp_math_{image.filename}"
|
| 25 |
+
|
| 26 |
+
# 1. Image එක Save කරගන්නවා
|
| 27 |
+
with open(temp_filename, "wb") as buffer:
|
| 28 |
+
shutil.copyfileobj(image.file, buffer)
|
| 29 |
+
|
| 30 |
+
# 2. ඔයාගේ math_solver එකේ Output එක (Print වෙන ඒවා) අල්ලාගන්නවා
|
| 31 |
+
output_capture = StringIO()
|
| 32 |
+
try:
|
| 33 |
+
# Stdout එක capture කරනවා (Console එකට print වෙන දේ variable එකකට ගන්නවා)
|
| 34 |
+
with contextlib.redirect_stdout(output_capture):
|
| 35 |
+
# ඔයාගේ original function එක call කරනවා
|
| 36 |
+
math_solver.solve_math_problem(temp_filename, prompt)
|
| 37 |
+
|
| 38 |
+
result = output_capture.getvalue()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
result = f"Error: {str(e)}"
|
| 41 |
+
finally:
|
| 42 |
+
# කුණු ෆයිල් අස් කරනවා
|
| 43 |
+
if os.path.exists(temp_filename):
|
| 44 |
+
os.remove(temp_filename)
|
| 45 |
+
|
| 46 |
+
# 3. JSON විදිහට උත්තරේ යවනවා
|
| 47 |
+
return {"result": result}
|
| 48 |
+
|
| 49 |
+
# =================================================================
|
| 50 |
+
# 2️⃣ SCAN / REVEAL ENDPOINT
|
| 51 |
+
# =================================================================
|
| 52 |
+
@app.post("/scan")
|
| 53 |
+
async def scan_watermark(image: UploadFile = File(...)):
|
| 54 |
+
in_file = f"in_{image.filename}"
|
| 55 |
+
out_file = f"out_{image.filename}"
|
| 56 |
+
|
| 57 |
+
# 1. Image එක Save කරගන්නවා
|
| 58 |
+
with open(in_file, "wb") as buffer:
|
| 59 |
+
shutil.copyfileobj(image.file, buffer)
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
# 2. ඔයාගේ scan script (reveal_watermark) එක run කරනවා
|
| 63 |
+
scan.reveal_watermark(in_file, out_file)
|
| 64 |
+
|
| 65 |
+
# 3. හැදුනු ෆොටෝ එක ආපහු යවනවා
|
| 66 |
+
if os.path.exists(out_file):
|
| 67 |
+
return FileResponse(out_file)
|
| 68 |
+
else:
|
| 69 |
+
return {"error": "Processing failed"}
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return {"error": str(e)}
|
| 73 |
+
|
| 74 |
+
finally:
|
| 75 |
+
# Input ෆයිල් එක විතරක් මකනවා (Output එක යැව්වට පස්සේ මැකෙන විදිහට HuggingFace එකේ auto clean වෙනවා)
|
| 76 |
+
if os.path.exists(in_file):
|
| 77 |
+
os.remove(in_file)
|
| 78 |
+
|
| 79 |
+
# Docker එකේ Run වෙන්න ඕන නිසා
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
import uvicorn
|
| 82 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|