Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,94 +10,86 @@ API_KEY = os.getenv("API_KEY")
|
|
| 10 |
HF_MODEL_URL = "https://api-inference.huggingface.co/models/tabularisai/multilingual-sentiment-analysis"
|
| 11 |
|
| 12 |
|
| 13 |
-
def analyze_post_gradio(post):
|
| 14 |
-
"""
|
| 15 |
-
Gradio version of the analyze_post function
|
| 16 |
-
"""
|
| 17 |
try:
|
|
|
|
|
|
|
|
|
|
| 18 |
# Clean the text
|
| 19 |
cleaned_post = clean_text(post)
|
| 20 |
-
|
| 21 |
# Get sentiment analysis
|
| 22 |
sentiment_result = query_hf_sentiment(cleaned_post, API_KEY, HF_MODEL_URL)
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Extract themes
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"post": post,
|
| 31 |
-
"cleaned_post": cleaned_post,
|
| 32 |
-
"sentiment": {
|
| 33 |
-
"label": sentiment['label'],
|
| 34 |
-
"score": sentiment['score']
|
| 35 |
-
},
|
| 36 |
-
"theme": themes
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
# Store in MongoDB
|
| 40 |
-
#insert_post(data)
|
| 41 |
-
|
| 42 |
-
# Format output for display
|
| 43 |
output = f"""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
return output
|
| 58 |
-
|
| 59 |
except Exception as e:
|
| 60 |
-
return f"β Error analyzing post
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
with gr.Blocks(title="Post Analyzer", theme=gr.themes.Soft()) as demo:
|
| 64 |
gr.Markdown("# π Post Analysis Tool")
|
| 65 |
-
gr.Markdown("Enter a post to analyze its sentiment and extract themes")
|
| 66 |
-
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column():
|
| 69 |
post_input = gr.Textbox(
|
| 70 |
label="Enter your post",
|
| 71 |
placeholder="Type your post here...",
|
| 72 |
lines=4,
|
| 73 |
-
max_lines=10
|
| 74 |
)
|
| 75 |
-
analyze_btn = gr.Button("Analyze Post", variant="primary")
|
| 76 |
-
|
| 77 |
with gr.Column():
|
| 78 |
-
output = gr.Markdown(
|
| 79 |
-
|
| 80 |
-
# Set up the interaction
|
| 81 |
analyze_btn.click(
|
| 82 |
fn=analyze_post_gradio,
|
| 83 |
inputs=post_input,
|
| 84 |
-
outputs=output
|
| 85 |
)
|
| 86 |
-
|
| 87 |
-
# Add examples
|
| 88 |
gr.Examples(
|
| 89 |
examples=[
|
| 90 |
"I absolutely love this product! It's amazing and works perfectly.",
|
| 91 |
"I'm really disappointed with the service. It was slow and unhelpful.",
|
| 92 |
-
"The weather today is nice, but I'm concerned about climate change."
|
| 93 |
],
|
| 94 |
-
inputs=post_input
|
| 95 |
)
|
| 96 |
|
| 97 |
-
# Launch
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
demo.launch(
|
| 100 |
-
server_name="0.0.0.0",
|
| 101 |
-
server_port=7860,
|
| 102 |
-
share=True
|
| 103 |
-
)
|
|
|
|
| 10 |
HF_MODEL_URL = "https://api-inference.huggingface.co/models/tabularisai/multilingual-sentiment-analysis"
|
| 11 |
|
| 12 |
|
| 13 |
+
def analyze_post_gradio(post: str):
|
| 14 |
+
"""Gradio version of the analyze_post function."""
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
+
if not post or not post.strip():
|
| 17 |
+
return "β οΈ Please enter a valid post before analyzing."
|
| 18 |
+
|
| 19 |
# Clean the text
|
| 20 |
cleaned_post = clean_text(post)
|
| 21 |
+
|
| 22 |
# Get sentiment analysis
|
| 23 |
sentiment_result = query_hf_sentiment(cleaned_post, API_KEY, HF_MODEL_URL)
|
| 24 |
+
|
| 25 |
+
# Handle possible formats of HF output
|
| 26 |
+
if isinstance(sentiment_result, dict) and "raw_output" in sentiment_result:
|
| 27 |
+
sentiment = sentiment_result["raw_output"][0]
|
| 28 |
+
elif isinstance(sentiment_result, list) and len(sentiment_result) > 0:
|
| 29 |
+
sentiment = sentiment_result[0]
|
| 30 |
+
else:
|
| 31 |
+
sentiment = {"label": "Unknown", "score": 0.0}
|
| 32 |
+
|
| 33 |
# Extract themes
|
| 34 |
+
openai_client = create_openai_client(API_KEY)
|
| 35 |
+
themes = extract_themes(cleaned_post, openai_client)
|
| 36 |
+
|
| 37 |
+
# Prepare data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
output = f"""
|
| 39 |
+
### π§Ύ **Original Post**
|
| 40 |
+
{post}
|
| 41 |
+
|
| 42 |
+
### π§Ή **Cleaned Post**
|
| 43 |
+
{cleaned_post}
|
| 44 |
+
|
| 45 |
+
### π¬ **Sentiment Analysis**
|
| 46 |
+
- **Label:** {sentiment.get('label', 'Unknown')}
|
| 47 |
+
- **Score:** {sentiment.get('score', 0.0):.4f}
|
| 48 |
+
|
| 49 |
+
### π·οΈ **Extracted Themes**
|
| 50 |
+
{themes}
|
| 51 |
+
|
| 52 |
+
β
**Status:** Post analyzed successfully.
|
| 53 |
+
"""
|
| 54 |
return output
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"β **Error analyzing post:** {str(e)}"
|
| 58 |
|
| 59 |
+
|
| 60 |
+
# --- Gradio Interface ---
|
| 61 |
with gr.Blocks(title="Post Analyzer", theme=gr.themes.Soft()) as demo:
|
| 62 |
gr.Markdown("# π Post Analysis Tool")
|
| 63 |
+
gr.Markdown("Enter a post to analyze its sentiment and extract key themes using AI models.")
|
| 64 |
+
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column():
|
| 67 |
post_input = gr.Textbox(
|
| 68 |
label="Enter your post",
|
| 69 |
placeholder="Type your post here...",
|
| 70 |
lines=4,
|
| 71 |
+
max_lines=10,
|
| 72 |
)
|
| 73 |
+
analyze_btn = gr.Button("π Analyze Post", variant="primary")
|
| 74 |
+
|
| 75 |
with gr.Column():
|
| 76 |
+
output = gr.Markdown("Results will appear here after analysis...")
|
| 77 |
+
|
|
|
|
| 78 |
analyze_btn.click(
|
| 79 |
fn=analyze_post_gradio,
|
| 80 |
inputs=post_input,
|
| 81 |
+
outputs=output,
|
| 82 |
)
|
| 83 |
+
|
|
|
|
| 84 |
gr.Examples(
|
| 85 |
examples=[
|
| 86 |
"I absolutely love this product! It's amazing and works perfectly.",
|
| 87 |
"I'm really disappointed with the service. It was slow and unhelpful.",
|
| 88 |
+
"The weather today is nice, but I'm concerned about climate change.",
|
| 89 |
],
|
| 90 |
+
inputs=post_input,
|
| 91 |
)
|
| 92 |
|
| 93 |
+
# --- Launch for Hugging Face Space ---
|
| 94 |
if __name__ == "__main__":
|
| 95 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|