File size: 6,345 Bytes
c28eb35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import gradio as gr
from inference import ContentClassifierInference
import os
import json
import logging

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize the model
try:
    model = ContentClassifierInference()
    model_initialized = True
    logger.info("Model initialized successfully")
except Exception as e:
    logger.error(f"Error initializing model: {e}")
    model_initialized = False

def classify_text(text):
    """Classify single text input"""
    if not model_initialized:
        return json.dumps({"error": "Model initialization failed"}, indent=2)
    
    if not text or not text.strip():
        return json.dumps({"error": "Please provide valid text input"}, indent=2)
    
    try:
        result = model.predict(text.strip())
        logger.info(f"Processed text classification: {result['threat_prediction']}")
        return json.dumps(result, indent=2)
    except Exception as e:
        logger.error(f"Classification error: {e}")
        return json.dumps({"error": str(e)}, indent=2)

def classify_batch(text):
    """Classify batch of texts (one per line)"""
    if not model_initialized:
        return json.dumps({"error": "Model initialization failed"}, indent=2)
    
    if not text or not text.strip():
        return json.dumps({"error": "Please provide valid text input"}, indent=2)
    
    try:
        # Split by newlines for batch processing
        texts = [t.strip() for t in text.split("\n") if t.strip()]
        if not texts:
            return json.dumps({"error": "No valid texts provided"}, indent=2)
            
        if len(texts) > 10:  # Limit batch size
            return json.dumps({"error": "Batch size limited to 10 texts"}, indent=2)
            
        results = model.predict_batch(texts)
        logger.info(f"Processed batch of {len(texts)} texts")
        return json.dumps(results, indent=2)
    except Exception as e:
        logger.error(f"Batch classification error: {e}")
        return json.dumps({"error": str(e)}, indent=2)

# API function for external use
def predict_api(text):
    """API endpoint for programmatic access"""
    if isinstance(text, list):
        return [json.loads(classify_text(t)) for t in text]
    else:
        return json.loads(classify_text(text))

# Create Gradio interface
with gr.Blocks(title="Content Classifier", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# πŸ” Content Classifier")
    gr.Markdown("""

    This tool classifies content as either **safe** or **unsafe** using an ONNX model.  

    Perfect for content moderation, safety checks, and automated text analysis.

    """)
    
    # Status indicator
    if model_initialized:
        gr.Markdown("βœ… **Model Status**: Ready")
    else:
        gr.Markdown("❌ **Model Status**: Failed to initialize")
    
    with gr.Tab("Single Text Classification"):
        with gr.Row():
            with gr.Column():
                text_input = gr.Textbox(
                    label="Enter text to classify", 
                    lines=5, 
                    placeholder="Type or paste your text here...",
                    max_lines=10
                )
                classify_btn = gr.Button("πŸ” Classify", variant="primary", size="lg")
                
                # Examples
                gr.Examples(
                    examples=[
                        ["This is a normal, safe piece of content."],
                        ["Hello, how are you doing today?"],
                        ["Example text for content classification"]
                    ],
                    inputs=text_input,
                    label="Try these examples:"
                )
                
            with gr.Column():
                result_output = gr.JSON(label="Classification Result", show_label=True)
        classify_btn.click(fn=classify_text, inputs=text_input, outputs=result_output)
    
    with gr.Tab("Batch Processing"):
        with gr.Row():
            with gr.Column():
                batch_input = gr.Textbox(
                    label="Enter multiple texts (one per line)", 
                    lines=10, 
                    placeholder="Text 1\nText 2\nText 3\n...(max 10 texts)",
                    max_lines=15
                )
                batch_btn = gr.Button("πŸ“‹ Process Batch", variant="primary", size="lg")
                
                gr.Markdown("**Note**: Maximum 10 texts per batch")
                
            with gr.Column():
                batch_output = gr.JSON(label="Batch Classification Results", show_label=True)
        
        batch_btn.click(fn=classify_batch, inputs=batch_input, outputs=batch_output)
    
    with gr.Tab("API Documentation"):
        gr.Markdown("""

        ## πŸ”Œ API Usage

        

        This Space can be used as an API endpoint for programmatic access.

        

        ### Single Text Classification

        ```python

        import requests

        

        url = "https://your-space-name.hf.space/predict"

        response = requests.post(url, json={"text": "Your content to classify"})

        result = response.json()

        ```

        

        ### Batch Processing

        ```python

        import requests

        

        url = "https://your-space-name.hf.space/predict"

        texts = ["Text 1", "Text 2", "Text 3"]

        response = requests.post(url, json={"text": texts})

        results = response.json()

        ```

        

        ### Response Format

        ```json

        {

          "is_threat": false,

          "final_confidence": 0.85,

          "threat_prediction": "safe",

          "onnx_prediction": {

            "safe": 0.85,

            "unsafe": 0.15

          },

          "models_used": ["onnx"],

          "raw_predictions": {...}

        }

        ```

        

        ### Using with curl

        ```bash

        curl -X POST https://your-space-name.hf.space/predict \\

          -H "Content-Type: application/json" \\

          -d '{"text": "Your content to classify"}'

        ```

        """)

# Launch the app
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)