Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,35 @@
|
|
1 |
-
import
|
2 |
-
from
|
|
|
3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
MODEL_PATH = "./saved_model1"
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
8 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
9 |
|
10 |
-
|
11 |
-
|
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 |
-
|
25 |
-
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
26 |
-
predicted_class = torch.argmax(probabilities).item()
|
27 |
-
|
28 |
return {
|
29 |
-
"
|
30 |
-
"
|
31 |
-
"probabilities": probabilities.tolist()[0]
|
32 |
}
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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 |
-
|
48 |
-
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
4 |
import torch
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Your existing prediction code remains unchanged
|
10 |
+
Label = ['Login Issue', 'Booking Issue', 'Delivery Issue', 'Laboratory Issue', 'Application Issue']
|
11 |
|
12 |
+
# ... [Keep your existing CORS and model loading code] ...
|
|
|
|
|
|
|
13 |
|
14 |
+
# Gradio Interface
|
15 |
+
def gradio_predict(issue_text):
|
16 |
+
inputs = tokenizer(issue_text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
with torch.no_grad():
|
18 |
outputs = model(**inputs)
|
19 |
+
predictions = torch.softmax(outputs.logits, dim=1)
|
20 |
+
label_idx = torch.argmax(predictions, dim=1).item()
|
|
|
|
|
|
|
21 |
return {
|
22 |
+
"Predicted Category": Label[label_idx],
|
23 |
+
"Confidence": f"{predictions[0][label_idx].item():.4f}"
|
|
|
24 |
}
|
25 |
|
26 |
+
gradio_app = gr.Interface(
|
27 |
+
fn=gradio_predict,
|
28 |
+
inputs=gr.Textbox(label="Enter Issue Description"),
|
29 |
+
outputs=gr.JSON(label="Prediction Results"),
|
30 |
+
title="Issue Classifier",
|
31 |
+
description="BERT-based classification demo"
|
|
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
|
34 |
+
# Mount Gradio to FastAPI
|
35 |
+
app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
|