import gradio as gr from transformers import pipeline import torch # Global variable to cache the model _classifier = None def load_model(): """Load the intent classification model""" global _classifier if _classifier is None: model_name = "YosefA/adfluence-intent-model" # Try multiple approaches to load the model loading_strategies = [ { "name": "Standard loading with trust_remote_code", "kwargs": {"trust_remote_code": True, "return_all_scores": True} }, { "name": "Loading with revision='main'", "kwargs": {"revision": "main", "return_all_scores": True} }, { "name": "Loading with use_fast=False", "kwargs": {"use_fast": False, "return_all_scores": True} }, { "name": "Loading with legacy tokenizer", "kwargs": {"use_fast": False, "trust_remote_code": True, "return_all_scores": True} } ] for strategy in loading_strategies: try: print(f"Trying: {strategy['name']}") _classifier = pipeline( "text-classification", model=model_name, **strategy['kwargs'] ) print(f"✅ Model loaded successfully using: {strategy['name']}") return _classifier except Exception as e: print(f"❌ Failed with {strategy['name']}: {e}") continue print("❌ All loading strategies failed") return None return _classifier def classify_intent(comment): """ Classify the intent of a comment Args: comment (str): The input comment text Returns: dict: Classification results with labels and scores """ if not comment.strip(): return "Please enter a comment to classify." classifier = load_model() if classifier is None: return "Error: Could not load the model. Please try again later." try: # Get predictions results = classifier(comment) # Format results for display formatted_results = [] for result in results: for item in result: label = item['label'] score = item['score'] formatted_results.append(f"{label}: {score:.4f} ({score*100:.2f}%)") return "\n".join(formatted_results) except Exception as e: return f"Error during classification: {str(e)}" # Create the Gradio interface with gr.Blocks(title="Ad Comments Intent Classifier") as demo: gr.Markdown(""" # 🎯 Ad Comments Intent Classifier This app classifies the intent of comments related to advertisements using the **YosefA/adfluence-intent-model**. Simply enter a comment below and get the classification results with confidence scores. """) with gr.Row(): with gr.Column(): comment_input = gr.Textbox( label="Comment Text", placeholder="Enter your comment here...", lines=3, max_lines=10 ) classify_btn = gr.Button("🔍 Classify Intent", variant="primary") with gr.Column(): output = gr.Textbox( label="Classification Results", lines=5, max_lines=10, interactive=False ) # Example inputs gr.Examples( examples=[ ["This product looks amazing! Where can I buy it?"], ["This is clearly a scam, don't trust it."], ["I love this brand, they make quality products."], ["The price seems too high for what you get."], ["Has anyone tried this? I'm curious about reviews."] ], inputs=comment_input, label="📝 Example Comments" ) # Set up the event handlers classify_btn.click( fn=classify_intent, inputs=comment_input, outputs=output ) comment_input.submit( fn=classify_intent, inputs=comment_input, outputs=output ) # Launch the app if __name__ == "__main__": demo.launch()