Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Get Hugging Face token from environment variable
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
+
if not HF_TOKEN:
|
| 9 |
+
raise ValueError("Please set HF_TOKEN environment variable with your Hugging Face access token")
|
| 10 |
+
|
| 11 |
+
# Load model and tokenizer
|
| 12 |
+
model_name = "iimran/AnalyserV1"
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN)
|
| 14 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, token=HF_TOKEN)
|
| 15 |
+
|
| 16 |
+
def classify_complaint(text):
|
| 17 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
return model.config.id2label[torch.argmax(outputs.logits).item()]
|
| 21 |
+
|
| 22 |
+
# Create Gradio interface
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=classify_complaint,
|
| 25 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter your complaint here...", label="Complaint Text"),
|
| 26 |
+
outputs=gr.Label(label="Predicted Category"),
|
| 27 |
+
title="Complaint Category Classifier",
|
| 28 |
+
description="Automatically classify community complaints into specific categories",
|
| 29 |
+
examples=[
|
| 30 |
+
["I wanted to bring to your attention that a huge big truck has been parked on Main Street"],
|
| 31 |
+
["There are overgrown bushes on Oak Road that pose a fire risk"],
|
| 32 |
+
["Excessive noise from construction site during night hours"]
|
| 33 |
+
]
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
demo.launch()
|