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

Update app.py

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