Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
|
2 |
-
import os, torch, gradio as gr
|
3 |
from pathlib import Path
|
4 |
from huggingface_hub import login
|
5 |
|
@@ -11,6 +10,7 @@ from llama_index.llms.huggingface import HuggingFaceLLM
|
|
11 |
from llama_index.embeddings.langchain import LangchainEmbedding
|
12 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
13 |
|
|
|
14 |
SYSTEM_PROMPT = """
|
15 |
You are a friendly café assistant for Café Eleven. Your job is to:
|
16 |
1. Greet the customer warmly
|
@@ -18,19 +18,20 @@ You are a friendly café assistant for Café Eleven. Your job is to:
|
|
18 |
3. Answer questions about ingredients, preparation, etc.
|
19 |
4. Process special requests (allergies, modifications)
|
20 |
5. Provide a friendly farewell
|
21 |
-
|
22 |
-
|
23 |
|
24 |
WRAPPER_PROMPT = PromptTemplate(
|
25 |
"[INST]<<SYS>>\n" + SYSTEM_PROMPT + "\n<</SYS>>\n\n{query_str} [/INST]"
|
26 |
)
|
27 |
|
|
|
28 |
login(token=os.environ["HF_TOKEN"])
|
29 |
|
30 |
-
# ---------- 1. Pre-load documents & build the vector index (CPU-safe) ----------
|
31 |
docs = SimpleDirectoryReader(
|
32 |
input_files=[str(p) for p in Path(".").glob("*.pdf")]
|
33 |
).load_data()
|
|
|
34 |
embed_model = LangchainEmbedding(
|
35 |
HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
36 |
)
|
@@ -39,8 +40,8 @@ Settings.chunk_size = 512
|
|
39 |
|
40 |
index = VectorStoreIndex.from_documents(docs)
|
41 |
|
42 |
-
# ---------- 2.
|
43 |
-
_state = {"chat_engine": None}
|
44 |
|
45 |
def get_chat_engine():
|
46 |
if _state["chat_engine"] is None:
|
@@ -49,7 +50,7 @@ def get_chat_engine():
|
|
49 |
model_name="meta-llama/Llama-2-7b-chat-hf",
|
50 |
context_window=3900,
|
51 |
max_new_tokens=256,
|
52 |
-
generate_kwargs={"temperature":0.2, "do_sample":True},
|
53 |
device_map="auto",
|
54 |
model_kwargs={
|
55 |
"torch_dtype": torch.float16,
|
@@ -69,27 +70,21 @@ def get_chat_engine():
|
|
69 |
)
|
70 |
return _state["chat_engine"]
|
71 |
|
72 |
-
# ---------- 3.
|
73 |
-
def
|
74 |
if message.lower().strip() in {"quit", "exit", "done"}:
|
75 |
-
return "Thank you for your order! We'll see you soon."
|
76 |
|
77 |
engine = get_chat_engine()
|
78 |
response = engine.chat(message).response
|
79 |
-
|
80 |
-
return "", chat_history
|
81 |
-
|
82 |
-
with gr.Blocks(title="Café Eleven Chat") as demo:
|
83 |
-
gr.Markdown("## ☕ Café Eleven Ordering Assistant")
|
84 |
-
gr.Markdown("Type your order or question below. Type 'quit' to end the chat.")
|
85 |
-
|
86 |
-
chatbot = gr.Chatbot(height=500)
|
87 |
-
msg = gr.Textbox(label="Your message", placeholder="Hi, I'd like a latte...")
|
88 |
-
clear = gr.Button("Clear Chat")
|
89 |
-
|
90 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
91 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
92 |
|
93 |
-
#
|
94 |
if __name__ == "__main__":
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, torch
|
|
|
2 |
from pathlib import Path
|
3 |
from huggingface_hub import login
|
4 |
|
|
|
10 |
from llama_index.embeddings.langchain import LangchainEmbedding
|
11 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
12 |
|
13 |
+
# ---------- Constants ----------
|
14 |
SYSTEM_PROMPT = """
|
15 |
You are a friendly café assistant for Café Eleven. Your job is to:
|
16 |
1. Greet the customer warmly
|
|
|
18 |
3. Answer questions about ingredients, preparation, etc.
|
19 |
4. Process special requests (allergies, modifications)
|
20 |
5. Provide a friendly farewell
|
21 |
+
Always be polite and helpful!
|
22 |
+
"""
|
23 |
|
24 |
WRAPPER_PROMPT = PromptTemplate(
|
25 |
"[INST]<<SYS>>\n" + SYSTEM_PROMPT + "\n<</SYS>>\n\n{query_str} [/INST]"
|
26 |
)
|
27 |
|
28 |
+
# ---------- 1. Login & Load Data ----------
|
29 |
login(token=os.environ["HF_TOKEN"])
|
30 |
|
|
|
31 |
docs = SimpleDirectoryReader(
|
32 |
input_files=[str(p) for p in Path(".").glob("*.pdf")]
|
33 |
).load_data()
|
34 |
+
|
35 |
embed_model = LangchainEmbedding(
|
36 |
HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
37 |
)
|
|
|
40 |
|
41 |
index = VectorStoreIndex.from_documents(docs)
|
42 |
|
43 |
+
# ---------- 2. Initialize Chat Engine ----------
|
44 |
+
_state = {"chat_engine": None}
|
45 |
|
46 |
def get_chat_engine():
|
47 |
if _state["chat_engine"] is None:
|
|
|
50 |
model_name="meta-llama/Llama-2-7b-chat-hf",
|
51 |
context_window=3900,
|
52 |
max_new_tokens=256,
|
53 |
+
generate_kwargs={"temperature": 0.2, "do_sample": True},
|
54 |
device_map="auto",
|
55 |
model_kwargs={
|
56 |
"torch_dtype": torch.float16,
|
|
|
70 |
)
|
71 |
return _state["chat_engine"]
|
72 |
|
73 |
+
# ---------- 3. Simple Chat Function ----------
|
74 |
+
def chat_with_cafe_eleven(message: str) -> str:
|
75 |
if message.lower().strip() in {"quit", "exit", "done"}:
|
76 |
+
return "Thank you for your order! We'll see you soon."
|
77 |
|
78 |
engine = get_chat_engine()
|
79 |
response = engine.chat(message).response
|
80 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
# ---------- Example usage ----------
|
83 |
if __name__ == "__main__":
|
84 |
+
while True:
|
85 |
+
user_message = input("You: ")
|
86 |
+
bot_response = chat_with_cafe_eleven(user_message)
|
87 |
+
print("Café Eleven:", bot_response)
|
88 |
+
|
89 |
+
if user_message.lower().strip() in {"quit", "exit", "done"}:
|
90 |
+
break
|