File size: 2,523 Bytes
b8ae42e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
"""
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