JOB_higher / app.py
Bushra346's picture
Create app.py
6d8718a verified
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import gradio as gr
import pytesseract
from PIL import Image
import requests
from bs4 import BeautifulSoup
# Load model
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Prediction function
def predict_job_post(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
prediction = torch.argmax(logits, dim=1).item()
prob = torch.nn.functional.softmax(logits, dim=1)[prediction].item()
if prediction == 1:
return f"🟒 REAL Job (Confidence: {prob:.2f})\nReason: The post has positive and trustworthy language."
else:
return f"πŸ”΄ FAKE Job (Confidence: {prob:.2f})\nReason: The post uses negative or suspicious language patterns."
# OCR for image
def process_image(image):
text = pytesseract.image_to_string(image)
return predict_job_post(text)
# Scraper for URL
def process_url(url):
try:
response = requests.get(url, timeout=5)
soup = BeautifulSoup(response.text, 'html.parser')
text = soup.get_text(separator=' ', strip=True)
return predict_job_post(text[:1000])
except:
return "❌ Error: Could not fetch or process the URL."
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## πŸ•΅β€β™€ Fake Job Detector AI App")
gr.Markdown("Input job description, image, or link to check if the job is real or fake.")
with gr.Tab("Text"):
text_input = gr.Textbox(lines=4, label="Enter Job Text")
text_output = gr.Textbox(label="Result")
text_button = gr.Button("Analyze Text")
text_button.click(fn=predict_job_post, inputs=text_input, outputs=text_output)
with gr.Tab("Image"):
image_input = gr.Image(type="pil", label="Upload Image")
image_output = gr.Textbox(label="Result")
image_button = gr.Button("Analyze Image")
image_button.click(fn=process_image, inputs=image_input, outputs=image_output)
with gr.Tab("URL"):
url_input = gr.Textbox(label="Paste Job Post URL")
url_output = gr.Textbox(label="Result")
url_button = gr.Button("Analyze URL")
url_button.click(fn=process_url, inputs=url_input, outputs=url_output)
demo.launch()