rviana commited on
Commit
2e48c0b
·
1 Parent(s): daf6891

Simplified model and dataset loading for testing

Browse files
Files changed (1) hide show
  1. app.py +26 -1
app.py CHANGED
@@ -1 +1,26 @@
1
- print("Hello, World!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+ import torch
5
+
6
+ # Check if GPU is available
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ # Load the IMDb dataset
10
+ dataset = load_dataset('imdb', split='test[:1%]') # Load a small portion for testing
11
+
12
+ # Initialize the tokenizer and model
13
+ tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
14
+ model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)
15
+ model.to(device)
16
+
17
+ # Function to classify sentiment
18
+ def classify_text(text):
19
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
20
+ outputs = model(**inputs)
21
+ prediction = torch.argmax(outputs.logits, dim=-1).item()
22
+ return "Positive" if prediction == 1 else "Negative"
23
+
24
+ # Set up the Gradio interface
25
+ iface = gr.Interface(fn=classify_text, inputs="text", outputs="text")
26
+ iface.launch()