File size: 2,650 Bytes
3dd9c1a
c52adb8
4605533
 
0065a7e
 
 
 
 
e66f4f9
4605533
e66f4f9
 
 
 
 
 
 
 
 
4605533
 
 
 
 
 
e66f4f9
 
 
 
 
 
 
4605533
 
 
3dd9c1a
 
 
 
 
 
 
 
 
4605533
e66f4f9
4605533
 
e66f4f9
 
 
 
 
 
 
 
 
 
 
4605533
 
 
 
 
e66f4f9
4605533
e66f4f9
 
 
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
from simple_sentiment import SimpleSentimentTool

# Create an instance of the tool without preloading to avoid startup errors
sentiment_tool = SimpleSentimentTool(default_model="distilbert", preload=False)

# Launch the Gradio interface
if __name__ == "__main__":
    import gradio as gr
    
    with gr.Blocks(title="Sentiment Analysis Tool") as demo:
        gr.Markdown("# Multi-Model Sentiment Analysis Tool")
        
        with gr.Row():
            with gr.Column():
                text_input = gr.Textbox(
                    label="Enter text to analyze", 
                    placeholder="Type your text here...",
                    lines=5
                )
                
                model_dropdown = gr.Dropdown(
                    choices=list(sentiment_tool.models.keys()),
                    value=sentiment_tool.default_model,
                    label="Select Model"
                )
                
                with gr.Row():
                    analyze_btn = gr.Button("Analyze Sentiment")
                    clear_btn = gr.Button("Clear")
            
            with gr.Column():
                output = gr.JSON(label="Sentiment Analysis Results")
        
        def analyze_with_model(text, model_key):
            """Call the tool's forward method directly with appropriate parameters."""
            if not text:
                return "{\"error\": \"Please enter some text to analyze\"}"
            # The tool returns a JSON string now
            json_str = sentiment_tool.forward(text, model_key)
            # But we need to parse it for the Gradio JSON component
            import json
            try:
                return json.loads(json_str)
            except:
                return {"error": "Failed to parse results"}
        
        analyze_btn.click(
            fn=analyze_with_model,
            inputs=[text_input, model_dropdown],
            outputs=output
        )
        
        clear_btn.click(
            fn=lambda: ("", None),
            inputs=None,
            outputs=[text_input, output]
        )
        
        gr.Examples(
            examples=[
                ["I love this product! It's amazing and works perfectly.", "distilbert"],
                ["This movie was terrible. I was very disappointed.", "distilbert"],
                ["The service was okay, but could be improved in several ways.", "distilbert"],
                ["Ce produit est vraiment excellent!", "multilingual"],
                ["Dieses Buch ist sehr interessant.", "german"]
            ],
            inputs=[text_input, model_dropdown]
        )
    
    demo.launch(share=True)