File size: 1,224 Bytes
bbcf032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bd6f78
bbcf032
 
 
 
8621222
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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()