Team Ai
Apppublic

netfusionai/python-arithmetic-calculator

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
app.py83 linesDownload Raw Back to root
1import gradio as gr2 3# ---------- Function Logic ----------4def calculate(n1, n2):5    try:6        n1, n2 = float(n1), float(n2)7    except ValueError:8        return "<span style='color:#ff4d4d;'>❌ Please enter valid numbers.</span>"9 10    result = []11    result.append(f"<b style='color:#00e676;'>➕ Sum:</b> <span style='color:white;'>{n1 + n2}</span>")12    result.append(f"<b style='color:#ffb74d;'>➖ Difference:</b> <span style='color:white;'>{n1 - n2}</span>")13    result.append(f"<b style='color:#64b5f6;'>✖️ Product:</b> <span style='color:white;'>{n1 * n2}</span>")14 15    if n2 != 0:16        result.append(f"<b style='color:#1de9b6;'>➗ Quotient:</b> <span style='color:white;'>{n1 / n2}</span>")17        result.append(f"<b style='color:#b388ff;'>🔹 Remainder:</b> <span style='color:white;'>{n1 % n2}</span>")18    else:19        result.append("<b style='color:#ff5252;'>⚠️ Division or modulus by zero not allowed.</b>")20 21    try:22        if abs(n2) > 100:23            result.append("<b style='color:#ffee58;'>⚠️ Power too large to calculate safely.</b>")24        else:25            result.append(f"<b style='color:#00e5ff;'>⚡ Power:</b> <span style='color:white;'>{n1 ** n2}</span>")26    except OverflowError:27        result.append("<b style='color:#ff9800;'>⚠️ Overflow error while calculating power.</b>")28 29    result.append("<hr><small style='color:#aaa;'>Made with ❤️ by NetFusionAI</small>")30    return "<br>".join(result)31 32 33# ---------- Custom CSS ----------34custom_css = """35body {background-color: #0f1115 !important; color: #f5f5f5;}36.gradio-container {max-width: 750px !important; margin: auto; text-align:center;}37h1.title {38    font-size: 2em !important;39    color: #00bfa6 !important;40    text-align:center;41    margin-top: 20px;42}43input[type=number] {44    background-color: #1e1e1e !important;45    color: #ffffff !important;46    border: 1px solid #333 !important;47    border-radius: 10px !important;48    padding: 10px !important;49    text-align: center;50}51button {52    background-color: #00bfa6 !important;53    color: white !important;54    border-radius: 10px !important;55    font-weight: bold !important;56    padding: 10px 20px !important;57}58.output-html {59    background-color: rgba(255,255,255,0.05) !important;60    border-radius: 12px;61    padding: 20px;62    font-size: 16px;63    text-align:left;64    color:white;65}66"""67 68 69# ---------- Gradio UI ----------70with gr.Blocks(css=custom_css, title="Python Arithmetic Calculator") as app:71    gr.HTML("<h1 class='title'>🧮 Python Arithmetic Calculator</h1>")72    n1 = gr.Number(label="Enter first number")73    n2 = gr.Number(label="Enter second number")74    btn = gr.Button("Calculate")75    output = gr.HTML()76    btn.click(calculate, inputs=[n1, n2], outputs=output)77    gr.HTML("<p style='font-size:12px;color:#888;margin-top:15px;'>⚡ Built using Gradio + HuggingFace Spaces</p>")78 79 80# ---------- Launch ----------81if __name__ == "__main__":82    app.launch()83