ai-calculator / app.py
asad231's picture
Update app.py
471a0fd verified
raw
history blame
1.15 kB
import gradio as gr
import openai
import os
# Get OpenAI API key from environment
openai.api_key = os.getenv("OPENAI_API_KEY")
def ai_calculator(num1, num2, operation):
if not openai.api_key:
return "❌ OpenAI API key not set."
prompt = f"""You are a smart calculator. Perform the following operation:
Input: {num1}, {num2}, {operation}
Output:"""
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0,
max_tokens=50,
)
result = response.choices[0].text.strip()
return f"βœ… Result: {result}"
except Exception as e:
return f"❌ Error: {str(e)}"
demo = gr.Interface(
fn=ai_calculator,
inputs=[
gr.Number(label="Enter First Number"),
gr.Number(label="Enter Second Number"),
gr.Radio(["add", "subtract", "multiply", "divide"], label="Operation"),
],
outputs="text",
title="🧠 AI Calculator",
description="A simple calculator using OpenAI GPT. Choose numbers and operation to get result.",
)
if __name__ == "__main__":
demo.launch()