| import gradio as gr | |
| import os | |
| import sys | |
| import json | |
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from api.predict import predict_review, models_loaded | |
| def analyze_review(reviewText): | |
| from api.predict import models_loaded | |
| if not reviewText or len(reviewText.strip()) == 0: | |
| return json.dumps({"error": "please enter some text"}, indent=2) | |
| if not models_loaded: | |
| return json.dumps({"error": "models are loading for the first time, this will take 20-30 minutes. please wait..."}, indent=2) | |
| try: | |
| result = predict_review(reviewText) | |
| print(f"raw result: {result}", flush=True) | |
| if "error" in result and result["prediction"] == "error": | |
| return json.dumps({"error": result['error']}, indent=2) | |
| return json.dumps(result, indent=2) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}, indent=2) | |
| demo = gr.Interface( | |
| fn=analyze_review, | |
| inputs=gr.Textbox( | |
| lines=5, | |
| placeholder="paste review text here...", | |
| label="review text" | |
| ), | |
| outputs=gr.Textbox( | |
| lines=10, | |
| label="analysis" | |
| ), | |
| title="SentinelCheck API", | |
| description="ensemble model (bert + roberta + distilbert) for detecting fake reviews targeting small businesses" | |
| ) | |
| if __name__ == "__main__": | |
| print("starting gradio interface", flush=True) | |
| print("preloading models...", flush=True) | |
| try: | |
| from api.predict import loadResources | |
| loadResources() | |
| print("models preloaded successfully", flush=True) | |
| except Exception as e: | |
| print(f"error preloading models: {str(e)}", flush=True) | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |