Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
model_name = "alperugurcan/nlp-disaster"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
+
|
9 |
+
def predict(text):
|
10 |
+
# Tokenize the input text
|
11 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
12 |
+
# Get model predictions
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
predictions = torch.argmax(outputs.logits, dim=-1)
|
16 |
+
return "Positive" if predictions.item() == 1 else "Negative"
|
17 |
+
|
18 |
+
# Create a Gradio interface
|
19 |
+
iface = gr.Interface(fn=predict,
|
20 |
+
inputs="text",
|
21 |
+
outputs="text",
|
22 |
+
title="Sentiment Analysis",
|
23 |
+
description="Enter text to analyze sentiment (Positive/Negative).")
|
24 |
+
|
25 |
+
# Launch the app
|
26 |
+
iface.launch()
|