Spaces:
Runtime error
Runtime error
update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load model and tokenizer from local directory
|
6 |
+
MODEL_PATH = "./saved_model1"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
9 |
+
|
10 |
+
def predict(text):
|
11 |
+
# Preprocess input
|
12 |
+
inputs = tokenizer(
|
13 |
+
text,
|
14 |
+
padding=True,
|
15 |
+
truncation=True,
|
16 |
+
max_length=512,
|
17 |
+
return_tensors="pt"
|
18 |
+
)
|
19 |
+
|
20 |
+
# Inference
|
21 |
+
with torch.no_grad():
|
22 |
+
outputs = model(**inputs)
|
23 |
+
|
24 |
+
# Postprocess output (modify based on your task)
|
25 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
26 |
+
predicted_class = torch.argmax(probabilities).item()
|
27 |
+
|
28 |
+
return {
|
29 |
+
"text": text,
|
30 |
+
"predicted_class": predicted_class,
|
31 |
+
"probabilities": probabilities.tolist()[0]
|
32 |
+
}
|
33 |
+
|
34 |
+
# Create Gradio interface
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=predict,
|
37 |
+
inputs=gr.Textbox(label="Input Text", lines=3),
|
38 |
+
outputs=[
|
39 |
+
gr.Textbox(label="Processed Text"),
|
40 |
+
gr.Number(label="Predicted Class"),
|
41 |
+
gr.Label(label="Class Probabilities")
|
42 |
+
],
|
43 |
+
title="BERT Model Deployment",
|
44 |
+
examples=[["Sample text 1"], ["Another example text"]]
|
45 |
+
)
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
demo.launch()
|