| import gradio as gr | |
| import os | |
| import sys | |
| 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 "error: please enter some text" | |
| if not models_loaded: | |
| return "models are loading for the first time, this will take 20-30 minutes. please wait..." | |
| try: | |
| result = predict_review(reviewText) | |
| print(f"raw result: {result}", flush=True) | |
| if "error" in result and result["prediction"] == "error": | |
| return f"error: {result['error']}" | |
| output = f"""prediction: {result['prediction']} | |
| confidence: {result['confidence']:.2%} | |
| fake probability: {result['fake_probability']:.2%} | |
| genuine probability: {result['genuine_probability']:.2%} | |
| model agreement: {result['model_agreement']:.1f}% | |
| is fake: {result['is_fake']} | |
| length category: {result['length_category']} | |
| token count: {result['token_count']}""" | |
| return output | |
| except Exception as e: | |
| return f"error: {str(e)}" | |
| 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="review classifier", | |
| description="ensemble model for detecting fake reviews" | |
| ) | |
| 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) |