File size: 1,666 Bytes
fa4258c
d4280f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d5d5a4
d4280f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d4edac
ffa3f44
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
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