seek007 commited on
Commit
6d8b322
1 Parent(s): 1f5ecb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -40
app.py CHANGED
@@ -34,28 +34,21 @@ def predict(text=None , fil=None):
34
  fig=None
35
  if fil:
36
  if fil.name.endswith('.csv'):
37
- df = pd.read_csv(fil.name)
38
  elif fil.name.endswith('.xlsx') or fil.name.endswith('.xls'):
39
- df = pd.read_excel(fil.name)
40
  else:
41
  raise ValueError("Unsupported file type. Please upload a CSV or Excel file.")
42
 
43
- # df= df.sample(20)
44
- lst = list(df.tweet)
45
  m =[normalizeTweet(i) for i in lst]
46
- # m = [truncate_string(i) for i in m]
47
  d = pd.DataFrame(pipe.predict(m))
48
  df['label'] = d['label']
49
- # print(df.sample(5))
50
- df.drop('sarcastic', axis=1, inplace=True)
51
- # print(df.sample(5))
52
 
53
- mapping = {
54
- 'LABEL_0': 'non_sarcastic',
55
- 'LABEL_1': 'sarcastic'
56
- }
57
 
58
- # df['label']=df['label'].map(mapping)
59
  sarcastic_count = np.sum(df.label =='sarcastic')
60
  non_sarcastic_count = np.sum(df.label =='non_sarcastic')
61
 
@@ -70,18 +63,11 @@ def predict(text=None , fil=None):
70
 
71
  plt.title('Sarcastic vs Non-Sarcastic Tweets')
72
 
73
- # fig = plt.figure() #figsize=(8, 6)
74
- # sns.countplot(x='label', data=df, palette='viridis')
75
- # plt.title('Result: Count Plot') # Add a title to the plot
76
- # plt.xlabel('label') # Add label for the x-axis
77
- # plt.ylabel('Count')
78
- # Perform sentiment prediction
79
  if text !="" or fil !=None:
80
  prediction = pipe.predict([preprocessed_text])[0]
81
  print(prediction)
82
- # sentiment = {p['label']: p['score'] for p in prediction}
83
- # sentiment['']
84
- # print(sentiment)
85
  sentiment = "Sarcastic" if (prediction['label'] == 'LABEL_1' or prediction['label'] =='sarcastic') else "Non Sarcastic"
86
  if fil == None:
87
  df= pd.DataFrame([{'tweet':text, 'label':sentiment}])
@@ -110,18 +96,19 @@ def classifyB(text=None , fil=None):
110
  sentiment =None
111
  df=None
112
  fig=None
 
113
  labels = ['sarcasm', 'irony','Staire', 'understatement','overstatement', 'rhetorical question']
 
114
  if fil:
115
  if fil.name.endswith('.csv'):
116
- df = pd.read_csv(fil.name)
117
  elif fil.name.endswith('.xlsx') or fil.name.endswith('.xls'):
118
- df = pd.read_excel(fil.name)
119
  else:
120
  raise ValueError("Unsupported file type. Please upload a CSV or Excel file.")
121
 
122
- lst = list(df.tweet)
123
  m =[normalizeTweet(i) for i in lst]
124
- # m = [truncate_string(i) for i in m]
125
  d = pipe2(m)
126
 
127
  structured_data = []
@@ -136,32 +123,26 @@ def classifyB(text=None , fil=None):
136
  df1 = pd.DataFrame(structured_data)
137
  df = pd.concat([df, df1], axis=1)
138
 
139
- # df["labels"] = d['labels']
140
- # print("df: ",df.head())
141
- # return df.head()
142
-
143
 
144
  fig = plt.figure() #figsize=(8, 6)
145
  sns.countplot(x='label', data=df, palette='viridis')
146
  plt.title('Result: Count Plot') # Add a title to the plot
147
  plt.xlabel('label') # Add label for the x-axis
148
  plt.ylabel('Count')
 
 
 
149
  # Perform sentiment prediction
150
- if text !=None or fil !=None:
151
  prediction = pipe2([preprocessed_text])[0]
152
- print(prediction["label"])
153
  labels = prediction['label']
154
- scores = prediction['score']
155
-
156
- # Combine labels and scores, and sort by score in descending order
157
-
158
-
159
- # Extract top 3 labels and their scores
160
-
161
  sentiment = labels
 
 
162
 
163
 
164
- return sentiment, df, fig
165
 
166
  file_path =gr.File(label="Upload a File")
167
  label = gr.Label( label="Labels")
 
34
  fig=None
35
  if fil:
36
  if fil.name.endswith('.csv'):
37
+ df = pd.read_csv(fil.name, header=None)
38
  elif fil.name.endswith('.xlsx') or fil.name.endswith('.xls'):
39
+ df = pd.read_excel(fil.name, header=None)
40
  else:
41
  raise ValueError("Unsupported file type. Please upload a CSV or Excel file.")
42
 
43
+
44
+ lst = list(df[0])
45
  m =[normalizeTweet(i) for i in lst]
46
+
47
  d = pd.DataFrame(pipe.predict(m))
48
  df['label'] = d['label']
 
 
 
49
 
 
 
 
 
50
 
51
+
52
  sarcastic_count = np.sum(df.label =='sarcastic')
53
  non_sarcastic_count = np.sum(df.label =='non_sarcastic')
54
 
 
63
 
64
  plt.title('Sarcastic vs Non-Sarcastic Tweets')
65
 
66
+
 
 
 
 
 
67
  if text !="" or fil !=None:
68
  prediction = pipe.predict([preprocessed_text])[0]
69
  print(prediction)
70
+
 
 
71
  sentiment = "Sarcastic" if (prediction['label'] == 'LABEL_1' or prediction['label'] =='sarcastic') else "Non Sarcastic"
72
  if fil == None:
73
  df= pd.DataFrame([{'tweet':text, 'label':sentiment}])
 
96
  sentiment =None
97
  df=None
98
  fig=None
99
+
100
  labels = ['sarcasm', 'irony','Staire', 'understatement','overstatement', 'rhetorical question']
101
+
102
  if fil:
103
  if fil.name.endswith('.csv'):
104
+ df = pd.read_csv(fil.name, header=None)
105
  elif fil.name.endswith('.xlsx') or fil.name.endswith('.xls'):
106
+ df = pd.read_excel(fil.name, header=None)
107
  else:
108
  raise ValueError("Unsupported file type. Please upload a CSV or Excel file.")
109
 
110
+ lst = list(df[0])
111
  m =[normalizeTweet(i) for i in lst]
 
112
  d = pipe2(m)
113
 
114
  structured_data = []
 
123
  df1 = pd.DataFrame(structured_data)
124
  df = pd.concat([df, df1], axis=1)
125
 
 
 
 
 
126
 
127
  fig = plt.figure() #figsize=(8, 6)
128
  sns.countplot(x='label', data=df, palette='viridis')
129
  plt.title('Result: Count Plot') # Add a title to the plot
130
  plt.xlabel('label') # Add label for the x-axis
131
  plt.ylabel('Count')
132
+ if text == None:
133
+ sentiment = df['label'][0]
134
+
135
  # Perform sentiment prediction
136
+ if text != None:
137
  prediction = pipe2([preprocessed_text])[0]
138
+ # print(prediction["label"])
139
  labels = prediction['label']
140
+ # scores = prediction['score']
 
 
 
 
 
 
141
  sentiment = labels
142
+
143
+ return sentiment, df, fig
144
 
145
 
 
146
 
147
  file_path =gr.File(label="Upload a File")
148
  label = gr.Label( label="Labels")