Spaces:
Sleeping
Sleeping
Download app.py from Tarak2134/CarReviews_Classification: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/Tarak2134/CarReviews_Classification/resolve/main/app.py
- Command line
-
hf download hf://spaces/Tarak2134/CarReviews_Classification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Tarak2134/CarReviews_Classification/resolve/main/app.py
1.22 kB
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| # Load the model and tokenizer from your Hugging Face model repository | |
| model_name = "Tarak2134/Car_Reviews" # Replace with your model repository name | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
| # Define a function for inference | |
| def classify_text(text): | |
| inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512) | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| predicted_class = torch.argmax(logits, dim=-1).item() # Get the predicted class | |
| labels = ["Negative", "Neutral", "Positive"] # Adjust based on your model's labels | |
| return labels[predicted_class] | |
| # Set up Gradio interface | |
| interface = gr.Interface( | |
| fn=classify_text, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter text to classify..."), | |
| outputs=gr.Textbox(label="Predicted Sentiment"), | |
| title="Sentiment Classifier", | |
| description="Enter a piece of car review and let the model classify it as Negative, Neutral, or Positive." | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| interface.launch() | |