entropy25's picture
Update app.py
b38933f verified
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
)