Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import requests
|
| 5 |
+
import os # for reading environment variables
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Enable CORS
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"], # Allow all origins, or replace with your frontend URL
|
| 13 |
+
allow_credentials=True,
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Read OpenRouter API key from environment variable (HF Secret)
|
| 19 |
+
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
| 20 |
+
if not OPENROUTER_API_KEY:
|
| 21 |
+
raise RuntimeError("OPENROUTER_API_KEY environment variable not set!")
|
| 22 |
+
|
| 23 |
+
# Request schema
|
| 24 |
+
class ExplainRequest(BaseModel):
|
| 25 |
+
message: str
|
| 26 |
+
label: str # "Phishing" or "Safe"
|
| 27 |
+
|
| 28 |
+
@app.post("/explain")
|
| 29 |
+
def explain(req: ExplainRequest):
|
| 30 |
+
user_message = req.message.strip()
|
| 31 |
+
label = req.label.strip()
|
| 32 |
+
|
| 33 |
+
if not user_message or not label:
|
| 34 |
+
raise HTTPException(status_code=400, detail="Missing message or label")
|
| 35 |
+
|
| 36 |
+
# Updated system prompt with bullet-point format
|
| 37 |
+
system_prompt = (
|
| 38 |
+
f"You are a robot that identifies phishing and safe messages. "
|
| 39 |
+
f"The message was classified as '{label}'. "
|
| 40 |
+
"Explain why this decision was made and point out any words or patterns that led to it. "
|
| 41 |
+
"Limit your answer to 2–3 sentences. No greetings, introductions, or closing remarks. "
|
| 42 |
+
"Don't restate the message or its classification. Output only the explanation as bullet points. "
|
| 43 |
+
f"Message:\n\n{user_message}"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 47 |
+
headers = {
|
| 48 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 49 |
+
"Content-Type": "application/json"
|
| 50 |
+
}
|
| 51 |
+
payload = {
|
| 52 |
+
"model": "x-ai/grok-4-fast:free",
|
| 53 |
+
"messages": [
|
| 54 |
+
{"role": "system", "content": system_prompt},
|
| 55 |
+
{"role": "user", "content": user_message}
|
| 56 |
+
]
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
response = requests.post(url, headers=headers, json=payload, timeout=20)
|
| 61 |
+
response.raise_for_status()
|
| 62 |
+
result = response.json()
|
| 63 |
+
reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
|
| 64 |
+
if not reply:
|
| 65 |
+
reply = "[No explanation returned]"
|
| 66 |
+
return {"reply": reply}
|
| 67 |
+
except requests.RequestException as e:
|
| 68 |
+
raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")
|