import os import gradio as gr from transformers import pipeline # 1. Thread Optimization & GPU/CPU Configuration # Force PyTorch to use 1 thread for CPU inference to avoid excessive context switching in constrained envs import torch torch.set_num_threads(1) # Global lazy-loaded pipelines to minimize startup latency _sentiment_pipeline = None _zero_shot_pipeline = None def get_sentiment_pipeline(): global _sentiment_pipeline if _sentiment_pipeline is None: # Using distilbert-base-uncased-finetuned-sst-2-english # Extremely fast, highly accurate (~92% on SST-2), and highly efficient for production CPUs _sentiment_pipeline = pipeline( "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", revision="714eb0f" ) return _sentiment_pipeline def get_zero_shot_pipeline(): global _zero_shot_pipeline if _zero_shot_pipeline is None: # Using typeform/distilbert-base-uncased-mnli # Extremely lightweight (~268MB), offering BART-like zero-shot capability at a fraction of the memory footprint _zero_shot_pipeline = pipeline( "zero-shot-classification", model="typeform/distilbert-base-uncased-mnli" ) return _zero_shot_pipeline # 2. Main Logic Handlers def analyze_sentiment(text): if not text or not text.strip(): return {"Error": 1.0}, "Please enter valid text." try: classifier = get_sentiment_pipeline() results = classifier(text) # Format the output beautifully for Gradio's gr.Label # e.g., {"POSITIVE": 0.999, "NEGATIVE": 0.001} label = results[0]['label'] score = results[0]['score'] other_label = "NEGATIVE" if label == "POSITIVE" else "POSITIVE" output_data = { label: score, other_label: 1.0 - score } emoji = "✨" if label == "POSITIVE" else "⚠️" explanation = f"The model is **{score:.2%}** confident that this text has a **{label.lower()}** sentiment {emoji}." return output_data, explanation except Exception as e: return {"Error": 1.0}, f"Inference Error: {str(e)}" def classify_zero_shot(text, candidate_labels): if not text or not text.strip(): return {"Error": 1.0}, "Please enter valid text." if not candidate_labels or not candidate_labels.strip(): return {"Error": 1.0}, "Please enter at least one candidate label." try: # Clean and split the candidate labels labels_list = [label.strip() for label in candidate_labels.split(",") if label.strip()] if len(labels_list) == 0: return {"Error": 1.0}, "Please provide valid comma-separated labels." classifier = get_zero_shot_pipeline() results = classifier(text, candidate_labels=labels_list) # Gradio Label component expects a dict of {label_name: float_probability} output_data = {label: score for label, score in zip(results['labels'], results['scores'])} top_label = results['labels'][0] top_score = results['scores'][0] explanation = f"**Top Match**: classified as **'{top_label}'** with **{top_score:.1%}** confidence." return output_data, explanation except Exception as e: return {"Error": 1.0}, f"Inference Error: {str(e)}" # 3. Custom Theming & Premium Visual Layout # Sleek, enterprise-grade Slate and Sapphire design with clean edges theme = gr.themes.Soft( primary_hue="indigo", secondary_hue="blue", neutral_hue="slate", font=[gr.themes.GoogleFont("Inter"), "Helvetica Neue", "Arial", "sans-serif"] ).set( body_background_fill="*neutral_50", body_background_fill_dark="*neutral_950", button_primary_background_fill="linear-gradient(135deg, #4f46e5 0%, #3b82f6 100%)", button_primary_background_fill_hover="linear-gradient(135deg, #4338ca 0%, #2563eb 100%)", button_primary_text_color="white", block_title_text_weight="600", block_border_width="1px", block_shadow="rgba(0, 0, 0, 0.05) 0px 4px 12px" ) # Custom CSS for glassmorphism headers and beautiful margins custom_css = """ .header-container { background: linear-gradient(135deg, #1e1b4b 0%, #0f172a 100%); padding: 2.5rem; border-radius: 12px; margin-bottom: 2rem; color: white; box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.15), 0 8px 10px -6px rgba(0, 0, 0, 0.15); } .header-title { font-size: 2.5rem; font-weight: 800; letter-spacing: -0.025em; margin: 0; background: linear-gradient(to right, #a5b4fc, #818cf8, #60a5fa); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } .header-subtitle { font-size: 1.1rem; opacity: 0.85; margin-top: 0.5rem; } .footer-text { text-align: center; font-size: 0.85rem; color: #64748b; margin-top: 3rem; } """ with gr.Blocks(title="NexusCorp AI Suite") as demo: # 4. Premium Header Section gr.HTML( """
State-of-the-art NLP classification models optimized for instant pipeline inference