Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
from pyngrok import ngrok
|
5 |
+
import random
|
6 |
+
import re
|
7 |
+
|
8 |
+
# β
Set up ngrok
|
9 |
+
ngrok.set_auth_token("xai-RrirCeSYWUT21ipAY6BjWr3jSEDmK8sA16S3EbGGUC4oWH1cyn3pfI0vIJg1D1ym3NfFkhUbDOeFmf3s")
|
10 |
+
public_url = ngrok.connect(8501).public_url
|
11 |
+
|
12 |
+
# β
Load AI Model
|
13 |
+
model_name = "deepseek-ai/deepseek-llm-7b-chat"
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
16 |
+
model_name, torch_dtype=torch.float16, device_map="auto", offload_folder="offload_weights"
|
17 |
+
)
|
18 |
+
|
19 |
+
# π Menu for chatbot
|
20 |
+
menu = {
|
21 |
+
"meals": ["Grilled Chicken with Rice", "Beef Steak", "Salmon with Lemon Butter Sauce", "Vegetable Stir-Fry"],
|
22 |
+
"fast_foods": ["Cheeseburger", "Pepperoni Pizza", "Fried Chicken", "Hot Dog", "Tacos", "French Fries"],
|
23 |
+
"drinks": ["Coke", "Pepsi", "Lemonade", "Orange Juice", "Iced Coffee", "Milkshake"],
|
24 |
+
"sweets": ["Chocolate Cake", "Ice Cream", "Apple Pie", "Cheesecake", "Brownies", "Donuts"]
|
25 |
+
}
|
26 |
+
|
27 |
+
system_prompt = f"""
|
28 |
+
You are OrderBot, a virtual restaurant assistant.
|
29 |
+
You help customers order food from the following menu:
|
30 |
+
|
31 |
+
π½οΈ **Meals**: {', '.join(menu['meals'])}
|
32 |
+
π **Fast Foods**: {', '.join(menu['fast_foods'])}
|
33 |
+
π₯€ **Drinks**: {', '.join(menu['drinks'])}
|
34 |
+
π° **Sweets**: {', '.join(menu['sweets'])}
|
35 |
+
|
36 |
+
Rules:
|
37 |
+
1οΈβ£ Always confirm the customer's order.
|
38 |
+
2οΈβ£ Ask if they need anything else.
|
39 |
+
3οΈβ£ Respond in a friendly and professional manner.
|
40 |
+
"""
|
41 |
+
|
42 |
+
def process_order(user_input):
|
43 |
+
"""
|
44 |
+
Handles chatbot conversation and order processing.
|
45 |
+
"""
|
46 |
+
responses = {
|
47 |
+
"greetings": ["Hello! How can I assist you today?", "Hey there! What would you like to order?", "Hi! Ready to place an order? π"],
|
48 |
+
"farewell": ["Goodbye! Have a great day! π", "See you next time!", "Take care!"],
|
49 |
+
"thanks": ["You're welcome! π", "Happy to help!", "Anytime!"],
|
50 |
+
"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."]
|
51 |
+
}
|
52 |
+
|
53 |
+
user_input = user_input.lower()
|
54 |
+
|
55 |
+
if any(word in user_input for word in ["hello", "hi", "hey"]):
|
56 |
+
return random.choice(responses["greetings"])
|
57 |
+
elif any(word in user_input for word in ["bye", "goodbye", "see you"]):
|
58 |
+
return random.choice(responses["farewell"])
|
59 |
+
elif any(word in user_input for word in ["thank you", "thanks"]):
|
60 |
+
return random.choice(responses["thanks"])
|
61 |
+
|
62 |
+
# AI-generated response
|
63 |
+
prompt = f"{system_prompt}\nUser: {user_input}\nOrderBot:"
|
64 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
65 |
+
output = model.generate(**inputs, max_new_tokens=150)
|
66 |
+
raw_response = tokenizer.decode(output[0], skip_special_tokens=True)
|
67 |
+
|
68 |
+
response = raw_response.split("OrderBot:")[-1].strip()
|
69 |
+
response = re.sub(r"Setting `pad_token_id`.*\n", "", response)
|
70 |
+
|
71 |
+
return response
|
72 |
+
|
73 |
+
# π¨ Streamlit UI
|
74 |
+
st.title("π OrderBot: AI Restaurant Assistant")
|
75 |
+
st.write(f"π **Public URL:** [{public_url}]({public_url}) (via ngrok)")
|
76 |
+
|
77 |
+
# βΉοΈ Display OrderBot Description
|
78 |
+
st.markdown("""
|
79 |
+
### π Hey there, I am OrderBot!
|
80 |
+
I am an **AI-driven assistant** powered by the **DeepSeek-7B Chat** model, designed for seamless natural language interaction.
|
81 |
+
I leverage **advanced machine learning** to process and respond to human input with **precision and efficiency**.
|
82 |
+
Let me take your order! ππ₯€π°
|
83 |
+
""")
|
84 |
+
|
85 |
+
# Chat History
|
86 |
+
if "messages" not in st.session_state:
|
87 |
+
st.session_state.messages = []
|
88 |
+
|
89 |
+
# Display previous chat messages
|
90 |
+
for message in st.session_state.messages:
|
91 |
+
with st.chat_message(message["role"]):
|
92 |
+
st.write(message["content"])
|
93 |
+
|
94 |
+
# User Input
|
95 |
+
user_input = st.chat_input("Type your message here...")
|
96 |
+
|
97 |
+
if user_input:
|
98 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
99 |
+
with st.chat_message("user"):
|
100 |
+
st.write(user_input)
|
101 |
+
|
102 |
+
response = process_order(user_input)
|
103 |
+
|
104 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
105 |
+
with st.chat_message("assistant"):
|
106 |
+
st.write(response)
|