preetidav commited on
Commit
4c6f025
·
verified ·
1 Parent(s): 3136a6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -23
app.py CHANGED
@@ -1,31 +1,22 @@
1
  import gradio as gr
2
- import transformers
3
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
- import torch
5
 
6
- # Load the model and tokenizer from the Hugging Face Hub
7
- model = AutoModelForSequenceClassification.from_pretrained("preetidav/sentomodel")
8
- tokenizer = AutoTokenizer.from_pretrained("preetidav/sentomodel")
9
 
10
- # Function to predict sentiment
11
- def predict_sentiment(text):
12
- # Tokenize the input text
13
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
14
- # Forward pass through the model
15
- outputs = model(**inputs)
16
- # Get the prediction (0 or 1 for binary classification)
17
- prediction = torch.argmax(outputs.logits, dim=1).item()
18
- # Map prediction to sentiment labels
19
- return "positive" if prediction == 1 else "negative"
20
 
21
- # Set up the Gradio interface
22
  iface = gr.Interface(
23
- fn=predict_sentiment, # Function to call
24
- inputs=gr.Textbox(label="Input Text"), # Input field for the text
25
- outputs=gr.Textbox(label="Sentiment"), # Output field for the sentiment
26
- title="Sentiment Analysis Model", # Title of the app
27
- description="This model predicts whether a given text has positive or negative sentiment.", # Description of the app
28
  )
29
 
30
- # Launch the app
31
  iface.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
 
3
 
4
+ # Load pre-trained model for CoLA (linguistic acceptability)
5
+ model_name = "preetidav/distilbert-base-uncased-finetuned-cola"
6
+ classifier = pipeline("text-classification", model=model_name)
7
 
8
+ def classify_sentence(sentence):
9
+ result = classifier(sentence)[0]
10
+ return f"Label: {result['label']} (Confidence: {result['score']:.2f})"
 
 
 
 
 
 
 
11
 
12
+ # Create Gradio interface
13
  iface = gr.Interface(
14
+ fn=classify_sentence,
15
+ inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
16
+ outputs="text",
17
+ title="Sentence Acceptability Classifier",
18
+ description="This model classifies whether a sentence is linguistically acceptable (LABEL_1) or not (LABEL_0).",
19
  )
20
 
21
+ # Launch app
22
  iface.launch()