hashirlodhi commited on
Commit
fe11d9b
·
verified ·
1 Parent(s): 0ad44c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import numpy as np
4
+
5
+ # Load the trained classifier and TF-IDF vectorizer
6
+ try:
7
+ svm_clf = pickle.load(open('clf.pkl', 'rb'))
8
+ svm_tfidf = pickle.load(open('tfidf.pkl', 'rb'))
9
+ except Exception as e:
10
+ raise Exception(f"Error loading model files: {str(e)}")
11
+
12
+ def predict_text(text):
13
+ # Transform the input text using the TF-IDF vectorizer
14
+ text_transformed = svm_tfidf.transform([text])
15
+
16
+ # Get prediction (0.0 for human, 1.0 for AI)
17
+ prediction = svm_clf.predict(text_transformed)[0]
18
+
19
+ # Get decision score (distance from hyperplane)
20
+ decision_score = svm_clf.decision_function(text_transformed)[0]
21
+
22
+ # Sigmoid scaling to get a confidence-like score (0-100%)
23
+ ai_confidence = 100 * (1 / (1 + np.exp(-decision_score)))
24
+ human_confidence = 100 - ai_confidence
25
+
26
+ # Return formatted result
27
+ label = "AI" if prediction == 1.0 else "Human"
28
+ return f"Prediction: {label}\nConfidence: {ai_confidence:.2f}% AI, {human_confidence:.2f}% Human"
29
+
30
+ # Define the Gradio interface
31
+ iface = gr.Interface(
32
+ fn=predict_text,
33
+ inputs=gr.Textbox(lines=5, placeholder="Enter your text here..."),
34
+ outputs="text",
35
+ title="AI vs Human Text Classifier",
36
+ description="Enter text to classify it as AI-generated or Human-written, with confidence scores."
37
+ )
38
+
39
+ # Launch the interface
40
+ if __name__ == "__main__":
41
+ iface.launch()