completely forgot about gradio
Browse files
app.py
CHANGED
|
@@ -1,65 +1,60 @@
|
|
| 1 |
-
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
|
| 6 |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 7 |
from api.predict import predict_review, models_loaded
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"model": "ensemble-v1",
|
| 17 |
-
"models_loaded": models_loaded
|
| 18 |
-
}), 200
|
| 19 |
-
|
| 20 |
-
@app.route('/predict', methods=['POST'])
|
| 21 |
-
def predict():
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
if not data or 'text' not in data:
|
| 26 |
-
return jsonify({"error": "missing 'text' field"}), 400
|
| 27 |
-
|
| 28 |
-
reviewText = data['text']
|
| 29 |
-
|
| 30 |
-
if not isinstance(reviewText, str):
|
| 31 |
-
return jsonify({"error": "'text' must be a string"}), 400
|
| 32 |
|
| 33 |
-
if
|
| 34 |
-
return
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"message": "models are loading for the first time, this will take 20-30 minutes. please wait...",
|
| 40 |
-
"models_loaded": False
|
| 41 |
-
}), 202
|
| 42 |
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
return
|
| 49 |
-
"prediction": result['prediction'],
|
| 50 |
-
"confidence": result['confidence'],
|
| 51 |
-
"is_fake": result['is_fake'],
|
| 52 |
-
"model_agreement": result['model_agreement'],
|
| 53 |
-
"fake_probability": result['fake_probability'],
|
| 54 |
-
"genuine_probability": result['genuine_probability'],
|
| 55 |
-
"length_category": result['length_category'],
|
| 56 |
-
"token_count": result['token_count']
|
| 57 |
-
}), 200
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
if __name__ ==
|
| 63 |
-
print("starting
|
| 64 |
print("models will load on first prediction request", flush=True)
|
| 65 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
|
| 5 |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 6 |
from api.predict import predict_review, models_loaded
|
| 7 |
|
| 8 |
+
def analyze_review(text):
|
| 9 |
+
if not text or len(text.strip()) == 0:
|
| 10 |
+
return "error: please enter some text"
|
| 11 |
+
|
| 12 |
+
if not models_loaded:
|
| 13 |
+
return "models are loading for the first time, this will take 20-30 minutes. please wait..."
|
| 14 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
+
result = predict_review(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
if "error" in result and result["prediction"] == "error":
|
| 19 |
+
return f"error: {result['error']}"
|
| 20 |
|
| 21 |
+
prediction = result['prediction']
|
| 22 |
+
confidence = result['confidence']
|
| 23 |
+
is_fake = result['is_fake']
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
status = "FAKE" if is_fake else "GENUINE"
|
| 26 |
|
| 27 |
+
output = f"""prediction: {status}
|
| 28 |
+
confidence: {confidence:.2%}
|
| 29 |
+
|
| 30 |
+
fake probability: {result['fake_probability']:.2%}
|
| 31 |
+
genuine probability: {result['genuine_probability']:.2%}
|
| 32 |
+
|
| 33 |
+
model agreement: {result['model_agreement']:.1f}%
|
| 34 |
+
length category: {result['length_category']}
|
| 35 |
+
token count: {result['token_count']}"""
|
| 36 |
|
| 37 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"error: {str(e)}"
|
| 41 |
+
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=analyze_review,
|
| 44 |
+
inputs=gr.Textbox(
|
| 45 |
+
lines=5,
|
| 46 |
+
placeholder="paste review text here...",
|
| 47 |
+
label="review text"
|
| 48 |
+
),
|
| 49 |
+
outputs=gr.Textbox(
|
| 50 |
+
lines=10,
|
| 51 |
+
label="analysis"
|
| 52 |
+
),
|
| 53 |
+
title="review classifier",
|
| 54 |
+
description="ensemble model for detecting fake reviews"
|
| 55 |
+
)
|
| 56 |
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
print("starting gradio interface", flush=True)
|
| 59 |
print("models will load on first prediction request", flush=True)
|
| 60 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|