Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,41 @@
|
|
1 |
-
import
|
2 |
import openai
|
3 |
import os
|
4 |
|
5 |
-
#
|
6 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
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 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
4 |
|
5 |
+
# Get OpenAI API key from environment
|
6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
+
|
8 |
+
def ai_calculator(num1, num2, operation):
|
9 |
+
if not openai.api_key:
|
10 |
+
return "❌ OpenAI API key not set."
|
11 |
+
|
12 |
+
prompt = f"""You are a smart calculator. Perform the following operation:
|
13 |
+
Input: {num1}, {num2}, {operation}
|
14 |
+
Output:"""
|
15 |
+
|
16 |
+
try:
|
17 |
+
response = openai.Completion.create(
|
18 |
+
engine="text-davinci-003",
|
19 |
+
prompt=prompt,
|
20 |
+
temperature=0,
|
21 |
+
max_tokens=50,
|
22 |
+
)
|
23 |
+
result = response.choices[0].text.strip()
|
24 |
+
return f"✅ Result: {result}"
|
25 |
+
except Exception as e:
|
26 |
+
return f"❌ Error: {str(e)}"
|
27 |
+
|
28 |
+
demo = gr.Interface(
|
29 |
+
fn=ai_calculator,
|
30 |
+
inputs=[
|
31 |
+
gr.Number(label="Enter First Number"),
|
32 |
+
gr.Number(label="Enter Second Number"),
|
33 |
+
gr.Radio(["add", "subtract", "multiply", "divide"], label="Operation"),
|
34 |
+
],
|
35 |
+
outputs="text",
|
36 |
+
title="🧠 AI Calculator",
|
37 |
+
description="A simple calculator using OpenAI GPT. Choose numbers and operation to get result.",
|
38 |
+
)
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
demo.launch()
|