File size: 1,262 Bytes
ebad87a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
import requests

GROQ_API_KEY = os.getenv("groq_api_key")
API_URL = "https://api.groq.ai/v1/text/analyze"

def detect_emotion_tone(text):
    if not text.strip():
        return "⚠ Please enter text.", None
    headers = {"Authorization": f"Bearer {groq_api_key}", "Content-Type": "application/json"}
    payload = {"text": text, "features": ["emotion", "tone"]}
    try:
        r = requests.post(API_URL, headers=headers, json=payload)
        r.raise_for_status()
        result = r.json()
        emotions = result.get("emotion", {})
        tones = result.get("tone", {})
        if not emotions and not tones:
            return "No analysis available", None
        return str(emotions), str(tones)
    except Exception as e:
        return f"Error: {e}", None

with gr.Blocks() as demo:
    gr.Markdown("# 🌍 Emotion & Tone Detector")
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(label="Enter text", lines=3)
            submit_btn = gr.Button("Detect")
        with gr.Column():
            top_output = gr.Textbox(label="Emotions")
            full_output = gr.Textbox(label="Tones")
    submit_btn.click(fn=detect_emotion_tone, inputs=input_text, outputs=[top_output, full_output])