Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,93 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Get
|
| 7 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
PRIVATE_SPACE = "entropy25/
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
try:
|
| 25 |
-
result =
|
|
|
|
| 26 |
return result
|
| 27 |
except Exception as e:
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
-
demo
|
| 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 |
+
)
|