Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Form
|
| 2 |
+
from fastapi.responses import Response
|
| 3 |
+
from twilio.rest import Client
|
| 4 |
+
from twilio.twiml.messaging_response import MessagingResponse
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="WhatsApp → Public HuggingFace → Forward Bot")
|
| 9 |
+
|
| 10 |
+
# -------------------------------
|
| 11 |
+
# Hugging Face public model URL
|
| 12 |
+
# -------------------------------
|
| 13 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/ST-THOMAS-OF-AQUINAS/SCAM"
|
| 14 |
+
|
| 15 |
+
# Twilio credentials (set in Space Secrets)
|
| 16 |
+
TWILIO_SID = os.environ.get("TWILIO_SID")
|
| 17 |
+
TWILIO_AUTH = os.environ.get("TWILIO_AUTH")
|
| 18 |
+
TWILIO_WHATSAPP = os.environ.get("TWILIO_WHATSAPP") # e.g., whatsapp:+14155238886
|
| 19 |
+
MY_WHATSAPP = os.environ.get("MY_WHATSAPP") # Your personal WhatsApp
|
| 20 |
+
|
| 21 |
+
twilio_client = Client(TWILIO_SID, TWILIO_AUTH)
|
| 22 |
+
|
| 23 |
+
# -------------------------------
|
| 24 |
+
# Incoming message webhook
|
| 25 |
+
# -------------------------------
|
| 26 |
+
@app.post("/whatsapp")
|
| 27 |
+
async def whatsapp_forward(From: str = Form(...), Body: str = Form(...)):
|
| 28 |
+
if not Body.strip():
|
| 29 |
+
return Response(content="", media_type="application/xml")
|
| 30 |
+
|
| 31 |
+
# 1️⃣ Send message to Hugging Face model
|
| 32 |
+
try:
|
| 33 |
+
payload = {"inputs": Body}
|
| 34 |
+
hf_response = requests.post(HF_API_URL, json=payload, timeout=30)
|
| 35 |
+
hf_response.raise_for_status()
|
| 36 |
+
result = hf_response.json()
|
| 37 |
+
|
| 38 |
+
prediction = result[0]["label"]
|
| 39 |
+
confidence = result[0]["score"] * 100
|
| 40 |
+
|
| 41 |
+
# 2️⃣ Build the forward message
|
| 42 |
+
forward_text = (
|
| 43 |
+
f"📩 New message from {From}\n\n"
|
| 44 |
+
f"Message: {Body}\n"
|
| 45 |
+
f"Prediction: {prediction}\n"
|
| 46 |
+
f"Confidence: {confidence:.2f}%"
|
| 47 |
+
)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
forward_text = f"❌ Error calling model: {str(e)}"
|
| 50 |
+
|
| 51 |
+
# 3️⃣ Send the message to YOUR WhatsApp
|
| 52 |
+
twilio_client.messages.create(
|
| 53 |
+
from_=TWILIO_WHATSAPP,
|
| 54 |
+
to=MY_WHATSAPP,
|
| 55 |
+
body=forward_text
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# 4️⃣ Reply to sender optionally
|
| 59 |
+
resp = MessagingResponse()
|
| 60 |
+
resp.message("✅ Message received and processed.")
|
| 61 |
+
return Response(content=str(resp), media_type="application/xml")
|
| 62 |
+
|
| 63 |
+
# -------------------------------
|
| 64 |
+
# Health check
|
| 65 |
+
# -------------------------------
|
| 66 |
+
@app.get("/")
|
| 67 |
+
async def health_check():
|
| 68 |
+
return {"status": "✅ API is running"}
|