Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,44 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
import torch
|
|
|
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
|
| 7 |
-
# β
|
| 8 |
-
model_repo =
|
| 9 |
|
| 10 |
-
# β
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_repo, use_fast=False)
|
| 12 |
model = AutoModelForSequenceClassification.from_pretrained(model_repo)
|
| 13 |
|
| 14 |
-
# β
|
| 15 |
label_encoder = joblib.load("campaign_bert_model/label_encoder.pkl")
|
| 16 |
|
| 17 |
-
# β
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
`{confidence * 100:.2f}%`
|
| 35 |
-
"""
|
| 36 |
-
except Exception as e:
|
| 37 |
-
return f"β Error: {str(e)}"
|
| 38 |
-
|
| 39 |
-
# β
5. Gradio UI
|
| 40 |
-
with gr.Blocks() as demo:
|
| 41 |
-
gr.Markdown("# π§ Campaign Personalizer AI\nPredict best template ID based on interest & sentiment.")
|
| 42 |
with gr.Row():
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
output = gr.Markdown()
|
| 47 |
-
submit.click(fn=predict, inputs=[client_interest, sentiment], outputs=output)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
import torch
|
| 4 |
+
import joblib
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
|
| 7 |
+
# β
Fixed: Use relative path (NOT absolute)
|
| 8 |
+
model_repo = "campaign_bert_model/campaign_bert_model"
|
| 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("campaign_bert_model/label_encoder.pkl")
|
| 16 |
|
| 17 |
+
# β
Prediction function
|
| 18 |
+
def predict_template(client_interest, sentiment):
|
| 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 |
+
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 25 |
+
|
| 26 |
+
predicted_template = label_encoder.inverse_transform([prediction])[0]
|
| 27 |
+
|
| 28 |
+
return f"π― Predicted Template: **{predicted_template}**"
|
| 29 |
+
|
| 30 |
+
# β
Gradio UI
|
| 31 |
+
with gr.Blocks(title="Campaign Personalizer") as demo:
|
| 32 |
+
gr.Markdown("# π§ Campaign Personalizer\nPredict the best template based on client interest and sentiment.")
|
| 33 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Row():
|
| 35 |
+
client_input = gr.Textbox(label="Client Interest")
|
| 36 |
+
sentiment_input = gr.Textbox(label="Sentiment (e.g., Positive, Neutral, Negative)")
|
| 37 |
+
|
| 38 |
output = gr.Markdown()
|
|
|
|
| 39 |
|
| 40 |
+
predict_button = gr.Button("π Predict Template")
|
| 41 |
+
predict_button.click(fn=predict_template, inputs=[client_input, sentiment_input], outputs=output)
|
| 42 |
+
|
| 43 |
+
# β
Launch
|
| 44 |
+
demo.launch()
|