duclo90 commited on
Commit
c458b34
·
verified ·
1 Parent(s): 765ba77

Update app.py

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