gopichandra commited on
Commit
4835f74
Β·
verified Β·
1 Parent(s): fbe5a56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
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
- # βœ… 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()
 
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()