Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -4,7 +4,8 @@ import torch
|
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
import gradio as gr
|
| 7 |
-
|
|
|
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
from langchain.document_loaders import TextLoader
|
| 10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
@@ -13,20 +14,22 @@ from langchain.vectorstores import FAISS as LangChainFAISS
|
|
| 13 |
from langchain.docstore import InMemoryDocstore
|
| 14 |
from langchain.schema import Document
|
| 15 |
from langchain.llms import HuggingFacePipeline
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
if os.path.exists("md_knowledge_base.zip"):
|
| 19 |
with zipfile.ZipFile("md_knowledge_base.zip", "r") as zip_ref:
|
| 20 |
zip_ref.extractall("md_knowledge_base")
|
| 21 |
print("✅ Knowledge base extracted.")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
KB_PATH = "md_knowledge_base"
|
| 25 |
files = [os.path.join(dp, f) for dp, _, fn in os.walk(KB_PATH) for f in fn if f.endswith(".md")]
|
| 26 |
docs = [doc for f in files for doc in TextLoader(f, encoding="utf-8").load()]
|
| 27 |
print(f"✅ Loaded {len(docs)} documents.")
|
| 28 |
|
| 29 |
-
#
|
| 30 |
def get_dynamic_chunk_size(text):
|
| 31 |
if len(text) < 1000:
|
| 32 |
return 300
|
|
@@ -42,8 +45,8 @@ for doc in docs:
|
|
| 42 |
chunks.extend(chunk_splitter.split_documents([doc]))
|
| 43 |
texts = [chunk.page_content for chunk in chunks]
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
embed_model_id = "
|
| 47 |
embedder = SentenceTransformer(embed_model_id)
|
| 48 |
embeddings = embedder.encode(texts, show_progress_bar=False)
|
| 49 |
|
|
@@ -63,11 +66,15 @@ vectorstore = LangChainFAISS(
|
|
| 63 |
embedding_function=embed_fn
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
tokenizer =
|
| 70 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
text_gen_pipeline = pipeline(
|
| 73 |
"text-generation",
|
|
@@ -83,7 +90,6 @@ text_gen_pipeline = pipeline(
|
|
| 83 |
|
| 84 |
llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
|
| 85 |
|
| 86 |
-
# === 6. Prompt Format and Q&A ===
|
| 87 |
def truncate_context(context, max_length=1024):
|
| 88 |
tokens = tokenizer.encode(context)
|
| 89 |
if len(tokens) > max_length:
|
|
@@ -92,7 +98,7 @@ def truncate_context(context, max_length=1024):
|
|
| 92 |
|
| 93 |
def format_prompt(context, question):
|
| 94 |
return (
|
| 95 |
-
"You are the
|
| 96 |
"helping students with questions about courses, admissions, tuition fees, and student life. "
|
| 97 |
"Use ONLY the information provided in the context below to answer the question. "
|
| 98 |
"If the answer cannot be found in the context, reply: \"I’m sorry, but I don’t have that "
|
|
@@ -114,17 +120,17 @@ def answer_fn(question):
|
|
| 114 |
except Exception as e:
|
| 115 |
return f"An error occurred: {e}"
|
| 116 |
|
| 117 |
-
#
|
| 118 |
def chat_fn(user_message, history):
|
| 119 |
bot_response = answer_fn(user_message)
|
| 120 |
history = history + [(user_message, bot_response)]
|
| 121 |
return history, history
|
| 122 |
|
| 123 |
with gr.Blocks() as demo:
|
| 124 |
-
gr.Markdown("## 📘 University of
|
| 125 |
chatbot = gr.Chatbot()
|
| 126 |
state = gr.State([])
|
| 127 |
-
user_input = gr.Textbox(placeholder="Ask a question about
|
| 128 |
|
| 129 |
user_input.submit(fn=chat_fn, inputs=[user_input, state], outputs=[chatbot, state])
|
| 130 |
|
|
|
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
import gradio as gr
|
| 7 |
+
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
from langchain.document_loaders import TextLoader
|
| 11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
| 14 |
from langchain.docstore import InMemoryDocstore
|
| 15 |
from langchain.schema import Document
|
| 16 |
from langchain.llms import HuggingFacePipeline
|
| 17 |
+
from huggingface_hub import login
|
| 18 |
+
from huggingface_hub import upload_file
|
| 19 |
|
| 20 |
+
# Extract the Knowledge Base ZIP
|
| 21 |
if os.path.exists("md_knowledge_base.zip"):
|
| 22 |
with zipfile.ZipFile("md_knowledge_base.zip", "r") as zip_ref:
|
| 23 |
zip_ref.extractall("md_knowledge_base")
|
| 24 |
print("✅ Knowledge base extracted.")
|
| 25 |
|
| 26 |
+
# Load Markdown Files
|
| 27 |
KB_PATH = "md_knowledge_base"
|
| 28 |
files = [os.path.join(dp, f) for dp, _, fn in os.walk(KB_PATH) for f in fn if f.endswith(".md")]
|
| 29 |
docs = [doc for f in files for doc in TextLoader(f, encoding="utf-8").load()]
|
| 30 |
print(f"✅ Loaded {len(docs)} documents.")
|
| 31 |
|
| 32 |
+
# Chunking
|
| 33 |
def get_dynamic_chunk_size(text):
|
| 34 |
if len(text) < 1000:
|
| 35 |
return 300
|
|
|
|
| 45 |
chunks.extend(chunk_splitter.split_documents([doc]))
|
| 46 |
texts = [chunk.page_content for chunk in chunks]
|
| 47 |
|
| 48 |
+
# Vectorstore (FAISS)
|
| 49 |
+
embed_model_id = "sentence-transformers/all-MiniLM-L6-v2"
|
| 50 |
embedder = SentenceTransformer(embed_model_id)
|
| 51 |
embeddings = embedder.encode(texts, show_progress_bar=False)
|
| 52 |
|
|
|
|
| 66 |
embedding_function=embed_fn
|
| 67 |
)
|
| 68 |
|
| 69 |
+
print("✅ FAISS vectorstore ready.")
|
| 70 |
|
| 71 |
+
# Load Falcon-e-1B-Instruct
|
| 72 |
+
model_id = "tiiuae/falcon-e-1b-instruct"
|
| 73 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 74 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 75 |
+
model_id,
|
| 76 |
+
torch_dtype=torch.bfloat16
|
| 77 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 78 |
|
| 79 |
text_gen_pipeline = pipeline(
|
| 80 |
"text-generation",
|
|
|
|
| 90 |
|
| 91 |
llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
|
| 92 |
|
|
|
|
| 93 |
def truncate_context(context, max_length=1024):
|
| 94 |
tokens = tokenizer.encode(context)
|
| 95 |
if len(tokens) > max_length:
|
|
|
|
| 98 |
|
| 99 |
def format_prompt(context, question):
|
| 100 |
return (
|
| 101 |
+
"You are the Hull University Assistant—a friendly, knowledgeable chatbot dedicated to "
|
| 102 |
"helping students with questions about courses, admissions, tuition fees, and student life. "
|
| 103 |
"Use ONLY the information provided in the context below to answer the question. "
|
| 104 |
"If the answer cannot be found in the context, reply: \"I’m sorry, but I don’t have that "
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
return f"An error occurred: {e}"
|
| 122 |
|
| 123 |
+
# Gradio Interface
|
| 124 |
def chat_fn(user_message, history):
|
| 125 |
bot_response = answer_fn(user_message)
|
| 126 |
history = history + [(user_message, bot_response)]
|
| 127 |
return history, history
|
| 128 |
|
| 129 |
with gr.Blocks() as demo:
|
| 130 |
+
gr.Markdown("## 📘 University of Hull Assistant")
|
| 131 |
chatbot = gr.Chatbot()
|
| 132 |
state = gr.State([])
|
| 133 |
+
user_input = gr.Textbox(placeholder="Ask a question about University of Hull...", show_label=False)
|
| 134 |
|
| 135 |
user_input.submit(fn=chat_fn, inputs=[user_input, state], outputs=[chatbot, state])
|
| 136 |
|