Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
# Initialize the Hugging Face chatbot model (using DialoGPT for simplicity)
|
| 7 |
+
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 8 |
+
|
| 9 |
+
# Example restaurant data
|
| 10 |
+
menu = {
|
| 11 |
+
'vegan': ['Vegan Burger', 'Tofu Stir Fry', 'Veggie Pizza'],
|
| 12 |
+
'vegetarian': ['Cheese Pizza', 'Pasta Alfredo', 'Caprese Salad'],
|
| 13 |
+
'non_vegetarian': ['Chicken Curry', 'Grilled Salmon', 'Steak'],
|
| 14 |
+
'spicy': ['Spicy Tacos', 'Chili Chicken', 'Spicy Wings'],
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
@app.route('/')
|
| 18 |
+
def home():
|
| 19 |
+
return "Welcome to the Restaurant Chatbot API!"
|
| 20 |
+
|
| 21 |
+
@app.route('/chat', methods=['POST'])
|
| 22 |
+
def chat():
|
| 23 |
+
# Get user input from the frontend
|
| 24 |
+
user_input = request.json.get('user_input', '')
|
| 25 |
+
|
| 26 |
+
# Generate a response from the chatbot model
|
| 27 |
+
response = chatbot(user_input)
|
| 28 |
+
chatbot_reply = response[0]['generated_text']
|
| 29 |
+
|
| 30 |
+
# Match user preference to the menu (based on keywords in the input)
|
| 31 |
+
user_preference = None
|
| 32 |
+
if 'vegan' in user_input.lower():
|
| 33 |
+
user_preference = 'vegan'
|
| 34 |
+
elif 'vegetarian' in user_input.lower():
|
| 35 |
+
user_preference = 'vegetarian'
|
| 36 |
+
elif 'non-vegetarian' in user_input.lower():
|
| 37 |
+
user_preference = 'non_vegetarian'
|
| 38 |
+
elif 'spicy' in user_input.lower():
|
| 39 |
+
user_preference = 'spicy'
|
| 40 |
+
|
| 41 |
+
# Get menu suggestions based on user preference
|
| 42 |
+
menu_suggestion = menu.get(user_preference, [])
|
| 43 |
+
|
| 44 |
+
# Return the chatbot response and menu suggestion
|
| 45 |
+
return jsonify({
|
| 46 |
+
'bot_response': chatbot_reply,
|
| 47 |
+
'menu_suggestion': menu_suggestion
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
if __name__ == '__main__':
|
| 51 |
+
app.run(debug=True)
|