Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,51 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
-
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 5 |
|
| 6 |
-
# β
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
try:
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(model_dir)
|
| 12 |
-
label_encoder = joblib.load(
|
| 13 |
-
print("β
Model, Tokenizer, and Label Encoder loaded.")
|
| 14 |
except Exception as e:
|
| 15 |
raise RuntimeError(f"β Failed to load model or tokenizer: {e}")
|
| 16 |
|
| 17 |
-
# β
|
| 18 |
def predict(client_interest, sentiment):
|
| 19 |
try:
|
| 20 |
text = f"{client_interest} Sentiment: {sentiment}"
|
| 21 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
with gr.Row():
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
with gr.Column():
|
| 41 |
-
result = gr.Markdown(label="π― Output")
|
| 42 |
|
| 43 |
-
|
| 44 |
|
| 45 |
-
# β
Launch
|
| 46 |
if __name__ == "__main__":
|
| 47 |
demo.launch()
|
| 48 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import joblib
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
# β
Detect environment and set paths
|
| 8 |
+
if os.path.exists("/home/user/app"):
|
| 9 |
+
model_dir = "campaign-bert-model" # Hugging Face Space
|
| 10 |
+
label_path = "label_encoder.pkl"
|
| 11 |
+
else:
|
| 12 |
+
model_dir = r"C:\Users\gopic\campaign_bert_model\campaign_bert_model\campaign-bert-model"
|
| 13 |
+
label_path = r"C:\Users\gopic\campaign_bert_model\campaign_bert_model\label_encoder.pkl"
|
| 14 |
|
| 15 |
+
# β
Load tokenizer, model, and label encoder
|
| 16 |
try:
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False)
|
| 18 |
model = AutoModelForSequenceClassification.from_pretrained(model_dir)
|
| 19 |
+
label_encoder = joblib.load(label_path)
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
raise RuntimeError(f"β Failed to load model or tokenizer: {e}")
|
| 22 |
|
| 23 |
+
# β
Define prediction function
|
| 24 |
def predict(client_interest, sentiment):
|
| 25 |
try:
|
| 26 |
text = f"{client_interest} Sentiment: {sentiment}"
|
| 27 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**inputs)
|
| 30 |
+
logits = outputs.logits
|
| 31 |
+
prediction = torch.argmax(logits, dim=1).item()
|
| 32 |
+
template = label_encoder.inverse_transform([prediction])[0]
|
| 33 |
+
confidence = torch.softmax(logits, dim=1)[0][prediction].item()
|
| 34 |
+
return f"π Suggested Template: {template}\nπ Confidence: {confidence:.2%}"
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"β Error in prediction: {e}"
|
| 37 |
+
|
| 38 |
+
# β
Build Gradio UI
|
| 39 |
+
with gr.Blocks(css=".gradio-container {background: #f0f4f8;}") as demo:
|
| 40 |
+
gr.Markdown("<h1 style='text-align: center; color: #1f2937;'>π’ Campaign Personalizer</h1>")
|
| 41 |
with gr.Row():
|
| 42 |
+
client_interest = gr.Textbox(label="Client Interest", placeholder="e.g. Term Insurance for family", lines=2)
|
| 43 |
+
sentiment = gr.Textbox(label="Sentiment", placeholder="e.g. Positive", lines=2)
|
| 44 |
+
predict_btn = gr.Button("π Predict Template")
|
| 45 |
+
output = gr.Textbox(label="Prediction Result")
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
predict_btn.click(fn=predict, inputs=[client_interest, sentiment], outputs=output)
|
| 48 |
|
| 49 |
+
# β
Launch
|
| 50 |
if __name__ == "__main__":
|
| 51 |
demo.launch()
|
|
|