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])