Spaces:
Runtime error
Runtime error
| """ | |
| Single Text Analysis Page for Vietnamese Sentiment Analysis | |
| """ | |
| import gradio as gr | |
| import time | |
| def create_single_analysis_page(app_instance): | |
| """Create the single text analysis tab""" | |
| def analyze_sentiment(text): | |
| """Analyze sentiment of a single text""" | |
| if not text.strip(): | |
| return "❌ Please enter some text to analyze." | |
| if not app_instance.model_loaded: | |
| return "❌ Model not loaded. Please refresh the page." | |
| try: | |
| sentiment, output_text = app_instance.predict_sentiment(text.strip()) | |
| if sentiment: | |
| return output_text | |
| else: | |
| return "❌ Analysis failed. Please try again." | |
| except Exception as e: | |
| app_instance.cleanup_memory() | |
| return f"❌ Error during analysis: {str(e)}" | |
| # Single Text Analysis Tab | |
| with gr.Tab("📝 Single Text Analysis"): | |
| gr.Markdown("# 🎭 Vietnamese Sentiment Analysis") | |
| gr.Markdown("Enter Vietnamese text to analyze sentiment using a transformer model from Hugging Face.") | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| text_input = gr.Textbox( | |
| label="Enter Vietnamese Text", | |
| placeholder="Nhập văn bản tiếng Việt để phân tích cảm xúc...", | |
| lines=4, | |
| max_lines=10 | |
| ) | |
| with gr.Row(): | |
| analyze_btn = gr.Button("🔍 Analyze Sentiment", variant="primary") | |
| clear_btn = gr.Button("🗑️ Clear", variant="secondary") | |
| with gr.Column(scale=2): | |
| result_output = gr.Markdown(label="Analysis Result", visible=True) | |
| # Example texts | |
| examples = [ | |
| "Giảng viên dạy rất hay và tâm huyết.", | |
| "Khóa học này không tốt lắm.", | |
| "Cơ sở vật chất bình thường.", | |
| "Học phí quá cao.", | |
| "Nội dung giảng dạy rất hữu ích." | |
| ] | |
| gr.Examples( | |
| examples=examples, | |
| inputs=[text_input], | |
| label="Example Texts" | |
| ) | |
| # Connect events | |
| analyze_btn.click( | |
| fn=analyze_sentiment, | |
| inputs=[text_input], | |
| outputs=[result_output] | |
| ) | |
| clear_btn.click( | |
| fn=lambda: "", | |
| outputs=[text_input] | |
| ) | |
| return analyze_btn, clear_btn, text_input, result_output |