Pushp123 commited on
Commit
0375059
·
verified ·
1 Parent(s): 358bae1

Update app.py

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