Spaces:
Sleeping
Sleeping
added the feedback option for future training data
Browse files
app.py
CHANGED
|
@@ -7,9 +7,9 @@ import os
|
|
| 7 |
detector = pipeline("text-classification", model="debojit01/fake-review-detector")
|
| 8 |
|
| 9 |
# CSV file setup
|
| 10 |
-
FEEDBACK_FILE = "
|
| 11 |
if not os.path.exists(FEEDBACK_FILE):
|
| 12 |
-
pd.DataFrame(columns=["text", "
|
| 13 |
|
| 14 |
def predict(text):
|
| 15 |
result = detector(text)[0]
|
|
@@ -18,42 +18,50 @@ def predict(text):
|
|
| 18 |
else: # Fake (LABEL_1)
|
| 19 |
return {"Real": 1 - result["score"], "Fake": result["score"]}
|
| 20 |
|
| 21 |
-
def save_feedback(text, prediction,
|
| 22 |
-
"""Save
|
| 23 |
-
|
| 24 |
-
"
|
| 25 |
-
"predicted_label": "Real" if prediction["Real"] > 0.5 else "Fake",
|
| 26 |
-
"user_feedback": feedback
|
| 27 |
-
}
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
with gr.Blocks() as app:
|
| 36 |
-
gr.Markdown("## Fake Review Detector
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
feedback = gr.Radio(
|
| 44 |
-
choices=["Correct", "Incorrect"],
|
| 45 |
-
label="Is this prediction correct?"
|
| 46 |
-
)
|
| 47 |
-
submit_btn = gr.Button("Submit Feedback")
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
save_feedback,
|
| 55 |
-
inputs=[
|
| 56 |
-
outputs=
|
| 57 |
)
|
| 58 |
|
| 59 |
app.launch()
|
|
|
|
| 7 |
detector = pipeline("text-classification", model="debojit01/fake-review-detector")
|
| 8 |
|
| 9 |
# CSV file setup
|
| 10 |
+
FEEDBACK_FILE = "training_data.csv"
|
| 11 |
if not os.path.exists(FEEDBACK_FILE):
|
| 12 |
+
pd.DataFrame(columns=["text", "label"]).to_csv(FEEDBACK_FILE, index=False)
|
| 13 |
|
| 14 |
def predict(text):
|
| 15 |
result = detector(text)[0]
|
|
|
|
| 18 |
else: # Fake (LABEL_1)
|
| 19 |
return {"Real": 1 - result["score"], "Fake": result["score"]}
|
| 20 |
|
| 21 |
+
def save_feedback(text, prediction, is_correct):
|
| 22 |
+
"""Save feedback only when user submits"""
|
| 23 |
+
if is_correct is None: # No feedback provided
|
| 24 |
+
return "Prediction shown"
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
predicted_label = "Real" if prediction["Real"] > 0.5 else "Fake"
|
| 27 |
+
true_label = predicted_label if is_correct else ("Fake" if predicted_label == "Real" else "Real")
|
| 28 |
+
|
| 29 |
+
new_data = pd.DataFrame({"text": [text], "label": [true_label]})
|
| 30 |
+
new_data.to_csv(FEEDBACK_FILE, mode='a', header=not os.path.exists(FEEDBACK_FILE), index=False)
|
| 31 |
+
return "Thank you for your feedback!"
|
| 32 |
|
| 33 |
with gr.Blocks() as app:
|
| 34 |
+
gr.Markdown("## Fake Review Detector")
|
| 35 |
|
| 36 |
with gr.Row():
|
| 37 |
+
review_input = gr.Textbox(label="Enter Review")
|
| 38 |
+
predict_btn = gr.Button("Predict")
|
| 39 |
|
| 40 |
+
output_label = gr.Label(label="Prediction")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
with gr.Row(visible=False) as feedback_row:
|
| 43 |
+
feedback_radio = gr.Radio(
|
| 44 |
+
["Correct", "Incorrect"],
|
| 45 |
+
label="Is this prediction accurate?"
|
| 46 |
+
)
|
| 47 |
+
feedback_btn = gr.Button("Submit Feedback")
|
| 48 |
|
| 49 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
| 50 |
+
|
| 51 |
+
def show_prediction(text):
|
| 52 |
+
prediction = predict(text)
|
| 53 |
+
return prediction, gr.Row(visible=True), ""
|
| 54 |
+
|
| 55 |
+
predict_btn.click(
|
| 56 |
+
show_prediction,
|
| 57 |
+
inputs=review_input,
|
| 58 |
+
outputs=[output_label, feedback_row, status_text]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
feedback_btn.click(
|
| 62 |
save_feedback,
|
| 63 |
+
inputs=[review_input, output_label, feedback_radio],
|
| 64 |
+
outputs=status_text
|
| 65 |
)
|
| 66 |
|
| 67 |
app.launch()
|