File size: 2,805 Bytes
02bffa3
 
b38933f
 
 
 
02bffa3
b38933f
b3d6596
b38933f
 
b3d6596
b38933f
 
8229863
b38933f
 
 
 
 
 
 
 
 
90e05cc
b38933f
 
 
 
 
 
 
 
 
 
8229863
 
b38933f
 
8229863
 
b38933f
 
 
d062f91
b38933f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8229863
 
b38933f
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import os
import logging

# Enable logging
logging.basicConfig(level=logging.INFO)

# Get the token
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
    raise ValueError("❌ HF_TOKEN environment variable is not set")

# Define the private Space path
PRIVATE_SPACE = "entropy25/private"

def load_private_space():
    """Load private space via token"""
    try:
        private_fn = gr.load(f"spaces/{PRIVATE_SPACE}", hf_token=hf_token)
        logging.info("βœ… Successfully connected to private space")
        return private_fn
    except Exception as e:
        logging.error(f"❌ Connection failed: {e}")
        return None

# Load once during startup
private_analyzer = load_private_space()

def analyze_sentiment(text):
    """Call the private space for sentiment analysis"""
    if not private_analyzer:
        return "❌ Error: Failed to connect to private space"
    
    if not text.strip():
        return "⚠️ Please enter text to analyze"
    
    try:
        result = private_analyzer(text)
        logging.info(f"βœ… Analysis result: {result}")
        return result
    except Exception as e:
        error_msg = f"❌ Analysis failed: {str(e)}"
        logging.error(error_msg)
        return error_msg

def create_interface():
    """Build the public-facing UI"""
    with gr.Blocks(title="Sentiment Analyzer - Public", theme=gr.themes.Soft()) as demo:
        gr.Markdown("# 🎭 Sentiment Analysis Tool")
        gr.Markdown("Call a private Hugging Face space for sentiment analysis")
        
        with gr.Row():
            with gr.Column(scale=2):
                text_input = gr.Textbox(
                    label="πŸ“ Input Text",
                    placeholder="Type a sentence to analyze...",
                    lines=3,
                    max_lines=10
                )
                analyze_btn = gr.Button("πŸ” Analyze Sentiment", variant="primary")
            
            with gr.Column(scale=2):
                result_output = gr.Textbox(
                    label="πŸ“Š Analysis Result",
                    lines=5,
                    max_lines=10
                )
        
        analyze_btn.click(
            fn=analyze_sentiment,
            inputs=text_input,
            outputs=result_output
        )
        
        gr.Examples(
            examples=[
                ["I'm feeling great today!"],
                ["That movie was a waste of time."],
                ["The weather is nice, perfect for a walk."],
                ["Service was okay, but the quality was mediocre."]
            ],
            inputs=text_input
        )
    
    return demo

if __name__ == "__main__":
    demo = create_interface()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )