Orderbot2.0 / app.py
Entie's picture
Update app.py
b5f5129 verified
raw
history blame
4.12 kB
import streamlit as st
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from pyngrok import ngrok
import random
import re
# βœ… Set up ngrok
ngrok.set_auth_token("2ppPfZORNKDM4PrFh24fot8Dgmu_7tfFX5fvm1gHnzoyAY236")
public_url = ngrok.connect(8501).public_url
# βœ… Load AI Model
model_name = "deepseek-ai/deepseek-llm-7b-chat"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name, torch_dtype=torch.float16, device_map="auto", offload_folder="offload_weights"
)
# πŸ“Œ Menu for chatbot
menu = {
"meals": ["Grilled Chicken with Rice", "Beef Steak", "Salmon with Lemon Butter Sauce", "Vegetable Stir-Fry"],
"fast_foods": ["Cheeseburger", "Pepperoni Pizza", "Fried Chicken", "Hot Dog", "Tacos", "French Fries"],
"drinks": ["Coke", "Pepsi", "Lemonade", "Orange Juice", "Iced Coffee", "Milkshake"],
"sweets": ["Chocolate Cake", "Ice Cream", "Apple Pie", "Cheesecake", "Brownies", "Donuts"]
}
system_prompt = f"""
You are OrderBot, a virtual restaurant assistant.
You help customers order food from the following menu:
🍽️ **Meals**: {', '.join(menu['meals'])}
πŸ” **Fast Foods**: {', '.join(menu['fast_foods'])}
πŸ₯€ **Drinks**: {', '.join(menu['drinks'])}
🍰 **Sweets**: {', '.join(menu['sweets'])}
Rules:
1️⃣ Always confirm the customer's order.
2️⃣ Ask if they need anything else.
3️⃣ Respond in a friendly and professional manner.
"""
def process_order(user_input):
"""
Handles chatbot conversation and order processing.
"""
responses = {
"greetings": ["Hello! How can I assist you today?", "Hey there! What would you like to order?", "Hi! Ready to place an order? 😊"],
"farewell": ["Goodbye! Have a great day! πŸ‘‹", "See you next time!", "Take care!"],
"thanks": ["You're welcome! 😊", "Happy to help!", "Anytime!"],
"default": ["I'm not sure how to respond to that. Can I take your order?", "Interesting! Tell me more.", "I'm here to assist with your order."]
}
user_input = user_input.lower()
if any(word in user_input for word in ["hello", "hi", "hey"]):
return random.choice(responses["greetings"])
elif any(word in user_input for word in ["bye", "goodbye", "see you"]):
return random.choice(responses["farewell"])
elif any(word in user_input for word in ["thank you", "thanks"]):
return random.choice(responses["thanks"])
# AI-generated response
prompt = f"{system_prompt}\nUser: {user_input}\nOrderBot:"
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
output = model.generate(**inputs, max_new_tokens=150)
raw_response = tokenizer.decode(output[0], skip_special_tokens=True)
response = raw_response.split("OrderBot:")[-1].strip()
response = re.sub(r"Setting `pad_token_id`.*\n", "", response)
return response
# 🎨 Streamlit UI
st.title("πŸ›’ OrderBot: AI Restaurant Assistant")
st.write(f"🌍 **Public URL:** [{public_url}]({public_url}) (via ngrok)")
# ℹ️ Display OrderBot Description
st.markdown("""
### πŸ‘‹ Hey there, I am OrderBot! Your friendly Restaurants AI Agent.
I am an **AI-driven assistant** powered by the **DeepSeek-7B Chat** model, designed for seamless natural language interaction.
I leverage **advanced machine learning** to process and respond to human input with **precision and efficiency**.
Let me take your order! πŸ”πŸ₯€πŸ°
""")
# Chat History
if "messages" not in st.session_state:
st.session_state.messages = []
# Display previous chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
# User Input
user_input = st.chat_input("Type your message here...")
if user_input:
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.write(user_input)
response = process_order(user_input)
st.session_state.messages.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.write(response)