Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,21 @@
|
|
1 |
-
import torch
|
2 |
-
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
10 |
-
|
11 |
-
# Tokenize the input
|
12 |
-
inputs = tokenizer(question, context, return_tensors="pt")
|
13 |
-
|
14 |
-
# Generate the answer
|
15 |
with torch.no_grad():
|
16 |
outputs = model(**inputs)
|
17 |
-
|
18 |
-
|
19 |
-
answer = tokenizer.decode(outputs.start_logits.argmax(), skip_special_tokens=True)
|
20 |
-
|
21 |
-
return answer
|
22 |
|
23 |
interface = gr.Interface(
|
24 |
-
fn=
|
25 |
-
inputs=
|
26 |
-
gr.inputs.Textbox(label="Question"),
|
27 |
-
gr.inputs.Textbox(label="Context")
|
28 |
-
],
|
29 |
outputs="text",
|
30 |
-
title="
|
31 |
-
description="Enter a
|
32 |
)
|
33 |
-
|
34 |
interface.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
|
5 |
+
def classify_text(text):
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
8 |
+
inputs = tokenizer(text, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
with torch.no_grad():
|
10 |
outputs = model(**inputs)
|
11 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
12 |
+
return "Positive" if predicted_class == 1 else "Negative"
|
|
|
|
|
|
|
13 |
|
14 |
interface = gr.Interface(
|
15 |
+
fn=classify_text,
|
16 |
+
inputs=gr.inputs.Textbox(label="Text"),
|
|
|
|
|
|
|
17 |
outputs="text",
|
18 |
+
title="Sentiment Analysis",
|
19 |
+
description="Enter a text to classify its sentiment as positive or negative.",
|
20 |
)
|
|
|
21 |
interface.launch()
|