Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
import torch
|
| 4 |
-
import joblib
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 6 |
|
| 7 |
-
# β
|
| 8 |
-
model_repo = "
|
| 9 |
|
| 10 |
# β
Load tokenizer and model
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_repo, use_fast=False)
|
| 12 |
model = AutoModelForSequenceClassification.from_pretrained(model_repo)
|
| 13 |
|
| 14 |
# β
Load label encoder
|
| 15 |
-
label_encoder = joblib.load("
|
| 16 |
|
| 17 |
-
# β
|
| 18 |
-
def
|
| 19 |
text = f"{client_interest} Sentiment: {sentiment}"
|
| 20 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 21 |
-
|
| 22 |
with torch.no_grad():
|
| 23 |
outputs = model(**inputs)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return f"
|
| 29 |
|
| 30 |
# β
Gradio UI
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import joblib
|
| 5 |
|
| 6 |
+
# β
Set your model repo correctly β it should match your uploaded folder name in the Space
|
| 7 |
+
model_repo = "./campaign-bert-model" # DO NOT use './' if it's confusing HF. Use just 'campaign-bert-model' if needed
|
| 8 |
|
| 9 |
# β
Load tokenizer and model
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_repo, use_fast=False)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(model_repo)
|
| 12 |
|
| 13 |
# β
Load label encoder
|
| 14 |
+
label_encoder = joblib.load("label_encoder.pkl")
|
| 15 |
|
| 16 |
+
# β
Inference function
|
| 17 |
+
def predict(client_interest, sentiment):
|
| 18 |
text = f"{client_interest} Sentiment: {sentiment}"
|
| 19 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = model(**inputs)
|
| 22 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 23 |
+
pred = torch.argmax(probs, dim=1).item()
|
| 24 |
+
label = label_encoder.inverse_transform([pred])[0]
|
| 25 |
+
confidence = probs[0][pred].item() * 100
|
| 26 |
+
return f"π© Recommended Template: {label} (Confidence: {confidence:.2f}%)"
|
| 27 |
|
| 28 |
# β
Gradio UI
|
| 29 |
+
ui = gr.Interface(
|
| 30 |
+
fn=predict,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Textbox(label="Client Interest"),
|
| 33 |
+
gr.Textbox(label="Sentiment")
|
| 34 |
+
],
|
| 35 |
+
outputs=gr.Textbox(label="Predicted Template"),
|
| 36 |
+
title="π§ Campaign Personalizer",
|
| 37 |
+
theme="soft",
|
| 38 |
+
description="Enter client interest and sentiment to get the best template suggestion."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
ui.launch()
|
|
|