Spaces:
Sleeping
Sleeping
from flask import Flask, render_template, send_from_directory, request, jsonify | |
from simple_salesforce import Salesforce | |
from dotenv import load_dotenv | |
import os | |
import json | |
import requests | |
# 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() | |
def index(): | |
return render_template('index.html') | |
def serve_static(filename): | |
return send_from_directory('static', filename) | |
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 | |
if dietary_preference == 'veg': | |
soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c = 'Veg' LIMIT 200" | |
elif dietary_preference == 'non-vegetarian': | |
soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c = 'Non-Veg' LIMIT 200" | |
else: | |
soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c LIMIT 200" | |
try: | |
result = sf.query(soql) | |
ingredients = [ | |
{"name": record['Name'], "image_url": record.get('Image_URL__c', '')} | |
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 | |
# Mock endpoint to generate South Indian recipes (replace with ChatGPT API) | |
def generate_recipes(): | |
data = request.json | |
selected_ingredients = data.get('ingredients', []) | |
# Mock response simulating ChatGPT (replace with real API call) | |
ingredients_str = ', '.join(selected_ingredients) | |
mock_response = { | |
"recipes": [ | |
{ | |
"name": "Pongal", | |
"image_url": "https://via.placeholder.com/100?text=Pongal", | |
"description": "A traditional Tamil Nadu dish made with rice and moong dal, seasoned with ghee, pepper, and cumin, reflecting the simplicity and warmth of South Indian breakfasts.", | |
"details": { | |
"preparation": "Cook rice and moong dal with water, temper with ghee, cumin, pepper, and cashews, and serve hot with chutney.", | |
"key_ingredients": ["rice", "moong dal", "ghee", "pepper", "cumin"] | |
} | |
}, | |
{ | |
"name": "Payasam", | |
"image_url": "https://via.placeholder.com/100?text=Payasam", | |
"description": "A creamy Kerala dessert made with milk, jaggery, and rice, enriched with cardamom and coconut, a staple in South Indian festivities.", | |
"details": { | |
"preparation": "Boil milk, add rice and jaggery, simmer until thick, and flavor with cardamom and coconut.", | |
"key_ingredients": ["milk", "jaggery", "rice", "cardamom", "coconut"] | |
} | |
}, | |
{ | |
"name": "Idli", | |
"image_url": "https://via.placeholder.com/100?text=Idli", | |
"description": "A fluffy steamed cake from Karnataka, made with fermented rice and urad dal, served with sambar and chutney, embodying South Indian comfort food.", | |
"details": { | |
"preparation": "Ferment rice and urad dal batter, steam in idli molds, and serve with sambar and coconut chutney.", | |
"key_ingredients": ["rice", "urad dal", "sambar", "chutney"] | |
} | |
}, | |
{ | |
"name": "Dosa", | |
"image_url": "https://via.placeholder.com/100?text=Dosa", | |
"description": "A crispy Andhra Pradesh crepe made from fermented rice and urad dal, often filled with potato masala, a versatile South Indian delight.", | |
"details": { | |
"preparation": "Spread fermented batter on a hot griddle, cook until crispy, add potato filling, and serve with chutney.", | |
"key_ingredients": ["rice", "urad dal", "potato", "chutney"] | |
} | |
}, | |
{ | |
"name": "Upma", | |
"image_url": "https://via.placeholder.com/100?text=Upma", | |
"description": "A savory Tamil Nadu porridge made with semolina, tempered with mustard seeds and curry leaves, a quick yet flavorful breakfast option.", | |
"details": { | |
"preparation": "Roast semolina, cook with water, and temper with mustard seeds, curry leaves, and vegetables.", | |
"key_ingredients": ["semolina", "mustard seeds", "curry leaves", "vegetables"] | |
} | |
} | |
] | |
} | |
# Filter recipes based on selected ingredients (mock logic) | |
filtered_recipes = [ | |
recipe for recipe in mock_response["recipes"] | |
if any(ing in ingredients_str.lower() for ing in recipe["details"]["key_ingredients"]) | |
][:5] # Limit to 5 recipes | |
return jsonify({"recipes": filtered_recipes}) | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0', port=7860) |