Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from fastapi import FastAPI, Request
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
# Load model
|
| 9 |
+
model_id = "ST-THOMAS-OF-AQUINAS/SCAM"
|
| 10 |
+
pipe = pipeline("text-classification", model=model_id)
|
| 11 |
+
|
| 12 |
+
# Label map (adjust to your model)
|
| 13 |
+
label_map = {0: "author1", 1: "author2"}
|
| 14 |
+
|
| 15 |
+
def predict(text: str):
|
| 16 |
+
if not text.strip():
|
| 17 |
+
return "⚠️ No input text"
|
| 18 |
+
|
| 19 |
+
results = pipe(text)
|
| 20 |
+
label = results[0]["label"]
|
| 21 |
+
score = results[0]["score"]
|
| 22 |
+
|
| 23 |
+
if label.startswith("LABEL_"):
|
| 24 |
+
idx = int(label.split("_")[1])
|
| 25 |
+
label = label_map.get(idx, label)
|
| 26 |
+
|
| 27 |
+
return f"Prediction: {label}\nConfidence: {round(score * 100, 2)}%"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# --- Gradio Interface ---
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=predict,
|
| 33 |
+
inputs=gr.Textbox(label="Enter WhatsApp Message"),
|
| 34 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 35 |
+
title="📲 WhatsApp Scam Detector",
|
| 36 |
+
description="Paste a WhatsApp message and the model will predict its author."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# --- FastAPI App (for Twilio Webhook) ---
|
| 40 |
+
app = FastAPI()
|
| 41 |
+
|
| 42 |
+
class Message(BaseModel):
|
| 43 |
+
Body: str
|
| 44 |
+
|
| 45 |
+
@app.post("/predict")
|
| 46 |
+
async def predict_api(msg: Message):
|
| 47 |
+
return {"reply": predict(msg.Body)}
|
| 48 |
+
|
| 49 |
+
# Mount Gradio inside FastAPI
|
| 50 |
+
app = gr.mount_gradio_app(app, iface, path="/")
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|