Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,27 @@ import gradio as gr
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
def ai_calculator(num1, num2, operation):
|
| 9 |
-
if not
|
| 10 |
return "❌ OpenAI API key not set."
|
| 11 |
|
| 12 |
-
prompt = f"
|
| 13 |
-
Input: {num1}, {num2}, {operation}
|
| 14 |
-
Output:"""
|
| 15 |
|
| 16 |
try:
|
| 17 |
-
response =
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
temperature=0,
|
| 21 |
-
max_tokens=50
|
| 22 |
)
|
| 23 |
-
result = response.choices[0].
|
| 24 |
return f"✅ Result: {result}"
|
| 25 |
except Exception as e:
|
| 26 |
return f"❌ Error: {str(e)}"
|
|
@@ -34,7 +36,7 @@ demo = gr.Interface(
|
|
| 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__":
|
|
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# OpenAI API setup
|
| 6 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
client = openai.OpenAI(api_key=api_key)
|
| 8 |
|
| 9 |
def ai_calculator(num1, num2, operation):
|
| 10 |
+
if not api_key:
|
| 11 |
return "❌ OpenAI API key not set."
|
| 12 |
|
| 13 |
+
prompt = f"Perform {operation} operation on {num1} and {num2}. Just return the result only."
|
|
|
|
|
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
+
response = client.chat.completions.create(
|
| 17 |
+
model="gpt-3.5-turbo",
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "system", "content": "You are a helpful and accurate calculator."},
|
| 20 |
+
{"role": "user", "content": prompt}
|
| 21 |
+
],
|
| 22 |
temperature=0,
|
| 23 |
+
max_tokens=50
|
| 24 |
)
|
| 25 |
+
result = response.choices[0].message.content.strip()
|
| 26 |
return f"✅ Result: {result}"
|
| 27 |
except Exception as e:
|
| 28 |
return f"❌ Error: {str(e)}"
|
|
|
|
| 36 |
],
|
| 37 |
outputs="text",
|
| 38 |
title="🧠 AI Calculator",
|
| 39 |
+
description="A simple calculator using OpenAI GPT-3.5. Choose numbers and operation to get result.",
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|