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