import gradio as gr import pickle import numpy as np # Load the trained classifier and TF-IDF vectorizer try: svm_clf = pickle.load(open('clf.pkl', 'rb')) svm_tfidf = pickle.load(open('tfidf.pkl', 'rb')) except Exception as e: raise Exception(f"Error loading model files: {str(e)}") def predict_text(text): # Transform the input text using the TF-IDF vectorizer text_transformed = svm_tfidf.transform([text]) # Get prediction (0.0 for human, 1.0 for AI) prediction = svm_clf.predict(text_transformed)[0] # Get decision score (distance from hyperplane) decision_score = svm_clf.decision_function(text_transformed)[0] # Sigmoid scaling to get a confidence-like score (0-100%) ai_confidence = 100 * (1 / (1 + np.exp(-decision_score))) human_confidence = 100 - ai_confidence # Return formatted result label = "AI" if prediction == 1.0 else "Human" return f"Prediction: {label}\nConfidence: {ai_confidence:.2f}% AI, {human_confidence:.2f}% Human" # Define the Gradio interface iface = gr.Interface( fn=predict_text, inputs=gr.Textbox(lines=5, placeholder="Enter your text here..."), outputs="text", title="AI vs Human Text Classifier", description="Enter text to classify it as AI-generated or Human-written, with confidence scores." ) # Launch the interface if __name__ == "__main__": iface.launch()