Spaces:
Running
Running
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) | |