import gradio as gr import requests API_URL = "http://127.0.0.1:8000/api/predict" def analyze_text(text): try: response = requests.post(API_URL, json={"text": text}) if response.status_code == 200: result = response.json() sentiment = result.get("sentiment", "") emotion = result.get("emotion", "") return f"➡️ **Sentiment:** {sentiment}\n\n ➡️ **Emotion:** {emotion}" else: return f"API Error: {response.status_code}" except Exception as e: return f"Error connecting to API: {str(e)}" custom_css = """ .gradio-container { background: linear-gradient(135deg, #e9f1fc 0%, #fefefe 100%); font-family: 'Segoe UI', Roboto, sans-serif; } h1 { text-align: center !important; font-size: 2.2rem !important; color: #1f2937 !important; font-weight: 600 !important; } textarea, .output_text { font-size: 1.1rem !important; line-height: 1.6 !important; } .output_text { background: #f9fafb !important; border-radius: 12px !important; padding: 16px !important; border: 1px solid #e5e7eb !important; } """ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: gr.Markdown("# 💬 Emotion & Sentiment Analyzer") gr.Markdown("### Type your text below to discover its emotional tone and sentiment ✨") with gr.Row(): text_input = gr.Textbox( label="Enter text here", placeholder="e.g. I'm so excited to work on this project!", lines=4, scale=2 ) with gr.Row(): output_box = gr.Markdown(label="Results", elem_classes="output_text") analyze_button = gr.Button("🔍 Analyze", variant="primary") analyze_button.click(fn=analyze_text, inputs=text_input, outputs=output_box) demo.launch()