File size: 3,653 Bytes
458a1a0 d0fc808 458a1a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import gradio as gr
import requests
import json
import os
import pandas as pd
BASE_URL = "https://api.jigsawstack.com/v1"
headers = {
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
}
def analyze_sentiment(text):
if not text or not text.strip():
return "Error: Text input is required.", None, None, None, None
try:
response = requests.post(
f"{BASE_URL}/ai/sentiment",
headers=headers,
json={"text": text.strip()}
)
response.raise_for_status()
result = response.json()
if not result.get("success"):
error_msg = f"Error: API call failed - {result.get('message', 'Unknown error')}"
return error_msg, None, None, None, None
sentiment_data = result.get("sentiment", {})
overall_emotion = sentiment_data.get("emotion", "N/A")
overall_sentiment = sentiment_data.get("sentiment", "N/A")
overall_score = sentiment_data.get("score", "N/A")
sentences = sentiment_data.get("sentences", [])
if sentences:
sentence_df = pd.DataFrame(sentences)
sentence_df = sentence_df[['text', 'emotion', 'sentiment', 'score']]
sentence_df.rename(columns={'text': 'Sentence', 'emotion': 'Emotion', 'sentiment': 'Sentiment', 'score': 'Score'}, inplace=True)
else:
sentence_df = pd.DataFrame(columns=['Sentence', 'Emotion', 'Sentiment', 'Score'])
status_message = "β
Sentiment analysis complete."
return status_message, overall_emotion, overall_sentiment, str(overall_score), sentence_df
except requests.exceptions.RequestException as e:
return f"Request failed: {str(e)}", None, None, None, None
except Exception as e:
return f"An unexpected error occurred: {str(e)}", None, None, None, None
with gr.Blocks() as demo:
gr.Markdown("""
<div style='text-align: center; margin-bottom: 24px;'>
<h1 style='font-size:2.2em; margin-bottom: 0.2em;'>π§© Analyze Sentiment</h1>
<p style='font-size:1.2em; margin-top: 0;'>Perform line-by-line sentiment analysis on any text with detailed emotion detection.</p>
<p style='font-size:1em; margin-top: 0.5em;'>For more details and API usage, see the <a href='https://jigsawstack.com/docs/api-reference/ai/sentiment' target='_blank'>documentation</a>.</p>
</div>
""")
with gr.Row():
with gr.Column():
gr.Markdown("#### Input Text")
sentiment_text = gr.Textbox(
label="Text to Analyze",
lines=8,
placeholder="Enter the text you want to analyze here..."
)
sentiment_btn = gr.Button("Analyze Sentiment", variant="primary")
with gr.Column():
gr.Markdown("#### Overall Analysis")
sentiment_status = gr.Textbox(label="Status", interactive=False)
sentiment_emotion = gr.Textbox(label="Overall Emotion", interactive=False)
sentiment_sentiment = gr.Textbox(label="Overall Sentiment", interactive=False)
sentiment_score = gr.Textbox(label="Overall Score", interactive=False)
gr.Markdown("#### Sentence-Level Breakdown")
sentiment_sentences_df = gr.DataFrame(label="Sentence Analysis")
sentiment_btn.click(
analyze_sentiment,
inputs=[sentiment_text],
outputs=[sentiment_status, sentiment_emotion, sentiment_sentiment, sentiment_score, sentiment_sentences_df]
)
demo.launch()
|