Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
+
from langchain.llms import HuggingFacePipeline
|
| 9 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 10 |
+
from langchain.memory import ConversationBufferMemory
|
| 11 |
+
from langchain.prompts import PromptTemplate
|
| 12 |
+
|
| 13 |
+
# --- Step 1: Unzip FAISS index ---
|
| 14 |
+
if not os.path.exists("faiss_index") and os.path.exists("faiss_index.zip"):
|
| 15 |
+
with zipfile.ZipFile("faiss_index.zip", "r") as zip_ref:
|
| 16 |
+
zip_ref.extractall(".")
|
| 17 |
+
|
| 18 |
+
# --- Step 2: Load embedding and vectorstore ---
|
| 19 |
+
embedding_model = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 20 |
+
vectordb = FAISS.load_local("faiss_index", embedding_model)
|
| 21 |
+
|
| 22 |
+
# --- Step 3: Load the LLM ---
|
| 23 |
+
model_id = "tiiuae/falcon3-1b-instruct"
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 25 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 26 |
+
|
| 27 |
+
pipe = pipeline(
|
| 28 |
+
"text-generation",
|
| 29 |
+
model=model,
|
| 30 |
+
tokenizer=tokenizer,
|
| 31 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 32 |
+
max_new_tokens=200,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
temperature=1.0,
|
| 35 |
+
)
|
| 36 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
| 37 |
+
|
| 38 |
+
# --- Step 4: Setup memory and QA chain ---
|
| 39 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 40 |
+
|
| 41 |
+
prompt = PromptTemplate.from_template("""
|
| 42 |
+
You are a helpful assistant at the University of Hertfordshire. Use the context below to answer the question clearly and factually.
|
| 43 |
+
If the answer is not in the context, say you don't know.
|
| 44 |
+
|
| 45 |
+
Context:
|
| 46 |
+
{context}
|
| 47 |
+
|
| 48 |
+
Question:
|
| 49 |
+
{question}
|
| 50 |
+
|
| 51 |
+
Helpful Answer:
|
| 52 |
+
""")
|
| 53 |
+
|
| 54 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 55 |
+
llm=llm,
|
| 56 |
+
retriever=vectordb.as_retriever(search_kwargs={"k": 3}),
|
| 57 |
+
memory=memory,
|
| 58 |
+
chain_type="map_reduce",
|
| 59 |
+
combine_docs_chain_kwargs={"prompt": prompt}
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# --- Step 5: Define chatbot logic ---
|
| 63 |
+
def chat(message, history):
|
| 64 |
+
result = qa_chain.invoke({"question": message})
|
| 65 |
+
response = result.get("answer", "")
|
| 66 |
+
response = response.split("Answer:")[-1].replace("<|assistant|>", "").strip()
|
| 67 |
+
return response
|
| 68 |
+
|
| 69 |
+
# UH logo
|
| 70 |
+
UH_LOGO = "images/UH.png"
|
| 71 |
+
|
| 72 |
+
# --- Step 6: UI ---
|
| 73 |
+
sample_questions = [
|
| 74 |
+
"How do I register as a new student?",
|
| 75 |
+
"Where can I find accommodation?",
|
| 76 |
+
"Can I renew my tenancy agreement?",
|
| 77 |
+
"What do I do on my first day?",
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
gr.Image(UH_LOGO, show_label=False, container=False, scale=1)
|
| 82 |
+
gr.Markdown("## ASK Herts Students Help Chatbot 🤖")
|
| 83 |
+
|
| 84 |
+
chatbot = gr.Chatbot()
|
| 85 |
+
txt = gr.Textbox(placeholder="Ask me anything about university life...", label="Your question")
|
| 86 |
+
submit = gr.Button("Submit")
|
| 87 |
+
|
| 88 |
+
gr.Markdown("#### 💡 Sample Questions:")
|
| 89 |
+
with gr.Row():
|
| 90 |
+
for q in sample_questions:
|
| 91 |
+
gr.Button(q).click(lambda x=q: gr.update(value=x), outputs=[txt])
|
| 92 |
+
|
| 93 |
+
def respond(message, history):
|
| 94 |
+
answer = chat(message, history)
|
| 95 |
+
history.append((message, answer))
|
| 96 |
+
return "", history
|
| 97 |
+
|
| 98 |
+
submit.click(respond, [txt, chatbot], [txt, chatbot])
|
| 99 |
+
txt.submit(respond, [txt, chatbot], [txt, chatbot])
|
| 100 |
+
|
| 101 |
+
demo.launch()
|