File size: 1,757 Bytes
19782f6
5e7487b
5ed6f8f
4e2469a
921635e
 
5ed6f8f
921635e
84c1843
70579dd
 
84c1843
4e2469a
19782f6
 
4e2469a
19782f6
921635e
84c1843
badce84
921635e
19782f6
4e2469a
921635e
4e2469a
921635e
 
4e2469a
19782f6
 
 
 
 
 
 
 
 
 
 
 
a026bba
 
19782f6
921635e
19782f6
 
32acc67
 
 
 
 
 
 
19782f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)