Spaces:
Sleeping
Sleeping
| 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) |