Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,40 @@
|
|
1 |
import gradio as gr
|
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 |
-
with
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
clear_btn.click(lambda s_id: ([], "", 0.0) if s_id in chat_histories else ([], "", 0.0),
|
33 |
-
inputs=[session_id],
|
34 |
-
outputs=[chatbot, emotion_out, score_out])
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# For Hugging Face Spaces, Gradio must be the main interface
|
44 |
if __name__ == "__main__":
|
45 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Define the sentiment analysis function that communicates with the Hugging Face model API
|
5 |
+
def analyze_sentiment(message: str):
|
6 |
+
try:
|
7 |
+
# Send request to the Hugging Face Space for sentiment analysis
|
8 |
+
response = requests.post(
|
9 |
+
'https://safwansajad-emotion-detection-gpt.hf.space/predict',
|
10 |
+
json={'text': message},
|
11 |
+
headers={'Content-Type': 'application/json'}
|
12 |
+
)
|
13 |
+
|
14 |
+
# Extract the sentiment and score from the response
|
15 |
+
data = response.json()
|
16 |
+
|
17 |
+
# Return the label and score in the format expected by your app
|
18 |
+
if 'emotion' in data and 'score' in data:
|
19 |
+
return [{"label": data['emotion'], "score": data['score']}]
|
20 |
+
else:
|
21 |
+
return [{"label": "Unknown", "score": 0}]
|
22 |
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error during sentiment analysis: {e}")
|
25 |
+
return [{"label": "Error", "score": 0}]
|
26 |
+
|
27 |
+
# Set up Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=analyze_sentiment, # Function to be called
|
30 |
+
inputs=gr.Textbox(label="Enter your message", placeholder="How are you feeling today?", lines=2), # User input
|
31 |
+
outputs=gr.JSON(), # Output in JSON format (label and score)
|
32 |
+
live=True, # Enables live input processing
|
33 |
+
title="Sentiment Analysis with SerenityAI", # Title of the interface
|
34 |
+
description="Enter a message and get feedback about your emotional state. Your feelings matter!",
|
35 |
+
theme="huggingface", # Optionally set the theme to Hugging Face's style
|
36 |
+
)
|
37 |
+
|
38 |
+
# Launch the Gradio app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if __name__ == "__main__":
|
40 |
+
iface.launch(share=True) # share=True gives you a public link
|