Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the model and tokenizer from Hugging Face
|
| 7 |
+
model_name = "poudel/Depression_and_Non-Depression_Classifier" # Replace with your Hugging Face model name
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Define the prediction function
|
| 13 |
+
def predict(text):
|
| 14 |
+
# Tokenize the input text
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 16 |
+
|
| 17 |
+
# Get model predictions
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
|
| 21 |
+
# Convert logits to probabilities
|
| 22 |
+
probabilities = torch.softmax(outputs.logits, dim=-1)
|
| 23 |
+
|
| 24 |
+
# Get the predicted class (0 or 1)
|
| 25 |
+
predicted_class = torch.argmax(probabilities, dim=1).item()
|
| 26 |
+
|
| 27 |
+
# Map the predicted class to the label (0 = Depression, 1 = Non-depression)
|
| 28 |
+
label_mapping = {0: "Depression", 1: "Non-depression"}
|
| 29 |
+
|
| 30 |
+
return label_mapping[predicted_class]
|
| 31 |
+
|
| 32 |
+
# Create a Gradio interface
|
| 33 |
+
interface = gr.Interface(
|
| 34 |
+
fn=predict, # The function to be called for predictions
|
| 35 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter some text here..."), # Input textbox for the user
|
| 36 |
+
outputs="text", # Output is the predicted class as text
|
| 37 |
+
title="Depression Classification", # Title of the app
|
| 38 |
+
description="Enter a sentence to classify it as 'Depression' or 'Non-depression'.", # Short description
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the Gradio app
|
| 42 |
+
interface.launch()
|