entropy25 commited on
Commit
b38933f
Β·
verified Β·
1 Parent(s): 0984dc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -27
app.py CHANGED
@@ -1,41 +1,93 @@
1
-
2
-
3
  import gradio as gr
4
  import os
 
 
 
 
5
 
6
- # Get token from environment variable
7
  hf_token = os.getenv("HF_TOKEN")
 
 
8
 
9
- # Replace with your private Space name
10
- PRIVATE_SPACE = "entropy25/sentiment-analyzer-docker2"
11
 
12
- # Load the private Space as a function
13
- try:
14
- private_fn = gr.load(f"spaces/{PRIVATE_SPACE}", hf_token=hf_token)
15
- print("Successfully connected to private Space")
16
- except Exception as e:
17
- print(f"Connection failed: {e}")
18
- private_fn = None
 
 
19
 
20
- def process_input(user_input):
21
- if not private_fn:
22
- return "Error: Unable to connect to private Space. Please check the token and space name."
 
 
 
 
 
 
 
23
 
24
  try:
25
- result = private_fn(user_input)
 
26
  return result
27
  except Exception as e:
28
- return f"Processing error: {str(e)}"
 
 
29
 
30
- # Create Gradio interface
31
- demo = gr.Interface(
32
- fn=process_input,
33
- inputs=gr.Textbox(label="Input", placeholder="Enter your content..."),
34
- outputs=gr.Textbox(label="Output"),
35
- title="Call Private Space",
36
- description="Call functions from a private Hugging Face Space via this public interface"
37
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  if __name__ == "__main__":
40
- demo.launch()
41
-
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
+ import logging
4
+
5
+ # Enable logging
6
+ logging.basicConfig(level=logging.INFO)
7
 
8
+ # Get the token
9
  hf_token = os.getenv("HF_TOKEN")
10
+ if not hf_token:
11
+ raise ValueError("❌ HF_TOKEN environment variable is not set")
12
 
13
+ # Define the private Space path
14
+ PRIVATE_SPACE = "entropy25/private"
15
 
16
+ def load_private_space():
17
+ """Load private space via token"""
18
+ try:
19
+ private_fn = gr.load(f"spaces/{PRIVATE_SPACE}", hf_token=hf_token)
20
+ logging.info("βœ… Successfully connected to private space")
21
+ return private_fn
22
+ except Exception as e:
23
+ logging.error(f"❌ Connection failed: {e}")
24
+ return None
25
 
26
+ # Load once during startup
27
+ private_analyzer = load_private_space()
28
+
29
+ def analyze_sentiment(text):
30
+ """Call the private space for sentiment analysis"""
31
+ if not private_analyzer:
32
+ return "❌ Error: Failed to connect to private space"
33
+
34
+ if not text.strip():
35
+ return "⚠️ Please enter text to analyze"
36
 
37
  try:
38
+ result = private_analyzer(text)
39
+ logging.info(f"βœ… Analysis result: {result}")
40
  return result
41
  except Exception as e:
42
+ error_msg = f"❌ Analysis failed: {str(e)}"
43
+ logging.error(error_msg)
44
+ return error_msg
45
 
46
+ def create_interface():
47
+ """Build the public-facing UI"""
48
+ with gr.Blocks(title="Sentiment Analyzer - Public", theme=gr.themes.Soft()) as demo:
49
+ gr.Markdown("# 🎭 Sentiment Analysis Tool")
50
+ gr.Markdown("Call a private Hugging Face space for sentiment analysis")
51
+
52
+ with gr.Row():
53
+ with gr.Column(scale=2):
54
+ text_input = gr.Textbox(
55
+ label="πŸ“ Input Text",
56
+ placeholder="Type a sentence to analyze...",
57
+ lines=3,
58
+ max_lines=10
59
+ )
60
+ analyze_btn = gr.Button("πŸ” Analyze Sentiment", variant="primary")
61
+
62
+ with gr.Column(scale=2):
63
+ result_output = gr.Textbox(
64
+ label="πŸ“Š Analysis Result",
65
+ lines=5,
66
+ max_lines=10
67
+ )
68
+
69
+ analyze_btn.click(
70
+ fn=analyze_sentiment,
71
+ inputs=text_input,
72
+ outputs=result_output
73
+ )
74
+
75
+ gr.Examples(
76
+ examples=[
77
+ ["I'm feeling great today!"],
78
+ ["That movie was a waste of time."],
79
+ ["The weather is nice, perfect for a walk."],
80
+ ["Service was okay, but the quality was mediocre."]
81
+ ],
82
+ inputs=text_input
83
+ )
84
+
85
+ return demo
86
 
87
  if __name__ == "__main__":
88
+ demo = create_interface()
89
+ demo.launch(
90
+ server_name="0.0.0.0",
91
+ server_port=7860,
92
+ share=False
93
+ )