duclo90 commited on
Commit
21b60b4
·
verified ·
1 Parent(s): c7dee11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -66
app.py CHANGED
@@ -5,50 +5,41 @@ from transformers import (
5
  AutoModelForSequenceClassification
6
  )
7
 
8
-
9
  MODEL_NAME = "duclo90/results"
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
12
  model.eval()
13
 
14
-
15
  def classify_text(text):
16
  if not text.strip():
17
  return """
18
- <div style="text-align: center; padding: 40px; background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
19
- border-radius: 20px; color: white;">
20
  <h2 style="margin: 0;">⚠️ Oops!</h2>
21
  <p style="margin-top: 10px; font-size: 18px;">Please enter some text to analyze</p>
22
  </div>
23
  """
24
-
25
  inputs = tokenizer(
26
  text,
27
  return_tensors="pt",
28
  truncation=True,
29
  max_length=512
30
  )
31
-
32
  with torch.no_grad():
33
  outputs = model(**inputs)
34
  probs = torch.softmax(outputs.logits, dim=-1)
35
  score, pred = torch.max(probs, dim=-1)
36
-
37
  label = model.config.id2label[pred.item()]
38
-
 
39
  if label.lower() == "human":
40
- return """
41
- <div style="text-align: center; padding: 50px 30px;
42
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
43
- border-radius: 25px; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
44
- animation: slideIn 0.5s ease-out;">
45
  <div style="font-size: 80px; margin-bottom: 20px; animation: bounce 1s ease;">👤</div>
46
- <h1 style="color: white; font-size: 42px; margin: 0; font-weight: 700;">Human-Written</h1>
47
- <p style="color: rgba(255,255,255,0.9); font-size: 20px; margin-top: 15px;">
48
- This text appears to be written by a human
49
- </p>
50
- <div style="margin-top: 30px; padding: 15px 30px; background: rgba(255,255,255,0.2);
51
- border-radius: 15px; display: inline-block;">
52
  <span style="color: white; font-size: 18px; font-weight: 600;">✨ Natural & Authentic</span>
53
  </div>
54
  </div>
@@ -64,18 +55,12 @@ def classify_text(text):
64
  </style>
65
  """
66
  else:
67
- return """
68
- <div style="text-align: center; padding: 50px 30px;
69
- background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
70
- border-radius: 25px; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
71
- animation: slideIn 0.5s ease-out;">
72
  <div style="font-size: 80px; margin-bottom: 20px; animation: bounce 1s ease;">🤖</div>
73
- <h1 style="color: white; font-size: 42px; margin: 0; font-weight: 700;">AI-Generated</h1>
74
- <p style="color: rgba(255,255,255,0.9); font-size: 20px; margin-top: 15px;">
75
- This text appears to be machine-generated
76
- </p>
77
- <div style="margin-top: 30px; padding: 15px 30px; background: rgba(255,255,255,0.2);
78
- border-radius: 15px; display: inline-block;">
79
  <span style="color: white; font-size: 18px; font-weight: 600;">⚡ AI-Powered Content</span>
80
  </div>
81
  </div>
@@ -90,36 +75,11 @@ def classify_text(text):
90
  }
91
  </style>
92
  """
 
 
93
 
94
-
95
  custom_css = """
96
- /* Remove Gradio top bar, API button, settings, footer */
97
- header,
98
- footer,
99
- .gradio-header,
100
- .gradio-footer,
101
- .gradio-toolbar,
102
- #top-bar,
103
- [class*="header"],
104
- [class*="toolbar"],
105
- [class*="footer"],
106
- .svelte-1ipelgc,
107
- .svelte-1xw6x5s {
108
- display: none !important;
109
- height: 0 !important;
110
- min-height: 0 !important;
111
- visibility: hidden !important;
112
- padding: 0 !important;
113
- margin: 0 !important;
114
- }
115
-
116
- /* Remove leftover spacing */
117
- body {
118
- margin: 0 !important;
119
- padding: 0 !important;
120
- }
121
-
122
- /* Your existing styles */
123
  #component-0 {
124
  max-width: 900px;
125
  margin: auto;
@@ -129,7 +89,6 @@ body {
129
  .gradio-container {
130
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
131
  font-family: 'Inter', sans-serif;
132
- min-height: 100vh !important;
133
  }
134
 
135
  #title {
@@ -138,6 +97,7 @@ body {
138
  font-size: 2.5em;
139
  font-weight: 700;
140
  margin-bottom: 10px;
 
141
  }
142
 
143
  #description {
@@ -151,31 +111,45 @@ body {
151
  border-radius: 15px !important;
152
  border: 2px solid rgba(255,255,255,0.3) !important;
153
  background: rgba(255,255,255,0.95) !important;
 
154
  }
155
 
156
  button {
157
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
 
158
  border-radius: 10px !important;
 
159
  font-weight: 600 !important;
 
 
 
160
  }
161
- """
162
 
 
 
 
 
163
 
 
 
 
 
 
 
164
  iface = gr.Interface(
165
  fn=classify_text,
166
  inputs=gr.Textbox(
167
- lines=8,
168
  placeholder="✍️ Paste or type text here to analyze...",
169
- label="Enter Text"
 
170
  ),
171
  outputs=gr.HTML(label="Analysis Result"),
172
  title="<div id='title'>🔍 AI Text Classifier</div>",
173
- description="<div id='description'>Detect whether text is human-written or AI-generated using advanced NLP<br>"
174
- "<span style='font-size: 0.85em; opacity: 0.7;'>Model by Dergaoui Ayoub</span></div>",
175
  flagging_mode="never"
176
  )
