Update app.py
Browse files
app.py
CHANGED
|
@@ -1,201 +1,37 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
"Appetizers": [
|
| 11 |
-
{"name": "Spring Rolls", "price": 5},
|
| 12 |
-
{"name": "Garlic Bread", "price": 4}
|
| 13 |
-
],
|
| 14 |
-
"Main Courses": [
|
| 15 |
-
{"name": "Grilled Chicken", "price": 15},
|
| 16 |
-
{"name": "Vegetable Stir Fry", "price": 12}
|
| 17 |
-
],
|
| 18 |
-
"Desserts": [
|
| 19 |
-
{"name": "Cheesecake", "price": 6},
|
| 20 |
-
{"name": "Chocolate Lava Cake", "price": 7}
|
| 21 |
-
],
|
| 22 |
-
"Drinks": [
|
| 23 |
-
{"name": "Soda", "price": 2},
|
| 24 |
-
{"name": "Wine", "price": 8}
|
| 25 |
-
],
|
| 26 |
-
"Combo Meals": [
|
| 27 |
-
{"name": "Lunch Combo", "price": 10},
|
| 28 |
-
{"name": "Dinner Combo", "price": 20}
|
| 29 |
-
]
|
| 30 |
-
}
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"Thursday": "Sushi - $14",
|
| 37 |
-
"Friday": "Fish & Chips - $11",
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
events = {
|
| 41 |
-
"Folk Lore Show": "Every Friday at 7 PM",
|
| 42 |
-
"Cooking Class": "Every Saturday at 10 AM",
|
| 43 |
-
"Food Festival": "Last weekend of each month"
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
website_info = {
|
| 47 |
-
"password_reset": "To reset your password, go to the password reset page and follow the instructions.",
|
| 48 |
-
"track_order": "Track your delivery order on our tracking page.",
|
| 49 |
-
"gift_cards": "We offer online gift cards. Purchase them on our gift card page.",
|
| 50 |
-
"change_address": "Change your delivery address in your account settings.",
|
| 51 |
-
"cancel_order": "Cancel your order online within a specified timeframe on your order page.",
|
| 52 |
-
"delete_account": "To delete your account, contact our support team.",
|
| 53 |
-
"chat_support": "Start an online chat with our support team on our chat support page.",
|
| 54 |
-
"social_media": {
|
| 55 |
-
"facebook": "You can reach us on Facebook.",
|
| 56 |
-
"twitter": "You can reach us on Twitter.",
|
| 57 |
-
"instagram": "You can reach us on Instagram."
|
| 58 |
-
},
|
| 59 |
-
"reviews": "Customer reviews are available on our reviews page.",
|
| 60 |
-
"update_profile": "Update your profile information in your account settings."
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
# Shortened quiz data with 5 questions
|
| 64 |
-
quiz_questions = [
|
| 65 |
-
{"question": "How many cuisines does 'The Chef Story' offer?",
|
| 66 |
-
"options": ["1", "3", "5", "More than 5"],
|
| 67 |
-
"answer": "More than 5"},
|
| 68 |
-
{"question": "What is a unique feature of 'The Chef Story'?",
|
| 69 |
-
"options": ["Drive-thru service", "Folk Lore shows", "Home delivery only", "DIY meal kits"],
|
| 70 |
-
"answer": "Folk Lore shows"},
|
| 71 |
-
{"question": "What type of shows can you enjoy at our facility?",
|
| 72 |
-
"options": ["Magic shows", "Folk Lore shows", "Stand-up comedy", "Movie screenings"],
|
| 73 |
-
"answer": "Folk Lore shows"},
|
| 74 |
-
{"question": "Can you customize your order at 'The Chef Story'?",
|
| 75 |
-
"options": ["Yes", "No"],
|
| 76 |
-
"answer": "Yes"},
|
| 77 |
-
{"question": "What is the price of the Lunch Combo?",
|
| 78 |
-
"options": ["$10", "$15", "$20", "$25"],
|
| 79 |
-
"answer": "$10"}
|
| 80 |
-
]
|
| 81 |
-
|
| 82 |
-
# State management for the quiz
|
| 83 |
-
if 'quiz_state' not in st.session_state:
|
| 84 |
-
st.session_state.quiz_state = {
|
| 85 |
-
"current_question": None,
|
| 86 |
-
"options": [],
|
| 87 |
-
"answer": None,
|
| 88 |
-
"score": 0,
|
| 89 |
-
"total_questions": 0
|
| 90 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
def
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
st.session_state.quiz_state["options"] = st.session_state.quiz_state["current_question"]["options"]
|
| 97 |
-
st.session_state.quiz_state["answer"] = st.session_state.quiz_state["current_question"]["answer"]
|
| 98 |
-
question = st.session_state.quiz_state["current_question"]["question"]
|
| 99 |
-
options = " / ".join(st.session_state.quiz_state["options"])
|
| 100 |
-
return f"Quiz Time! {question} Options: {options}"
|
| 101 |
-
|
| 102 |
-
def check_answer(user_answer):
|
| 103 |
-
if st.session_state.quiz_state["current_question"] is None:
|
| 104 |
-
return "No quiz in progress. Type 'start quiz' to begin."
|
| 105 |
-
|
| 106 |
-
correct_answer = st.session_state.quiz_state["answer"]
|
| 107 |
-
|
| 108 |
-
if user_answer.lower() == correct_answer.lower():
|
| 109 |
-
st.session_state.quiz_state["score"] += 1
|
| 110 |
-
response = "Correct! "
|
| 111 |
-
else:
|
| 112 |
-
response = "Incorrect. The correct answer was: " + correct_answer + ". "
|
| 113 |
-
|
| 114 |
-
st.session_state.quiz_state["total_questions"] -= 1
|
| 115 |
-
|
| 116 |
-
if st.session_state.quiz_state["total_questions"] == 0:
|
| 117 |
-
final_score = st.session_state.quiz_state["score"]
|
| 118 |
-
st.session_state.quiz_state = {"current_question": None, "options": [], "answer": None, "score": 0, "total_questions": 0}
|
| 119 |
-
return f"Quiz over! Your final score is {final_score}."
|
| 120 |
-
|
| 121 |
-
st.session_state.quiz_state["current_question"] = random.choice(quiz_questions)
|
| 122 |
-
st.session_state.quiz_state["options"] = st.session_state.quiz_state["current_question"]["options"]
|
| 123 |
-
st.session_state.quiz_state["answer"] = st.session_state.quiz_state["current_question"]["answer"]
|
| 124 |
-
question = st.session_state.quiz_state["current_question"]["question"]
|
| 125 |
-
options = " / ".join(st.session_state.quiz_state["options"])
|
| 126 |
-
return f"{response} Next question: {question} Options: {options}"
|
| 127 |
-
|
| 128 |
-
def handle_message(message):
|
| 129 |
-
message = message.lower()
|
| 130 |
-
if "start quiz" in message:
|
| 131 |
-
return start_quiz()
|
| 132 |
-
elif "answer" in message:
|
| 133 |
-
user_answer = message.split(" ", 1)[1].strip()
|
| 134 |
-
return check_answer(user_answer)
|
| 135 |
-
elif "menu" in message:
|
| 136 |
-
return menu
|
| 137 |
-
elif "price" in message:
|
| 138 |
-
return "Check our menu for detailed pricing information."
|
| 139 |
-
elif "special" in message:
|
| 140 |
-
return specials.get("Monday", "No special today.")
|
| 141 |
-
elif "events" in message:
|
| 142 |
-
return events
|
| 143 |
-
elif "kid" in message or "play area" in message:
|
| 144 |
-
return "Yes, we have a dedicated play area for kids."
|
| 145 |
-
elif "wheelchair" in message or "accessibility" in message:
|
| 146 |
-
return "Yes, our restaurant is wheelchair accessible."
|
| 147 |
-
elif "digital payments" in message:
|
| 148 |
-
return "We accept digital payments like Apple Pay and Google Pay."
|
| 149 |
-
elif "password reset" in message:
|
| 150 |
-
return website_info["password_reset"]
|
| 151 |
-
elif "track order" in message:
|
| 152 |
-
return website_info["track_order"]
|
| 153 |
-
elif "gift card" in message:
|
| 154 |
-
return website_info["gift_cards"]
|
| 155 |
-
elif "change address" in message:
|
| 156 |
-
return website_info["change_address"]
|
| 157 |
-
elif "cancel order" in message:
|
| 158 |
-
return website_info["cancel_order"]
|
| 159 |
-
elif "delete account" in message:
|
| 160 |
-
return website_info["delete_account"]
|
| 161 |
-
elif "chat support" in message:
|
| 162 |
-
return website_info["chat_support"]
|
| 163 |
-
elif "social media" in message:
|
| 164 |
-
return website_info["social_media"]
|
| 165 |
-
elif "reviews" in message:
|
| 166 |
-
return website_info["reviews"]
|
| 167 |
-
elif "update profile" in message:
|
| 168 |
-
return website_info["update_profile"]
|
| 169 |
-
else:
|
| 170 |
-
# Use the model to generate a response if no direct match is found
|
| 171 |
-
prompt = f"Customer inquiry: {message}. Please provide a helpful response."
|
| 172 |
-
return generate_response(prompt)
|
| 173 |
-
|
| 174 |
-
def generate_response(prompt):
|
| 175 |
-
# Generate a response using Hugging Face's transformers pipeline
|
| 176 |
-
response = llm(prompt, max_length=150, num_return_sequences=1)
|
| 177 |
-
return response[0]['generated_text']
|
| 178 |
-
|
| 179 |
-
def main():
|
| 180 |
-
st.title("The Cuisine Crafter")
|
| 181 |
-
|
| 182 |
-
user_input = st.text_area("You:", "")
|
| 183 |
-
|
| 184 |
-
if user_input:
|
| 185 |
-
response = handle_message(user_input)
|
| 186 |
-
st.write(f"Bot: {response}")
|
| 187 |
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
st.write(f"Bot: {quiz_response}")
|
| 192 |
|
| 193 |
-
|
| 194 |
-
if 'quiz_state' in st.session_state and st.session_state.quiz_state["current_question"]:
|
| 195 |
-
user_answer = st.text_input("Your Answer:", "")
|
| 196 |
-
if st.button('Submit Answer'):
|
| 197 |
-
quiz_response = check_answer(user_answer)
|
| 198 |
-
st.write(f"Bot: {quiz_response}")
|
| 199 |
|
| 200 |
-
if __name__ ==
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
# Replace with your actual LangSmith API key
|
| 5 |
+
LANGSMITH_API_KEY = 'your_langsmith_api_key'
|
| 6 |
+
LANGSMITH_ENDPOINT = 'https://api.langsmith.com/v1/tailor'
|
| 7 |
|
| 8 |
+
# Initialize the Hugging Face conversational pipeline
|
| 9 |
+
conversational_pipeline = pipeline('conversational', model='facebook/blenderbot-3B')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def tailor_with_langsmith(model_data):
|
| 12 |
+
headers = {
|
| 13 |
+
'Authorization': f'Bearer {LANGSMITH_API_KEY}',
|
| 14 |
+
'Content-Type': 'application/json'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
+
data = {
|
| 17 |
+
'model_data': model_data
|
| 18 |
+
}
|
| 19 |
+
response = requests.post(LANGSMITH_ENDPOINT, json=data, headers=headers)
|
| 20 |
+
response.raise_for_status()
|
| 21 |
+
return response.json()
|
| 22 |
|
| 23 |
+
def create_custom_conversation(prompt):
|
| 24 |
+
# Step 1: Get response from Hugging Face model
|
| 25 |
+
hf_response = conversational_pipeline(prompt)
|
| 26 |
+
hf_reply = hf_response[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Step 2: Tailor the response using LangSmith
|
| 29 |
+
tailored_response = tailor_with_langsmith(hf_reply)
|
| 30 |
+
tailored_reply = tailored_response.get('tailored_reply', '')
|
|
|
|
| 31 |
|
| 32 |
+
return tailored_reply
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
if __name__ == '__main__':
|
| 35 |
+
user_prompt = "Tell me about the latest advancements in AI."
|
| 36 |
+
response = create_custom_conversation(user_prompt)
|
| 37 |
+
print("Tailored Response:", response)
|