| | import gradio as gr |
| | import os |
| | import logging |
| |
|
| | |
| | logging.basicConfig(level=logging.INFO) |
| |
|
| | |
| | hf_token = os.getenv("HF_TOKEN") |
| | if not hf_token: |
| | raise ValueError("β HF_TOKEN environment variable is not set") |
| |
|
| | |
| | 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 |
| |
|
| | |
| | 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 |
| | ) |
| |
|