9191
9292 < section class ="page form-section " id ="formSection " style ="display: none ">
9393 < div id ="formContent ">
94- < form id ="aiDetectorForm " method ="POST " action ="/classify ">
94+ < form id ="aiDetectorForm " method ="POST " action ="/api/ classify ">
9595 < h1 class ="form-text "> Text Detector</ h1 >
9696 < textarea
97- class ="{% if prediction == 'Human' %}human-text{% elif prediction %}ai-text{% endif %} "
97+ style ="background-color: black; color: white; opacity: 0.7; font-weight: bold; "
98+ class ="{% if prediction == 'Human' %}human-text{% elif prediction %}ai-text{% endif %} "
9899 placeholder ="Enter text here... "
99100 rows ="5 "
100101 id ="input_text "
@@ -105,11 +106,13 @@ <h1 class="form-text">Text Detector</h1>
105106</ textarea
106107 >
107108 < button class ="submit " type ="submit "> Analyze</ button >
108- {% if prediction and prediction != "" %}
109+
109110 < div id ="output " style ="margin-top: 30px " class ="description output ">
111+ {% if prediction and prediction != "" %}
110112 Prediction : {{ prediction }}
111- </ div >
113+
112114 {% endif %}
115+ </ div >
113116 </ form >
114117 </ div >
115118 </ section >
@@ -192,23 +195,58 @@ <h1 class="form-text">Text Detector</h1>
192195 window . changeTextListenerAdded = true ;
193196 }
194197
195- document
196- . getElementById ( "aiDetectorForm" )
197- . addEventListener ( "submit" , function ( event ) {
198- // event.preventDefault();
199- // const input_text = document.getElementById("input_text").value.trim();
200- // const outputElement = document.getElementById("output");
201-
202- // if (input_text.length === 0) {
203- // outputElement.innerHTML = "Please enter some text.";
204- // return;
205- // }
206-
207- // // // Mock AI detection result
208- // // const isAI = Math.random() > 0.5 ? "AI-generated" : "Human-written";
209- // outputElement.innerHTML = `Analysis: The text is generated by <strong>{{prediction}}</strong>.`;
210- outputElement . classList . add ( "active" ) ;
211- } ) ;
198+
199+
200+
201+ document . getElementById ( "aiDetectorForm" ) . addEventListener ( "submit" , async function ( event ) {
202+ event . preventDefault ( ) ;
203+
204+ const inputText = document . getElementById ( "input_text" ) . value . trim ( ) ;
205+ const outputElement = document . getElementById ( "output" ) ;
206+
207+ // Clear previous result
208+ outputElement . innerHTML = "" ;
209+ outputElement . classList . remove ( "active" ) ;
210+
211+ if ( inputText . length === 0 ) {
212+ outputElement . innerHTML = "Please enter some text." ;
213+ outputElement . classList . add ( "active" ) ;
214+ return ;
215+ }
216+
217+ try {
218+ const response = await fetch ( "/api/classify" , {
219+ method : "POST" ,
220+ headers : {
221+ "Content-Type" : "application/json" ,
222+ } ,
223+ body : JSON . stringify ( { input_text : inputText } ) ,
224+ } ) ;
225+
226+ const data = await response . json ( ) ;
227+
228+ if ( data && data . prediction ) {
229+ outputElement . innerHTML = `Prediction: <strong>${ data . prediction } </strong>` ;
230+ outputElement . classList . add ( "active" ) ;
231+
232+ // Optional: Clear the textarea for next input
233+
234+ } else {
235+ outputElement . innerHTML = "Something went wrong. Try again." ;
236+ outputElement . classList . add ( "active" ) ;
237+ }
238+ } catch ( error ) {
239+ console . error ( "Error:" , error ) ;
240+ outputElement . innerHTML = "Error occurred while processing." ;
241+ outputElement . classList . add ( "active" ) ;
242+ }
243+ } ) ;
244+
245+
246+
247+
248+
249+
212250 </ script >
213251 </ body >
214252</ html >
0 commit comments