177
 
178
-
179
  iface.launch(
180
  theme=gr.themes.Soft(
181
  primary_hue="purple",
@@ -185,4 +159,4 @@ iface.launch(
185
  ),
186
  css=custom_css,
187
  share=False
188
- )
 
5
  AutoModelForSequenceClassification
6
  )
7
 
 
8
  MODEL_NAME = "duclo90/results"
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
11
  model.eval()
12
 
 
13
  def classify_text(text):
14
  if not text.strip():
15
  return """
16
+ <div style="text-align: center; padding: 40px; background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); border-radius: 20px; color: white;">
 
17
  <h2 style="margin: 0;">⚠️ Oops!</h2>
18
  <p style="margin-top: 10px; font-size: 18px;">Please enter some text to analyze</p>
19
  </div>
20
  """
21
+
22
  inputs = tokenizer(
23
  text,
24
  return_tensors="pt",
25
  truncation=True,
26
  max_length=512
27
  )
 
28
  with torch.no_grad():
29
  outputs = model(**inputs)
30
  probs = torch.softmax(outputs.logits, dim=-1)
31
  score, pred = torch.max(probs, dim=-1)
32
+
33
  label = model.config.id2label[pred.item()]
34
+
35
+ # Create attractive HTML output
36
  if label.lower() == "human":
37
+ result = """
38
+ <div style="text-align: center; padding: 50px 30px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 25px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); animation: slideIn 0.5s ease-out;">
 
 
 
39
  <div style="font-size: 80px; margin-bottom: 20px; animation: bounce 1s ease;">👤</div>
40
+ <h1 style="color: white; font-size: 42px; margin: 0; font-weight: 700; text-shadow: 2px 2px 4px rgba(0,0,0,0.2);">Human-Written</h1>
41
+ <p style="color: rgba(255,255,255,0.9); font-size: 20px; margin-top: 15px;">This text appears to be written by a human</p>
42
+ <div style="margin-top: 30px; padding: 15px 30px; background: rgba(255,255,255,0.2); border-radius: 15px; display: inline-block;">
 
 
 
43
  <span style="color: white; font-size: 18px; font-weight: 600;">✨ Natural & Authentic</span>
44
  </div>
45
  </div>
 
55
  </style>
56
  """
57
  else:
58
+ result = """
59
+ <div style="text-align: center; padding: 50px 30px; background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); border-radius: 25px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); animation: slideIn 0.5s ease-out;">
 
 
 
60
  <div style="font-size: 80px; margin-bottom: 20px; animation: bounce 1s ease;">🤖</div>
61
+ <h1 style="color: white; font-size: 42px; margin: 0; font-weight: 700; text-shadow: 2px 2px 4px rgba(0,0,0,0.2);">AI-Generated</h1>
62
+ <p style="color: rgba(255,255,255,0.9); font-size: 20px; margin-top: 15px;">This text appears to be machine-generated</p>
63
+ <div style="margin-top: 30px; padding: 15px 30px; background: rgba(255,255,255,0.2); border-radius: 15px; display: inline-block;">
 
 
 
64
  <span style="color: white; font-size: 18px; font-weight: 600;">⚡ AI-Powered Content</span>
65
  </div>
66
  </div>
 
75
  }
76
  </style>
77
  """
78
+
79
+ return result
80
 
81
+ # Custom CSS for modern look
82
  custom_css = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  #component-0 {
84
  max-width: 900px;
85
  margin: auto;
 
89
  .gradio-container {
90
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
91
  font-family: 'Inter', sans-serif;
 
92
  }
93
 
94
  #title {
 
97
  font-size: 2.5em;
98
  font-weight: 700;
99
  margin-bottom: 10px;
100
+ text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
101
  }
102
 
103
  #description {
 
111
  border-radius: 15px !important;
112
  border: 2px solid rgba(255,255,255,0.3) !important;
113
  background: rgba(255,255,255,0.95) !important;
114
+ box-shadow: 0 8px 32px rgba(0,0,0,0.1) !important;
115
  }
116
 
117
  button {
118
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
119
+ border: none !important;
120
  border-radius: 10px !important;
121
+ padding: 12px 30px !important;
122
  font-weight: 600 !important;
123
+ font-size: 16px !important;
124
+ box-shadow: 0 4px 15px rgba(0,0,0,0.2) !important;
125
+ transition: transform 0.2s !important;
126
  }
 
127
 
128
+ button:hover {
129
+ transform: translateY(-2px) !important;
130
+ box-shadow: 0 6px 20px rgba(0,0,0,0.3) !important;
131
+ }
132
 
133
+ .footer {
134
+ display: none !important;
135
+ }
136
+ """
137
+
138
+ # Create interface with modern theme
139
  iface = gr.Interface(
140
  fn=classify_text,
141
  inputs=gr.Textbox(
142
+ lines=8,
143
  placeholder="✍️ Paste or type text here to analyze...",
144
+ label="Enter Text",
145
+ elem_id="input-text"
146
  ),
147
  outputs=gr.HTML(label="Analysis Result"),
148
  title="<div id='title'>🔍 AI Text Classifier</div>",
149
+ description="<div id='description'>Detect whether text is human-written or AI-generated using advanced NLP<br><span style='font-size: 0.85em; opacity: 0.7; margin-top: 8px; display: inline-block;'>Model by Dergaoui Ayoub</span></div>",
 
150
  flagging_mode="never"
151
  )
152
 
 
153
  iface.launch(
154
  theme=gr.themes.Soft(
155
  primary_hue="purple",
 
159
  ),
160
  css=custom_css,
161
  share=False
162
+ )