|
import gradio as gr |
|
import requests |
|
|
|
|
|
def analyze_sentiment(message: str): |
|
try: |
|
|
|
response = requests.post( |
|
'https://safwansajad-emotion-detection-gpt.hf.space/predict', |
|
json={'text': message}, |
|
headers={'Content-Type': 'application/json'} |
|
) |
|
|
|
|
|
data = response.json() |
|
|
|
|
|
if 'emotion' in data and 'score' in data: |
|
return [{"label": data['emotion'], "score": data['score']}] |
|
else: |
|
return [{"label": "Unknown", "score": 0}] |
|
|
|
except Exception as e: |
|
print(f"Error during sentiment analysis: {e}") |
|
return [{"label": "Error", "score": 0}] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_sentiment, |
|
inputs=gr.Textbox(label="Enter your message", placeholder="How are you feeling today?", lines=2), |
|
outputs=gr.JSON(), |
|
live=True, |
|
title="Sentiment Analysis with SerenityAI", |
|
description="Enter a message and get feedback about your emotional state. Your feelings matter!", |
|
theme="huggingface", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch(share=True) |