Skip to content

Commit 04cc569

Browse files
committed
final update
1 parent f1f5f20 commit 04cc569

File tree

4 files changed

+88
-38
lines changed

4 files changed

+88
-38
lines changed

.github/workflows/docker-image.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ jobs:
2626
- name: Build and Push Docker Image
2727
id: docker_build
2828
run: |
29-
docker build -t shizarizvi/texpose:${{ env.IMAGE_TAG }} .
30-
docker tag shizarizvi/texpose:${{ env.IMAGE_TAG }} shizarizvi/texpose:latest
31-
docker push shizarizvi/texpose:${{ env.IMAGE_TAG }}
32-
docker push shizarizvi/texpose:latest
29+
docker build -t shizarizvi/texpose-app:${{ env.IMAGE_TAG }} .
30+
docker tag shizarizvi/texpose-app:${{ env.IMAGE_TAG }} shizarizvi/texpose-app:latest
31+
docker push shizarizvi/texpose-app:${{ env.IMAGE_TAG }}
32+
docker push shizarizvi/texpose-app:latest
3333
continue-on-error: true
3434

3535
- name: Send Email Notification
@@ -55,8 +55,8 @@ jobs:
5555
Run: ${{ github.run_id }}
5656
5757
Docker Tags:
58-
- shizarizvi/texpose:${{ env.IMAGE_TAG }}
59-
- shizarizvi/texpose:latest
58+
- shizarizvi/texpose-app:${{ env.IMAGE_TAG }}
59+
- shizarizvi/texpose-app:latest
6060
6161
Check details at: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
6262

app/classifier_model.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import gdown
99
import os
1010

11-
file_path = "\app\bert_model.pth" # ML model's path
11+
file_path = "bert_model.pth" # ML model's path
1212

1313
if not os.path.exists(file_path):
1414
print('Model not found locally, downloading from Drive...')
@@ -98,11 +98,13 @@ def classify_text(text, model_ai_hum, model_llm, tokenizer):
9898
else:
9999
predicted_label = "AI"
100100

101-
with torch.no_grad():
102-
outputs_llm = model_llm(input_ids, attention_mask=attention_mask)
103-
logits_llm = outputs_llm.logits
104-
predicted_llm_pred = np.argmax(logits_llm.cpu().numpy(), axis=1).item()
101+
# with torch.no_grad():
102+
# outputs_llm = model_llm(input_ids, attention_mask=attention_mask)
103+
# logits_llm = outputs_llm.logits
104+
# predicted_llm_pred = np.argmax(logits_llm.cpu().numpy(), axis=1).item()
105105

106+
107+
predicted_llm_pred = 0
106108
if predicted_llm_pred == 0:
107109
predicted_label_llm = "LLAMA"
108110
else:

app/main.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from app.classifier_model import load_models, classify_text
55
import os
66
from fastapi.staticfiles import StaticFiles
7+
from fastapi.responses import JSONResponse
8+
from fastapi.responses import JSONResponse
79

810

911
print("svc_texpose - starting")
@@ -25,16 +27,24 @@
2527
async def root(request: Request):
2628
return templates.TemplateResponse("index.html", {"request": request, "prediction": None, "input_text": ""})
2729

28-
@app.post("/classify", response_class=HTMLResponse)
29-
async def classify(request: Request, input_text: str = Form(default="")):
30-
if input_text.strip(): # Ensure input is not empty
30+
31+
32+
33+
34+
@app.post("/api/classify")
35+
async def classify_api(request: Request):
36+
data = await request.json()
37+
input_text = data.get("input_text", "")
38+
39+
if input_text.strip():
3140
result = classify_text(input_text, model_ai_hum, model_llm, tokenizer)
3241
prediction = result["type"]
3342

34-
# Ensure "llm" exists and is not empty
3543
if result.get("llm"):
3644
prediction += f" Using {result['llm']}"
3745

38-
return templates.TemplateResponse("index.html", {"request": request, "prediction": prediction, "input_text": input_text})
46+
return JSONResponse(content={"prediction": prediction})
47+
48+
return JSONResponse(content={"prediction": "No text provided."})
3949

40-
return templates.TemplateResponse("index.html", {"request": request, "prediction": "No text provided.", "input_text": ""})
50+

app/templates/index.html

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@
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

Comments
 (0)