no more string as a return output only json
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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
|
|
@@ -9,32 +10,22 @@ def analyze_review(reviewText):
|
|
| 9 |
from api.predict import models_loaded
|
| 10 |
|
| 11 |
if not reviewText or len(reviewText.strip()) == 0:
|
| 12 |
-
return "error: please enter some text"
|
| 13 |
|
| 14 |
if not models_loaded:
|
| 15 |
-
return "models are loading for the first time, this will take 20-30 minutes. please wait..."
|
| 16 |
|
| 17 |
try:
|
| 18 |
result = predict_review(reviewText)
|
| 19 |
print(f"raw result: {result}", flush=True)
|
| 20 |
|
| 21 |
if "error" in result and result["prediction"] == "error":
|
| 22 |
-
return
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
confidence: {result['confidence']:.4f}
|
| 27 |
-
fake probability: {result['fake_probability']:.4f}
|
| 28 |
-
genuine probability: {result['genuine_probability']:.4f}
|
| 29 |
-
model agreement: {result['model_agreement']:.2f}
|
| 30 |
-
is fake: {result['is_fake']}
|
| 31 |
-
length category: {result['length_category']}
|
| 32 |
-
token count: {result['token_count']}"""
|
| 33 |
-
|
| 34 |
-
return output
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
-
return
|
| 38 |
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=analyze_review,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 7 |
from api.predict import predict_review, models_loaded
|
|
|
|
| 10 |
from api.predict import models_loaded
|
| 11 |
|
| 12 |
if not reviewText or len(reviewText.strip()) == 0:
|
| 13 |
+
return json.dumps({"error": "please enter some text"}, indent=2)
|
| 14 |
|
| 15 |
if not models_loaded:
|
| 16 |
+
return json.dumps({"error": "models are loading for the first time, this will take 20-30 minutes. please wait..."}, indent=2)
|
| 17 |
|
| 18 |
try:
|
| 19 |
result = predict_review(reviewText)
|
| 20 |
print(f"raw result: {result}", flush=True)
|
| 21 |
|
| 22 |
if "error" in result and result["prediction"] == "error":
|
| 23 |
+
return json.dumps({"error": result['error']}, indent=2)
|
| 24 |
|
| 25 |
+
return json.dumps(result, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
+
return json.dumps({"error": str(e)}, indent=2)
|
| 29 |
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=analyze_review,
|