Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,9 +36,15 @@ def init_llm():
|
|
| 36 |
raise ValueError("HUGGINGFACEHUB_API_TOKEN is not set in environment variables.")
|
| 37 |
|
| 38 |
model_id = "tiiuae/falcon-rw-1b" # ✅ Can switch to "tiiuae/falcon-rw-1b" for lighter model
|
| 39 |
-
hf_pipeline = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
llm_pipeline = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 41 |
-
|
| 42 |
embeddings = HuggingFaceEmbeddings(
|
| 43 |
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 44 |
model_kwargs={"device": DEVICE}
|
|
@@ -63,7 +69,7 @@ def process_document(file):
|
|
| 63 |
|
| 64 |
# Load or create ChromaDB
|
| 65 |
db = Chroma.from_documents(texts, embedding=embeddings, persist_directory=persist_directory)
|
| 66 |
-
retriever = db.as_retriever(search_type="
|
| 67 |
|
| 68 |
conversation_retrieval_chain = ConversationalRetrievalChain.from_llm(
|
| 69 |
llm=llm_pipeline, retriever=retriever
|
|
@@ -82,11 +88,11 @@ def process_prompt(prompt, chat_history_display):
|
|
| 82 |
if not conversation_retrieval_chain:
|
| 83 |
return chat_history_display + [("❌ No document uploaded.", "Please upload a PDF first.")]
|
| 84 |
|
| 85 |
-
output = conversation_retrieval_chain({"question": prompt, "chat_history": chat_history})
|
| 86 |
answer = output["answer"]
|
| 87 |
|
| 88 |
chat_history.append((prompt, answer))
|
| 89 |
-
return
|
| 90 |
|
| 91 |
# Define Gradio UI
|
| 92 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 36 |
raise ValueError("HUGGINGFACEHUB_API_TOKEN is not set in environment variables.")
|
| 37 |
|
| 38 |
model_id = "tiiuae/falcon-rw-1b" # ✅ Can switch to "tiiuae/falcon-rw-1b" for lighter model
|
| 39 |
+
hf_pipeline = pipeline(
|
| 40 |
+
"text-generation",
|
| 41 |
+
model=model_id,
|
| 42 |
+
device=DEVICE,
|
| 43 |
+
max_new_tokens=512 # Increase this as needed
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
llm_pipeline = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 47 |
+
|
| 48 |
embeddings = HuggingFaceEmbeddings(
|
| 49 |
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 50 |
model_kwargs={"device": DEVICE}
|
|
|
|
| 69 |
|
| 70 |
# Load or create ChromaDB
|
| 71 |
db = Chroma.from_documents(texts, embedding=embeddings, persist_directory=persist_directory)
|
| 72 |
+
retriever = db.as_retriever(search_type="mmr", search_kwargs={'k': 6})
|
| 73 |
|
| 74 |
conversation_retrieval_chain = ConversationalRetrievalChain.from_llm(
|
| 75 |
llm=llm_pipeline, retriever=retriever
|
|
|
|
| 88 |
if not conversation_retrieval_chain:
|
| 89 |
return chat_history_display + [("❌ No document uploaded.", "Please upload a PDF first.")]
|
| 90 |
|
| 91 |
+
output = conversation_retrieval_chain.invoke({"question": prompt, "chat_history": chat_history})
|
| 92 |
answer = output["answer"]
|
| 93 |
|
| 94 |
chat_history.append((prompt, answer))
|
| 95 |
+
return answer
|
| 96 |
|
| 97 |
# Define Gradio UI
|
| 98 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|