asad231 commited on
Commit
82f9b9e
Β·
verified Β·
1 Parent(s): 64178de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -25
app.py CHANGED
@@ -1,42 +1,31 @@
1
  import gradio as gr
2
- import openai
3
- import os
4
-
5
- # OpenAI API key (from Hugging Face secrets)
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"Calculate {num1} {operation} {num2} and 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)}"
29
 
30
  demo = gr.Interface(
31
- fn=ai_calculator,
32
  inputs=[
33
  gr.Number(label="Enter First Number"),
34
  gr.Number(label="Enter Second Number"),
35
  gr.Radio(["+", "-", "*", "/"], label="Operation"),
36
  ],
37
  outputs="text",
38
- title="🧠 AI Calculator",
39
- description="A simple calculator using OpenAI GPT-3.5 Turbo. Enter numbers and choose operation.",
40
  )
41
 
42
  if __name__ == "__main__":
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ def simple_calculator(num1, num2, operation):
4
  try:
5
+ if operation == "+":
6
+ result = num1 + num2
7
+ elif operation == "-":
8
+ result = num1 - num2
9
+ elif operation == "*":
10
+ result = num1 * num2
11
+ elif operation == "/":
12
+ result = num1 / num2 if num2 != 0 else "∞ (Divide by Zero)"
13
+ else:
14
+ result = "Unknown operation"
15
  return f"βœ… Result: {result}"
16
  except Exception as e:
17
  return f"❌ Error: {str(e)}"
18
 
19
  demo = gr.Interface(
20
+ fn=simple_calculator,
21
  inputs=[
22
  gr.Number(label="Enter First Number"),
23
  gr.Number(label="Enter Second Number"),
24
  gr.Radio(["+", "-", "*", "/"], label="Operation"),
25
  ],
26
  outputs="text",
27
+ title="🧠 Simple Calculator (No OpenAI)",
28
+ description="This is a temporary calculator while you cry over OpenAI quota 😒",
29
  )
30
 
31
  if __name__ == "__main__":