Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
# Define the sentiment analysis function that communicates with the Hugging Face model API | |
def analyze_sentiment(message: str): | |
try: | |
# Send request to the Hugging Face Space for sentiment analysis | |
response = requests.post( | |
'https://safwansajad-emotion-detection-gpt.hf.space/predict', | |
json={'text': message}, | |
headers={'Content-Type': 'application/json'} | |
) | |
# Extract the sentiment and score from the response | |
data = response.json() | |
# Return the label and score in the format expected by your app | |
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}] | |
# Set up Gradio interface | |
iface = gr.Interface( | |
fn=analyze_sentiment, # Function to be called | |
inputs=gr.Textbox(label="Enter your message", placeholder="How are you feeling today?", lines=2), # User input | |
outputs=gr.JSON(), # Output in JSON format (label and score) | |
live=True, # Enables live input processing | |
title="Sentiment Analysis with SerenityAI", # Title of the interface | |
description="Enter a message and get feedback about your emotional state. Your feelings matter!", | |
theme="huggingface", # Optionally set the theme to Hugging Face's style | |
) | |
# Launch the Gradio app | |
if __name__ == "__main__": | |
iface.launch(share=True) # share=True gives you a public link | |