nagasurendra commited on
Commit
253a332
·
verified ·
1 Parent(s): d37f9c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py CHANGED
@@ -2,6 +2,10 @@ 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()
@@ -74,6 +78,31 @@ def submit_ingredients():
74
  print(f"Ingredients submitted: {ingredients}")
75
 
76
  return jsonify({'success': True})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
 
79
 
 
2
  from simple_salesforce import Salesforce
3
  from dotenv import load_dotenv
4
  import os
5
+ import openai
6
+
7
+ openai.api_key = "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"
8
+
9
 
10
  # Load environment variables from .env file
11
  load_dotenv()
 
78
  print(f"Ingredients submitted: {ingredients}")
79
 
80
  return jsonify({'success': True})
81
+ @app.route('/get_dish_suggestions', methods=['POST'])
82
+ def get_dish_suggestions():
83
+ selected_ingredients = request.json.get('ingredients', [])
84
+ if not selected_ingredients:
85
+ return jsonify({'error': 'No ingredients selected'}), 400
86
+
87
+ ingredients_text = ', '.join([ingredient['name'] for ingredient in selected_ingredients])
88
+ prompt = f"Suggest a recipe based on the following ingredients: {ingredients_text}"
89
+
90
+ try:
91
+ # Request a response from the OpenAI API (ChatGPT)
92
+ response = openai.Completion.create(
93
+ model="gpt-4", # or use another version (e.g., 'gpt-3.5-turbo')
94
+ prompt=prompt,
95
+ max_tokens=100, # Limit the response length
96
+ n=1, # Number of responses
97
+ stop=None, # Optional: define where the response should stop
98
+ temperature=0.7 # Control the creativity of the response
99
+ )
100
+
101
+ dish_suggestions = response.choices[0].text.strip() # Get the suggested dishes
102
+ return jsonify({"suggestions": dish_suggestions})
103
+
104
+ except Exception as e:
105
+ return jsonify({"error": f"Error generating suggestions: {str(e)}"}), 500
106
 
107
 
108