asad231 commited on
Commit
18e75a5
·
verified ·
1 Parent(s): d60467f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -2,25 +2,27 @@ 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)}"
@@ -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__":