Spaces:
Sleeping
Sleeping
File size: 2,167 Bytes
cc06c26 bd1c1a3 cc06c26 bd1c1a3 cc06c26 bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 6e8fa3a bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 b62a757 bd1c1a3 6e8fa3a bd1c1a3 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import gradio as gr
from transformers import pipeline
import pandas as pd
import os
# Initialize detector
detector = pipeline("text-classification", model="debojit01/fake-review-detector")
# CSV file setup
FEEDBACK_FILE = "training_data.csv"
if not os.path.exists(FEEDBACK_FILE):
pd.DataFrame(columns=["text", "label"]).to_csv(FEEDBACK_FILE, index=False)
def predict(text):
result = detector(text)[0]
if result["label"] == "LABEL_0": # Real
return {"Real": result["score"], "Fake": 1 - result["score"]}
else: # Fake (LABEL_1)
return {"Real": 1 - result["score"], "Fake": result["score"]}
def save_feedback(text, prediction, is_correct):
"""Save feedback only when user submits"""
if is_correct is None: # No feedback provided
return "Prediction shown"
predicted_label = "Real" if prediction["Real"] > 0.5 else "Fake"
true_label = predicted_label if is_correct else ("Fake" if predicted_label == "Real" else "Real")
new_data = pd.DataFrame({"text": [text], "label": [true_label]})
new_data.to_csv(FEEDBACK_FILE, mode='a', header=not os.path.exists(FEEDBACK_FILE), index=False)
return "Thank you for your feedback!"
with gr.Blocks() as app:
gr.Markdown("## Fake Review Detector")
with gr.Row():
review_input = gr.Textbox(label="Enter Review")
predict_btn = gr.Button("Predict")
output_label = gr.Label(label="Prediction")
with gr.Row(visible=False) as feedback_row:
feedback_radio = gr.Radio(
["Correct", "Incorrect"],
label="Is this prediction accurate?"
)
feedback_btn = gr.Button("Submit Feedback")
status_text = gr.Textbox(label="Status", interactive=False)
def show_prediction(text):
prediction = predict(text)
return prediction, gr.Row(visible=True), ""
predict_btn.click(
show_prediction,
inputs=review_input,
outputs=[output_label, feedback_row, status_text]
)
feedback_btn.click(
save_feedback,
inputs=[review_input, output_label, feedback_radio],
outputs=status_text
)
app.launch() |