geethareddy commited on
Commit
51455e0
·
verified ·
1 Parent(s): fba6dac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py CHANGED
@@ -2,6 +2,7 @@ from flask import Flask, render_template, send_from_directory, request, jsonify
2
  from simple_salesforce import Salesforce
3
  from dotenv import load_dotenv
4
  import os
 
5
 
6
  # Load environment variables from .env file
7
  load_dotenv()
@@ -25,6 +26,9 @@ def get_salesforce_connection():
25
  # Initialize Salesforce connection
26
  sf = get_salesforce_connection()
27
 
 
 
 
28
  @app.route('/')
29
  def index():
30
  return render_template('index.html')
@@ -58,5 +62,27 @@ def get_ingredients():
58
  except Exception as e:
59
  return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  if __name__ == '__main__':
62
  app.run(debug=True, host='0.0.0.0', port=7860)
 
2
  from simple_salesforce import Salesforce
3
  from dotenv import load_dotenv
4
  import os
5
+ from openai import OpenAI
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
 
26
  # Initialize Salesforce connection
27
  sf = get_salesforce_connection()
28
 
29
+ # Initialize OpenAI client
30
+ openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
31
+
32
  @app.route('/')
33
  def index():
34
  return render_template('index.html')
 
62
  except Exception as e:
63
  return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
64
 
65
+ @app.route('/get_food_suggestions', methods=['POST'])
66
+ def get_food_suggestions():
67
+ selected_ingredients = request.json.get('ingredients', [])
68
+ if not selected_ingredients:
69
+ return jsonify({"error": "No ingredients selected"}), 400
70
+
71
+ prompt = f"Suggest some food items using the following ingredients: {', '.join(selected_ingredients)}. Provide a list of 3-5 recipe ideas."
72
+
73
+ try:
74
+ response = openai_client.chat.completions.create(
75
+ model="gpt-3.5-turbo", # or "gpt-4" if you have access
76
+ messages=[
77
+ {"role": "system", "content": "You are a helpful chef assistant."},
78
+ {"role": "user", "content": prompt}
79
+ ],
80
+ max_tokens=150
81
+ )
82
+ suggestions = response.choices[0].message.content.strip().split('\n')
83
+ return jsonify({"suggestions": suggestions})
84
+ except Exception as e:
85
+ return jsonify({"error": f"Failed to get food suggestions: {str(e)}"}), 500
86
+
87
  if __name__ == '__main__':
88
  app.run(debug=True, host='0.0.0.0', port=7860)