ngwakomadikwe commited on
Commit
c0bc8cc
·
verified ·
1 Parent(s): 53c126d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -1,11 +1,22 @@
1
  import os
2
- import openai
3
  from flask import Flask, request, jsonify, render_template
4
 
5
  app = Flask(__name__)
6
 
7
  # ✅ Get the API key from environment variables
8
- openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  @app.route("/")
11
  def home():
@@ -15,7 +26,7 @@ def home():
15
  @app.route("/chat", methods=["POST"])
16
  def chat():
17
  try:
18
- if not openai.api_key:
19
  return jsonify({"error": "OpenAI API key not configured"}), 500
20
 
21
  data = request.json
@@ -24,27 +35,15 @@ def chat():
24
  if not user_message:
25
  return jsonify({"error": "Message is required"}), 400
26
 
27
- # Try the new OpenAI client first, fall back to old method
28
- try:
29
- from openai import OpenAI
30
- client = OpenAI()
31
- response = client.chat.completions.create(
32
- model="gpt-3.5-turbo",
33
- messages=[{"role": "user", "content": user_message}],
34
- temperature=0.7,
35
- max_tokens=1000
36
- )
37
- reply = response.choices[0].message.content
38
- except:
39
- # Fallback to older OpenAI API method
40
- response = openai.ChatCompletion.create(
41
- model="gpt-3.5-turbo",
42
- messages=[{"role": "user", "content": user_message}],
43
- temperature=0.7,
44
- max_tokens=1000
45
- )
46
- reply = response.choices[0].message["content"]
47
 
 
48
  return jsonify({"reply": reply})
49
 
50
  except Exception as e:
@@ -53,7 +52,7 @@ def chat():
53
  @app.route("/health")
54
  def health():
55
  """Health check endpoint"""
56
- return jsonify({"status": "healthy", "service": "SmartMate"})
57
 
58
  if __name__ == "__main__":
59
  app.run(host="0.0.0.0", port=7860)
 
1
  import os
2
+ from openai import OpenAI
3
  from flask import Flask, request, jsonify, render_template
4
 
5
  app = Flask(__name__)
6
 
7
  # ✅ Get the API key from environment variables
8
+ api_key = os.getenv("OPENAI_API_KEY")
9
+
10
+ if not api_key:
11
+ print("WARNING: OPENAI_API_KEY is not set. Chat functionality will not work.")
12
+ client = None
13
+ else:
14
+ try:
15
+ from openai import OpenAI
16
+ client = OpenAI(api_key=api_key)
17
+ except Exception as e:
18
+ print(f"Error initializing OpenAI client: {e}")
19
+ client = None
20
 
21
  @app.route("/")
22
  def home():
 
26
  @app.route("/chat", methods=["POST"])
27
  def chat():
28
  try:
29
+ if not client:
30
  return jsonify({"error": "OpenAI API key not configured"}), 500
31
 
32
  data = request.json
 
35
  if not user_message:
36
  return jsonify({"error": "Message is required"}), 400
37
 
38
+ # Use the new OpenAI client
39
+ response = client.chat.completions.create(
40
+ model="gpt-3.5-turbo",
41
+ messages=[{"role": "user", "content": user_message}],
42
+ temperature=0.7,
43
+ max_tokens=1000
44
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ reply = response.choices[0].message.content
47
  return jsonify({"reply": reply})
48
 
49
  except Exception as e:
 
52
  @app.route("/health")
53
  def health():
54
  """Health check endpoint"""
55
+ return jsonify({"status": "healthy", "service": "ThutoAI"})
56
 
57
  if __name__ == "__main__":
58
  app.run(host="0.0.0.0", port=7860)