Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6001CEM-Individual-Project-Customer-ServiceChatbot/app.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
244 lines (153 sloc)
7.92 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify, render_template | |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration | |
app = Flask(__name__) | |
system_prompt = ( | |
"You are a customer service bot for an e-commerce shop that sells many different kinds of products. " | |
"Your role is to answer questions related to customer service and products only. " | |
"Please provide accurate and helpful information within this scope. " | |
"Always end your response with 'How can I help you today?' if you need further input from the customer. " | |
"If a user asks to buy something, give them the price that you know, or provide an estimated price if you are unsure. " | |
"Only answer the asked question correctly." | |
) | |
@app.route("/") | |
def home(): | |
return render_template("index.html") | |
@app.route("/chat", methods=["POST"]) | |
def chat(): | |
user_input = request.json.get("message") | |
response = get_response(user_input) | |
return jsonify({"response": response}) | |
def get_response(user_input): | |
try: | |
response = get_santiago_response(user_input) | |
if response: | |
return response | |
except Exception as e: | |
print(f"SantiagoPG model failed: {e}") | |
# Fallback to BlenderBot model | |
full_input = system_prompt + " " + user_input | |
return get_blenderbot_response(full_input) | |
def get_santiago_response(user_input): | |
try: | |
from transformers import pipeline | |
santiago_pipe = pipeline("text2text-generation", model="SantiagoPG/chatbot_customer_service") | |
response = santiago_pipe(user_input)[0]['generated_text'] | |
return response | |
except ImportError as e: | |
print(f"Failed to import SantiagoPG model pipeline: {e}") | |
return "" | |
def get_blenderbot_response(user_input): | |
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill") | |
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill") | |
inputs = tokenizer(user_input, return_tensors="pt") | |
reply_ids = model.generate(**inputs) | |
response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0] | |
return response | |
if __name__ == "__main__": | |
app.run(debug=True) | |
# from flask import Flask, request, jsonify, render_template | |
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline | |
# app = Flask(__name__) | |
# # Load SantiagoPG model | |
# santiago_pipe = pipeline("text2text-generation", model="SantiagoPG/chatbot_customer_service") | |
# # Load BlenderBot fallback model | |
# blenderbot_tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") | |
# blenderbot_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/blenderbot-400M-distill") | |
# system_prompt = ( | |
# "You are a customer service bot for an e-commerce shop that sells many different kinds of products. " | |
# "Your role is to answer questions related to customer service and products only. " | |
# "Please provide accurate and helpful information within this scope. " | |
# "Always end your response with 'How can I help you today?' if you need further input from the customer. " | |
# "If a user asks to buy something, give them the price that you know, or provide an estimated price if you are unsure. " | |
# "Only answer the asked question correctly." | |
# ) | |
# @app.route("/") | |
# def home(): | |
# return render_template("index.html") | |
# @app.route("/chat", methods=["POST"]) | |
# def chat(): | |
# user_input = request.json.get("message") | |
# response = get_response(user_input) | |
# return jsonify({"response": response}) | |
# def get_response(user_input): | |
# try: | |
# santiago_response = santiago_pipe(user_input)[0]['generated_text'] | |
# if santiago_response: | |
# return santiago_response | |
# except Exception as e: | |
# print(f"SantiagoPG model failed: {e}") | |
# # Fallback to BlenderBot model | |
# full_input = system_prompt + " " + user_input | |
# inputs = blenderbot_tokenizer(full_input, return_tensors="pt") | |
# reply_ids = blenderbot_model.generate(**inputs) | |
# response = blenderbot_tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0] | |
# return response | |
# if __name__ == "__main__": | |
# app.run(debug=True) | |
# from flask import Flask, request, jsonify, render_template | |
# from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM | |
# app = Flask(__name__) | |
# # Load SantiagoPG customer service model | |
# santiago_pipe = pipeline("text2text-generation", model="SantiagoPG/chatbot_customer_service") | |
# # Load BlenderBot fallback model | |
# tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") | |
# model = AutoModelForSeq2SeqLM.from_pretrained("facebook/blenderbot-400M-distill") | |
# system_prompt = ( | |
# "You are a customer service bot for an e-commerce shop that sells many different kinds of products. " | |
# "Your role is to answer questions related to customer service and products only. " | |
# "Please provide accurate and helpful information within this scope. " | |
# "Always end your response with 'How can I help you today?' if you need further input from the customer. " | |
# "If a user asks to buy something, give them the price that you know, or provide an estimated price if you are unsure. " | |
# "Only answer the asked question correctly." | |
# ) | |
# @app.route("/") | |
# def home(): | |
# return render_template("index.html") | |
# @app.route("/chat", methods=["POST"]) | |
# def chat(): | |
# user_input = request.json.get("message") | |
# response = get_response(user_input) | |
# return jsonify({"response": response}) | |
# def get_response(user_input): | |
# # Try the primary model first | |
# santiago_response = santiago_pipe(user_input)[0]['generated_text'] | |
# if santiago_response: | |
# return santiago_response | |
# # Fallback to BlenderBot model | |
# full_input = system_prompt + " " + user_input | |
# inputs = tokenizer(full_input, return_tensors="pt") | |
# reply_ids = model.generate(**inputs) | |
# response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0] | |
# return response | |
# if __name__ == "__main__": | |
# app.run(debug=True) | |
# from flask import Flask, request, jsonify, render_template | |
# from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration | |
# app = Flask(__name__) | |
# tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill") | |
# model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill") | |
# system_prompt = ( "You are a customer service bot for an ecommerce shop that sells many different kinds of products." | |
# "Your role is to answer questions related to customer service and products only. " | |
# "Please provide accurate and helpful information within this scope." | |
# "Always end your response with 'How can I help you today!' if you need an input from the customer" | |
# "If a user asks to buy something, give them the price that you know just any amount based on your training" | |
# "Only answer the asked question correctly" | |
# # "You must be helpful you are a customer service chatbot for a shop" | |
# # "if asked something that starts with 'I want..' take that as an input and answer correctly" | |
# ) | |
# @app.route("/") | |
# def home(): | |
# return render_template("index.html") | |
# @app.route("/chat", methods=["POST"]) | |
# def chat(): | |
# user_input = request.json.get("message") | |
# response = get_blenderbot_response(user_input) | |
# return jsonify({"response": response}) | |
# def get_blenderbot_response(user_input): | |
# full_input = system_prompt + " " + user_input | |
# inputs = tokenizer(full_input, return_tensors="pt") | |
# reply_ids = model.generate(**inputs) | |
# response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0] | |
# return response | |
# if __name__ == "__main__": | |
# app.run(debug=True) | |