Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,44 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
| 4 |
from langchain.chains import RetrievalQA
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
-
from
|
| 7 |
from langchain_community.llms import HuggingFacePipeline
|
| 8 |
|
| 9 |
# ------------------ LOAD EMBEDDINGS ------------------
|
|
|
|
| 10 |
embeddings = HuggingFaceEmbeddings(
|
| 11 |
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 12 |
)
|
| 13 |
|
| 14 |
# ------------------ LOAD VECTOR STORE ------------------
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# ------------------ LOAD LLM ------------------
|
| 22 |
-
#
|
|
|
|
|
|
|
| 23 |
text_gen_pipeline = pipeline(
|
| 24 |
"text-generation",
|
| 25 |
model="microsoft/phi-2",
|
| 26 |
-
max_new_tokens=
|
| 27 |
temperature=0.2,
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
|
|
@@ -41,7 +55,12 @@ def chat(user_message, history):
|
|
| 41 |
if not user_message.strip():
|
| 42 |
return history
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
history.append((user_message, answer))
|
| 46 |
return history
|
| 47 |
|
|
@@ -54,15 +73,20 @@ with gr.Blocks(title="Document RAG Chatbot") as demo:
|
|
| 54 |
"""
|
| 55 |
)
|
| 56 |
|
| 57 |
-
chatbot = gr.Chatbot(height=
|
| 58 |
query = gr.Textbox(
|
| 59 |
label="Ask a question",
|
| 60 |
placeholder="Ask something from the documents..."
|
| 61 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
query.submit(chat, [query, chatbot], chatbot)
|
|
|
|
| 66 |
clear_btn.click(lambda: [], None, chatbot)
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# specific imports to fix "ModuleNotFoundError"
|
| 6 |
from langchain.chains import RetrievalQA
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 9 |
from langchain_community.llms import HuggingFacePipeline
|
| 10 |
|
| 11 |
# ------------------ LOAD EMBEDDINGS ------------------
|
| 12 |
+
# We use a standard efficient embedding model
|
| 13 |
embeddings = HuggingFaceEmbeddings(
|
| 14 |
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 15 |
)
|
| 16 |
|
| 17 |
# ------------------ LOAD VECTOR STORE ------------------
|
| 18 |
+
# Check if vectorstore exists to avoid crashing
|
| 19 |
+
if not os.path.exists("vectorstore/faiss_index"):
|
| 20 |
+
print("❌ ERROR: 'vectorstore/faiss_index' folder not found.")
|
| 21 |
+
print(" Please run your ingest/indexing script first to create the database.")
|
| 22 |
+
# Create a dummy empty DB just so the app doesn't crash immediately (optional)
|
| 23 |
+
db = FAISS.from_texts(["Empty index"], embeddings)
|
| 24 |
+
else:
|
| 25 |
+
db = FAISS.load_local(
|
| 26 |
+
"vectorstore/faiss_index",
|
| 27 |
+
embeddings,
|
| 28 |
+
allow_dangerous_deserialization=True
|
| 29 |
+
)
|
| 30 |
|
| 31 |
# ------------------ LOAD LLM ------------------
|
| 32 |
+
# Using phi-2.
|
| 33 |
+
# WARNING: If the Space crashes with "OOM" (Out of Memory), change this to "google/flan-t5-small"
|
| 34 |
+
print("Loading Model...")
|
| 35 |
text_gen_pipeline = pipeline(
|
| 36 |
"text-generation",
|
| 37 |
model="microsoft/phi-2",
|
| 38 |
+
max_new_tokens=256, # Reduced slightly to save memory
|
| 39 |
temperature=0.2,
|
| 40 |
+
do_sample=True,
|
| 41 |
+
truncation=True
|
| 42 |
)
|
| 43 |
|
| 44 |
llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
|
|
|
|
| 55 |
if not user_message.strip():
|
| 56 |
return history
|
| 57 |
|
| 58 |
+
try:
|
| 59 |
+
# 'invoke' is the new standard, but 'run' is kept for compatibility with your code
|
| 60 |
+
answer = qa_chain.run(user_message)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
answer = f"Error generating answer: {str(e)}"
|
| 63 |
+
|
| 64 |
history.append((user_message, answer))
|
| 65 |
return history
|
| 66 |
|
|
|
|
| 73 |
"""
|
| 74 |
)
|
| 75 |
|
| 76 |
+
chatbot = gr.Chatbot(height=400)
|
| 77 |
query = gr.Textbox(
|
| 78 |
label="Ask a question",
|
| 79 |
placeholder="Ask something from the documents..."
|
| 80 |
)
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 84 |
+
clear_btn = gr.Button("Clear Chat")
|
| 85 |
|
| 86 |
+
# Wire up the buttons
|
|
|
|
| 87 |
query.submit(chat, [query, chatbot], chatbot)
|
| 88 |
+
submit_btn.click(chat, [query, chatbot], chatbot)
|
| 89 |
clear_btn.click(lambda: [], None, chatbot)
|
| 90 |
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|