Update app.py
Browse filesFix runtime error - add uvicorn launch
app.py
CHANGED
|
@@ -14,6 +14,7 @@ from pydantic import BaseModel
|
|
| 14 |
from PIL import Image
|
| 15 |
import torch
|
| 16 |
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
|
|
|
|
| 17 |
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
|
@@ -55,6 +56,16 @@ def recognize_captcha(image, model_id):
|
|
| 55 |
generated_ids = model.generate(pixel_values, max_new_tokens=32)
|
| 56 |
return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
iface = gr.Interface(
|
| 60 |
fn=recognize_captcha,
|
|
@@ -64,19 +75,7 @@ iface = gr.Interface(
|
|
| 64 |
description="API available at /solve-captcha-base64"
|
| 65 |
)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
app = gr.mount_gradio_app(
|
| 69 |
-
FastAPI(title="CAPTCHA Solver API"),
|
| 70 |
-
iface,
|
| 71 |
-
path="/"
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
app.add_middleware(
|
| 75 |
-
CORSMiddleware,
|
| 76 |
-
allow_origins=["*"],
|
| 77 |
-
allow_methods=["*"],
|
| 78 |
-
allow_headers=["*"],
|
| 79 |
-
)
|
| 80 |
|
| 81 |
# ββ Schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 82 |
class SolveRequest(BaseModel):
|
|
@@ -113,3 +112,7 @@ def solve(req: SolveRequest, _: str = Depends(verify_key)):
|
|
| 113 |
except Exception as e:
|
| 114 |
logger.error(f"Error: {e}")
|
| 115 |
return SolveResponse(success=False, error=str(e), processing_time=time.time() - start)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
from PIL import Image
|
| 15 |
import torch
|
| 16 |
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
|
| 17 |
+
import uvicorn
|
| 18 |
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
logger = logging.getLogger(__name__)
|
|
|
|
| 56 |
generated_ids = model.generate(pixel_values, max_new_tokens=32)
|
| 57 |
return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 58 |
|
| 59 |
+
# ββ FastAPI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 60 |
+
app = FastAPI(title="CAPTCHA Solver API")
|
| 61 |
+
|
| 62 |
+
app.add_middleware(
|
| 63 |
+
CORSMiddleware,
|
| 64 |
+
allow_origins=["*"],
|
| 65 |
+
allow_methods=["*"],
|
| 66 |
+
allow_headers=["*"],
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=recognize_captcha,
|
|
|
|
| 75 |
description="API available at /solve-captcha-base64"
|
| 76 |
)
|
| 77 |
|
| 78 |
+
app = gr.mount_gradio_app(app, iface, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# ββ Schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 81 |
class SolveRequest(BaseModel):
|
|
|
|
| 112 |
except Exception as e:
|
| 113 |
logger.error(f"Error: {e}")
|
| 114 |
return SolveResponse(success=False, error=str(e), processing_time=time.time() - start)
|
| 115 |
+
|
| 116 |
+
# ββ Launch ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|