Pushp123 commited on
Commit
70a78ec
·
verified ·
1 Parent(s): 4a82a14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -1,33 +1,40 @@
1
- #1. Importing Lib
 
 
2
  import numpy as np
3
  import pandas as pd
4
  from sklearn.model_selection import train_test_split
5
  from sklearn.feature_extraction.text import TfidfVectorizer
6
  from sklearn.linear_model import LogisticRegression
7
  from sklearn.metrics import accuracy_score
8
- import gradio as gr
9
 
10
- #2. Data Preprocessing
11
- df=pd.read_csv("mail_data (1).csv")
 
12
 
13
- df.loc[df["Category"]=="spam","Category",]=0
14
- df.loc[df["Category"]=="ham","Category",]=1
15
 
16
- # split data into dependent and independednt
17
  x=df["Message"]
18
  y=df["Category"]
19
 
20
- #3. Modeling Part
 
 
 
 
21
  x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
22
 
23
- # Vectorization process for message content
 
24
  feature_extraction=TfidfVectorizer(min_df=1,stop_words="english",lowercase=True)
25
 
26
- x_train_features = feature_extraction.fit_transform(x_train)
27
- x_test_features = feature_extraction.transform(x_test)
 
28
 
29
- y_train = y_train.astype("int")
30
- y_test = y_test.astype("int")
31
 
32
 
33
 
@@ -35,7 +42,6 @@ model=LogisticRegression()
35
  model.fit(x_train_features,y_train)
36
 
37
 
38
-
39
  x_predict=model.predict(x_train_features)
40
  x_accuracy=accuracy_score(x_predict,y_train)
41
 
@@ -43,23 +49,24 @@ y_predict=model.predict(x_test_features)
43
  y_accuracy=accuracy_score(y_predict,y_test)
44
 
45
 
46
- #4. Gradio Part
 
47
 
48
  # Function to predict whether the email is spam or ham
49
  def classify_email(email_text):
50
  # Transform the input email text using the same vectorizer used during training
51
  input_data_features = feature_extraction.transform([email_text])
52
-
53
  # Predict using the trained model
54
  prediction = model.predict(input_data_features)
55
-
56
  # Return the result based on the prediction
57
  if prediction[0] == 0:
58
  return "Your email is Spam"
59
  else:
60
  return "Your email is Ham"
61
 
62
- # Create a Gradio interface for user input
63
  interface = gr.Interface(
64
  fn=classify_email, # Function to be called when user interacts
65
  inputs=gr.Textbox(label="Enter your email text here", placeholder="Type your email...", lines=5),
@@ -69,4 +76,4 @@ interface = gr.Interface(
69
 
70
  # Launch the interface
71
  interface.launch()
72
-
 
1
+ # importing lib
2
+
3
+ import gradio as gr
4
  import numpy as np
5
  import pandas as pd
6
  from sklearn.model_selection import train_test_split
7
  from sklearn.feature_extraction.text import TfidfVectorizer
8
  from sklearn.linear_model import LogisticRegression
9
  from sklearn.metrics import accuracy_score
 
10
 
11
+ #Data Preprocessing
12
+
13
+ df=pd.read_csv("mail_data.csv")
14
 
15
+ df.loc[df["Category"]=="spam","Category"]=0
16
+ df.loc[df["Category"]=="ham","Category"]=1
17
 
 
18
  x=df["Message"]
19
  y=df["Category"]
20
 
21
+
22
+
23
+
24
+ # Modeling part
25
+
26
  x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
27
 
28
+ # Features extraction using TfidfVectorizer
29
+
30
  feature_extraction=TfidfVectorizer(min_df=1,stop_words="english",lowercase=True)
31
 
32
+ x_train_features=feature_extraction.fit_transform(x_train)
33
+ x_test_features=feature_extraction.transform(x_test)
34
+
35
 
36
+ y_train=y_train.astype('int')
37
+ y_test=y_test.astype('int')
38
 
39
 
40
 
 
42
  model.fit(x_train_features,y_train)
43
 
44
 
 
45
  x_predict=model.predict(x_train_features)
46
  x_accuracy=accuracy_score(x_predict,y_train)
47
 
 
49
  y_accuracy=accuracy_score(y_predict,y_test)
50
 
51
 
52
+
53
+ # UI for the Model
54
 
55
  # Function to predict whether the email is spam or ham
56
  def classify_email(email_text):
57
  # Transform the input email text using the same vectorizer used during training
58
  input_data_features = feature_extraction.transform([email_text])
59
+
60
  # Predict using the trained model
61
  prediction = model.predict(input_data_features)
62
+
63
  # Return the result based on the prediction
64
  if prediction[0] == 0:
65
  return "Your email is Spam"
66
  else:
67
  return "Your email is Ham"
68
 
69
+ # Create the Gradio interface
70
  interface = gr.Interface(
71
  fn=classify_email, # Function to be called when user interacts
72
  inputs=gr.Textbox(label="Enter your email text here", placeholder="Type your email...", lines=5),
 
76
 
77
  # Launch the interface
78
  interface.launch()
79
+