File size: 1,682 Bytes
c3ada6b
2543d6d
c3ada6b
2543d6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ada6b
 
 
2543d6d
c3ada6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import joblib

# Define the custom pipeline
class CustomSVMTextClassificationPipeline:
    def __init__(self, model_path, vectorizer_path):
        # Load the model and vectorizer
        self.model = joblib.load(model_path)
        self.vectorizer = joblib.load(vectorizer_path)
    
    def __call__(self, texts):
        if isinstance(texts, str):
            texts = [texts]  # Ensure input is a list

        # Preprocess input using the vectorizer
        preprocessed_texts = self.vectorizer.transform(texts)

        # Predict using the model
        predictions = self.model.predict(preprocessed_texts)

        # Convert predictions into a readable format
        results = [{"text": text, "predictions": list(pred)} for text, pred in zip(texts, predictions)]
        return results

# Load the model and vectorizer
model_path = "svm_multi_output_model.pkl"  # Replace with your model file path
vectorizer_path = "tfidf_vectorizer.pkl"  # Replace with your vectorizer file path
classifier = CustomSVMTextClassificationPipeline(model_path, vectorizer_path)

def classify_text(input_text):
    """
    Classify the input text using the custom pipeline.
    """
    results = classifier(input_text)
    return results

# Create the Gradio interface
with gr.Blocks() as app:
    gr.Markdown("# Text Classification App")
    gr.Markdown("Enter text to classify:")
    
    input_text = gr.Textbox(label="Input Text")
    output = gr.JSON(label="Classification Results")
    submit_button = gr.Button("Classify")
    
    submit_button.click(classify_text, inputs=[input_text], outputs=[output])

# Launch the app
if __name__ == "__main__":
    app.launch()