gopichandra commited on
Commit
fbe5a56
Β·
verified Β·
1 Parent(s): 65ce341

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -38
app.py CHANGED
@@ -1,51 +1,44 @@
1
- import os
2
  import gradio as gr
3
- import joblib
4
  import torch
 
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
 
7
- # βœ… 1. Define your model directory
8
- model_repo = os.path.abspath("./campaign_bert_model/campaign_bert_model")
9
 
10
- # βœ… 2. Load tokenizer and model
11
  tokenizer = AutoTokenizer.from_pretrained(model_repo, use_fast=False)
12
  model = AutoModelForSequenceClassification.from_pretrained(model_repo)
13
 
14
- # βœ… 3. Load label encoder
15
  label_encoder = joblib.load("campaign_bert_model/label_encoder.pkl")
16
 
17
- # βœ… 4. Define prediction function
18
- def predict(client_interest, sentiment):
19
- try:
20
- text = f"{client_interest} Sentiment: {sentiment}"
21
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
22
- with torch.no_grad():
23
- outputs = model(**inputs)
24
- probs = torch.nn.functional.softmax(outputs.logits, dim=1)
25
- pred_idx = torch.argmax(probs, dim=1).item()
26
- pred_label = label_encoder.inverse_transform([pred_idx])[0]
27
- confidence = probs[0][pred_idx].item()
28
-
29
- return f"""
30
- ### 🎯 Predicted Template ID:
31
- **🟒 {pred_label}**
32
-
33
- ### πŸ“Š Confidence:
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
- client_interest = gr.Textbox(label="πŸ“ Client Interest", placeholder="e.g., Interested in term insurance")
44
- sentiment = gr.Textbox(label="😊 Sentiment", placeholder="e.g., positive / negative / neutral")
45
- submit = gr.Button("πŸ” Predict Template")
46
  output = gr.Markdown()
47
- submit.click(fn=predict, inputs=[client_interest, sentiment], outputs=output)
48
 
49
- # βœ… 6. Launch the app
50
- if __name__ == "__main__":
51
- demo.launch()
 
 
 
 
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()