Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -1,49 +1,48 @@ 
     | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
             
            import joblib
         
     | 
| 3 | 
         
            -
            from transformers import AutoTokenizer, AutoModelForSequenceClassification
         
     | 
| 4 | 
         
             
            import torch
         
     | 
| 
         | 
|
| 5 | 
         | 
| 6 | 
         
            -
            # β
  
     | 
| 7 | 
         
            -
            model_dir = " 
     | 
| 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("label_encoder.pkl")
         
     | 
| 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 | 
         
            -
            # β
 Prediction  
     | 
| 18 | 
         
             
            def predict(client_interest, sentiment):
         
     | 
| 19 | 
         
             
                try:
         
     | 
| 20 | 
         
             
                    text = f"{client_interest} Sentiment: {sentiment}"
         
     | 
| 21 | 
         
            -
                    inputs = tokenizer(text, return_tensors="pt",  
     | 
| 22 | 
         
             
                    outputs = model(**inputs)
         
     | 
| 23 | 
         
            -
                     
     | 
| 24 | 
         
            -
                     
     | 
| 25 | 
         
            -
                    confidence = torch.softmax(outputs.logits, dim=1)[0][ 
     | 
| 26 | 
         
            -
             
     | 
| 27 | 
         
            -
                    return f"π― Predicted Template: **{predicted_label}**\nβ
 Confidence: **{confidence:.2%}**"
         
     | 
| 28 | 
         
             
                except Exception as err:
         
     | 
| 29 | 
         
            -
                    return f"β  
     | 
| 30 | 
         | 
| 31 | 
         
            -
            # β
  
     | 
| 32 | 
         
             
            with gr.Blocks(theme=gr.themes.Soft()) as demo:
         
     | 
| 33 | 
         
            -
                gr.Markdown("##  
     | 
| 34 | 
         
            -
                gr.Markdown(" 
     | 
| 35 | 
         | 
| 36 | 
         
             
                with gr.Row():
         
     | 
| 37 | 
         
             
                    with gr.Column():
         
     | 
| 38 | 
         
            -
                        interest = gr.Textbox(label="π Client Interest" 
     | 
| 39 | 
         
            -
                        sentiment = gr.Textbox(label="π Sentiment" 
     | 
| 40 | 
         
            -
                         
     | 
| 41 | 
         
            -
             
     | 
| 42 | 
         
             
                    with gr.Column():
         
     | 
| 43 | 
         
            -
                         
     | 
| 44 | 
         | 
| 45 | 
         
            -
                 
     | 
| 46 | 
         | 
| 47 | 
         
            -
            # β
 Launch
         
     | 
| 48 | 
         
             
            if __name__ == "__main__":
         
     | 
| 49 | 
         
             
                demo.launch()
         
     | 
| 
         | 
| 
         | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
             
            import joblib
         
     | 
| 
         | 
|
| 3 | 
         
             
            import torch
         
     | 
| 4 | 
         
            +
            from transformers import AutoTokenizer, AutoModelForSequenceClassification
         
     | 
| 5 | 
         | 
| 6 | 
         
            +
            # β
 Use folder name directly (no ./)
         
     | 
| 7 | 
         
            +
            model_dir = "campaign-bert-model"
         
     | 
| 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("label_encoder.pkl")
         
     | 
| 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 | 
         
            +
            # β
 Prediction logic
         
     | 
| 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 | 
         
             
                    outputs = model(**inputs)
         
     | 
| 23 | 
         
            +
                    predicted_id = torch.argmax(outputs.logits, dim=1).item()
         
     | 
| 24 | 
         
            +
                    label = label_encoder.inverse_transform([predicted_id])[0]
         
     | 
| 25 | 
         
            +
                    confidence = torch.softmax(outputs.logits, dim=1)[0][predicted_id].item()
         
     | 
| 26 | 
         
            +
                    return f"π― Template: **{label}**\nπ Confidence: **{confidence:.2%}**"
         
     | 
| 
         | 
|
| 27 | 
         
             
                except Exception as err:
         
     | 
| 28 | 
         
            +
                    return f"β Prediction failed: {err}"
         
     | 
| 29 | 
         | 
| 30 | 
         
            +
            # β
 UI with styling
         
     | 
| 31 | 
         
             
            with gr.Blocks(theme=gr.themes.Soft()) as demo:
         
     | 
| 32 | 
         
            +
                gr.Markdown("## π― Campaign Personalizer")
         
     | 
| 33 | 
         
            +
                gr.Markdown("Provide interest and sentiment to get the best marketing template.")
         
     | 
| 34 | 
         | 
| 35 | 
         
             
                with gr.Row():
         
     | 
| 36 | 
         
             
                    with gr.Column():
         
     | 
| 37 | 
         
            +
                        interest = gr.Textbox(label="π Client Interest")
         
     | 
| 38 | 
         
            +
                        sentiment = gr.Textbox(label="π Sentiment")
         
     | 
| 39 | 
         
            +
                        submit = gr.Button("π Predict")
         
     | 
| 
         | 
|
| 40 | 
         
             
                    with gr.Column():
         
     | 
| 41 | 
         
            +
                        result = gr.Markdown(label="π― Output")
         
     | 
| 42 | 
         | 
| 43 | 
         
            +
                submit.click(predict, inputs=[interest, sentiment], outputs=result)
         
     | 
| 44 | 
         | 
| 45 | 
         
            +
            # β
 Launch app
         
     | 
| 46 | 
         
             
            if __name__ == "__main__":
         
     | 
| 47 | 
         
             
                demo.launch()
         
     | 
| 48 | 
         
            +
             
     |