File size: 2,600 Bytes
a32dacf
 
 
 
 
 
 
 
 
1ef2c6a
 
5379e57
 
 
fd0d231
5379e57
9c3e3cf
5379e57
 
 
 
 
 
 
 
 
 
 
9c3e3cf
5379e57
1ef2c6a
5379e57
 
9c3e3cf
5379e57
 
 
 
 
 
 
 
 
 
 
9c3e3cf
 
 
 
 
69a70da
5379e57
 
 
 
 
9c3e3cf
 
 
5379e57
 
 
 
 
 
 
 
 
 
 
9c3e3cf
fd0d231
5379e57
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
# Check for PyTorch installation
try:
    import torch
    print(f"PyTorch version: {torch.__version__}")
except ImportError:
    print("PyTorch is not installed. Please install PyTorch to run this script.")
    raise

from transformers import pipeline
import gradio as gr

# Initialize models as None
model1 = None
model2 = None

# Attempt to load the models and run test predictions
try:
    model1_name = "JimminDev/jim-text-class"
    model2_name = "JimminDev/Depressive-detector"
    print("Loading models...")
    
    model1 = pipeline("text-classification", model=model1_name)
    test_output1 = model1("Testing the first model with a simple sentence.")
    print("Model 1 test output:", test_output1)
    
    model2 = pipeline("text-classification", model=model2_name)
    test_output2 = model2("Testing the second model with a simple sentence.")
    print("Model 2 test output:", test_output2)
except Exception as e:
    print(f"Failed to load or run models: {e}")

# Prediction function with model selection and error handling
def predict_sentiment(text, model_choice):
    try:
        if model_choice == "Model 1":
            if model1 is None:
                raise ValueError("Model 1 not loaded.")
            predictions = model1(text)
        elif model_choice == "Model 2":
            if model2 is None:
                raise ValueError("Model 2 not loaded.")
            predictions = model2(text)
        else:
            raise ValueError("Invalid model choice.")
        
        return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
    except Exception as e:
        return f"Error processing input: {e}"

# Define example inputs
examples = [
    ["I absolutely love this product! It has changed my life.", "Model 1"],
    ["This is the worst movie I have ever seen. Completely disappointing.", "Model 1"],
    ["I'm not sure how I feel about this new update. It has some good points, but also many drawbacks.", "Model 2"],
    ["The customer service was fantastic! Very helpful and polite.", "Model 2"],
    ["Honestly, this was quite a mediocre experience. Nothing special.", "Model 1"]
]

# Gradio interface setup
iface = gr.Interface(
    fn=predict_sentiment,
    title="Sentiment Analysis",
    description="Enter text to analyze sentiment. Powered by Hugging Face Transformers.",
    inputs=[
        gr.inputs.Textbox(lines=2, placeholder="Enter text here..."),
        gr.inputs.Radio(choices=["Model 1", "Model 2"], label="Select Model")
    ],
    outputs="text",
    examples=examples
)

if __name__ == "__main__":
    iface.launch()