Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pytesseract
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import requests
|
| 7 |
+
from bs4 import BeautifulSoup
|
| 8 |
+
|
| 9 |
+
# Load model
|
| 10 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
# Prediction function
|
| 15 |
+
def predict_job_post(text):
|
| 16 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
logits = outputs.logits
|
| 20 |
+
prediction = torch.argmax(logits, dim=1).item()
|
| 21 |
+
prob = torch.nn.functional.softmax(logits, dim=1)[prediction].item()
|
| 22 |
+
|
| 23 |
+
if prediction == 1:
|
| 24 |
+
return f"🟢 REAL Job (Confidence: {prob:.2f})\nReason: The post has positive and trustworthy language."
|
| 25 |
+
else:
|
| 26 |
+
return f"🔴 FAKE Job (Confidence: {prob:.2f})\nReason: The post uses negative or suspicious language patterns."
|
| 27 |
+
|
| 28 |
+
# OCR for image
|
| 29 |
+
def process_image(image):
|
| 30 |
+
text = pytesseract.image_to_string(image)
|
| 31 |
+
return predict_job_post(text)
|
| 32 |
+
|
| 33 |
+
# Scraper for URL
|
| 34 |
+
def process_url(url):
|
| 35 |
+
try:
|
| 36 |
+
response = requests.get(url, timeout=5)
|
| 37 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 38 |
+
text = soup.get_text(separator=' ', strip=True)
|
| 39 |
+
return predict_job_post(text[:1000])
|
| 40 |
+
except:
|
| 41 |
+
return "❌ Error: Could not fetch or process the URL."
|
| 42 |
+
|
| 43 |
+
# Gradio UI
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("## 🕵♀ Fake Job Detector AI App")
|
| 46 |
+
gr.Markdown("Input job description, image, or link to check if the job is real or fake.")
|
| 47 |
+
|
| 48 |
+
with gr.Tab("Text"):
|
| 49 |
+
text_input = gr.Textbox(lines=4, label="Enter Job Text")
|
| 50 |
+
text_output = gr.Textbox(label="Result")
|
| 51 |
+
text_button = gr.Button("Analyze Text")
|
| 52 |
+
text_button.click(fn=predict_job_post, inputs=text_input, outputs=text_output)
|
| 53 |
+
|
| 54 |
+
with gr.Tab("Image"):
|
| 55 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 56 |
+
image_output = gr.Textbox(label="Result")
|
| 57 |
+
image_button = gr.Button("Analyze Image")
|
| 58 |
+
image_button.click(fn=process_image, inputs=image_input, outputs=image_output)
|
| 59 |
+
|
| 60 |
+
with gr.Tab("URL"):
|
| 61 |
+
url_input = gr.Textbox(label="Paste Job Post URL")
|
| 62 |
+
url_output = gr.Textbox(label="Result")
|
| 63 |
+
url_button = gr.Button("Analyze URL")
|
| 64 |
+
url_button.click(fn=process_url, inputs=url_input, outputs=url_output)
|
| 65 |
+
|
| 66 |
+
demo.launch()
|