Spaces:
Sleeping
Sleeping
File size: 4,117 Bytes
a2f1a4d 9b35d16 71978cc 51455e0 71978cc 4be2e38 c32c1af 4be2e38 84a5b18 71978cc 84a5b18 9b35d16 84a5b18 71978cc 84a5b18 a2f1a4d 51455e0 4be2e38 c32c1af 4be2e38 a2f1a4d 84a5b18 71978cc a2f1a4d fba6dac a2f1a4d 87d489d a2f1a4d 87d489d a2f1a4d fba6dac a2f1a4d 84a5b18 fba6dac a2f1a4d 84a5b18 a2f1a4d 51455e0 3dad656 51455e0 3dad656 51455e0 3dad656 51455e0 3dad656 51455e0 a3745bb 51455e0 3dad656 45329dc 3dad656 45329dc 51455e0 4be2e38 fba6dac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
from flask import Flask, render_template, send_from_directory, request, jsonify
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
from openai import OpenAI
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__, template_folder='templates', static_folder='static')
# Function to get Salesforce connection
def get_salesforce_connection():
try:
sf = Salesforce(
username=os.getenv('SFDC_USERNAME'),
password=os.getenv('SFDC_PASSWORD'),
security_token=os.getenv('SFDC_SECURITY_TOKEN'),
domain=os.getenv('SFDC_DOMAIN', 'login')
)
return sf
except Exception as e:
print(f"Error connecting to Salesforce: {e}")
return None
# Initialize Salesforce connection
sf = get_salesforce_connection()
# Initialize OpenAI client
openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/static/<path:filename>')
def serve_static(filename):
return send_from_directory('static', filename)
@app.route('/get_ingredients', methods=['POST'])
def get_ingredients():
global sf
if not sf:
sf = get_salesforce_connection()
if not sf:
return jsonify({"error": "Failed to connect to Salesforce"}), 500
dietary_preference = request.json.get('dietary_preference', '').lower()
# SOQL query based on dietary preference with corrected field name 'Name'
if dietary_preference == 'veg':
soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c IN ('Veg', 'Both') LIMIT 200"
elif dietary_preference == 'non-vegetarian':
soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c IN ('Non-Veg', 'Both') LIMIT 200"
else:
soql = "SELECT Name FROM Sector_Detail__c LIMIT 200"
try:
result = sf.query(soql)
ingredients = [record['Name'] for record in result['records'] if 'Name' in record]
return jsonify({"ingredients": ingredients})
except Exception as e:
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
@app.route('/get_food_suggestions', methods=['POST'])
def get_food_suggestions():
selected_ingredients = request.json.get('ingredients', [])
if not selected_ingredients:
return jsonify({"error": "No ingredients selected"}), 400
# Detailed prompt for tailored recipe generation
prompt = f"Suggest 3-5 creative, realistic, and simple recipe ideas using only the following ingredients: {', '.join(selected_ingredients)}. Provide the recipe names in a numbered list format, ensuring each recipe is feasible with the exact ingredients provided and avoids additional ingredients."
try:
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo", # Upgrade to "gpt-4" if available
messages=[
{"role": "system", "content": "You are a highly skilled chef assistant specializing in creating recipes from a limited set of ingredients."},
{"role": "user", "content": prompt}
],
max_tokens=200 # Sufficient for detailed suggestions
)
suggestions = response.choices[0].message.content.strip().split('\n')
return jsonify({"suggestions": [s.strip() for s in suggestions if s.strip()]})
except Exception as e:
error_message = str(e)
if "429" in error_message or "insufficient_quota" in error_message.lower():
return jsonify({
"error": "Unable to get recipe ideas. HTTP error! Status: 429. You exceeded your current OpenAI quota. Please upgrade your plan at https://platform.openai.com/account/billing or check the documentation at https://platform.openai.com/docs/guides/error-codes/api-errors."
}), 429
return jsonify({"error": f"Unable to get recipe ideas. {error_message}. Please contact support if the issue persists."}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=7860) |