Spaces:
Sleeping
Sleeping
Create src/app.py
Browse files- src/app.py +34 -0
src/app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
|
| 8 |
+
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed")
|
| 9 |
+
|
| 10 |
+
GAMBLING_KEYWORDS = ["WIN", "BET", "JACKPOT", "CASINO", "SLOT", "999"]
|
| 11 |
+
|
| 12 |
+
def detect_text(image: Image.Image) -> str:
|
| 13 |
+
"""Run OCR on the image and return extracted text."""
|
| 14 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 15 |
+
generated_ids = model.generate(pixel_values, max_length=64)
|
| 16 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 17 |
+
return text.upper()
|
| 18 |
+
|
| 19 |
+
def check_gambling(image):
|
| 20 |
+
"""Main Gradio function: return text and gambling status."""
|
| 21 |
+
text = detect_text(image)
|
| 22 |
+
gambling = any(re.search(rf"\b{kw}\b", text) for kw in GAMBLING_KEYWORDS)
|
| 23 |
+
return f"📝 Detected Text: {text}\n🎰 Gambling Detected: {gambling}"
|
| 24 |
+
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=check_gambling,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="🎰 Gambling Text Detector",
|
| 30 |
+
description="Upload an image (e.g., casino banner, slot machine screenshot). The app will detect gambling-related text like WIN, JACKPOT, BET, 999, etc."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|