Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,19 +25,13 @@ User: {question}
|
|
| 25 |
Assistant:"""
|
| 26 |
prompt = PromptTemplate(template=prompt_template, input_variables=["question"])
|
| 27 |
chain = LLMChain(llm=llm, prompt=prompt)
|
| 28 |
-
|
| 29 |
-
# Load and split the document for the retrieval-based QA system
|
| 30 |
with open("brookline_data.txt", "r") as f:
|
| 31 |
data = f.read()
|
| 32 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
|
| 33 |
splitted_data = text_splitter.split_text(data)
|
| 34 |
-
|
| 35 |
-
# Generate embeddings for the split text
|
| 36 |
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-mpnet-base-v2')
|
| 37 |
metadatas = [{"source": f"source_{i}"} for i in range(len(splitted_data))]
|
| 38 |
documents = [Document(page_content=text, metadata=metadata) for text, metadata in zip(splitted_data, metadatas)]
|
| 39 |
-
|
| 40 |
-
# Initialize Qdrant vector store
|
| 41 |
qdrant = Qdrant.from_documents(
|
| 42 |
documents,
|
| 43 |
embeddings,
|
|
@@ -46,7 +40,7 @@ qdrant = Qdrant.from_documents(
|
|
| 46 |
)
|
| 47 |
retriever = qdrant.as_retriever()
|
| 48 |
qna = RetrievalQA.from_chain_type(
|
| 49 |
-
llm=HuggingFaceHub(repo_id="ahmadmac/Trained-T5-large", model_kwargs={"temperature": 0.
|
| 50 |
huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"]),
|
| 51 |
chain_type="stuff",
|
| 52 |
retriever=retriever
|
|
@@ -55,11 +49,15 @@ def chatbot(question, chat_history):
|
|
| 55 |
response = chain.run(question)
|
| 56 |
retrieval_result = qna(question)
|
| 57 |
retrieval_answer = retrieval_result['result']
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return combined_response
|
| 60 |
demo = gr.ChatInterface(
|
| 61 |
fn=chatbot,
|
| 62 |
title="Chatbot",
|
| 63 |
-
description="AI Assistant
|
| 64 |
)
|
| 65 |
demo.launch()
|
|
|
|
| 25 |
Assistant:"""
|
| 26 |
prompt = PromptTemplate(template=prompt_template, input_variables=["question"])
|
| 27 |
chain = LLMChain(llm=llm, prompt=prompt)
|
|
|
|
|
|
|
| 28 |
with open("brookline_data.txt", "r") as f:
|
| 29 |
data = f.read()
|
| 30 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
|
| 31 |
splitted_data = text_splitter.split_text(data)
|
|
|
|
|
|
|
| 32 |
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-mpnet-base-v2')
|
| 33 |
metadatas = [{"source": f"source_{i}"} for i in range(len(splitted_data))]
|
| 34 |
documents = [Document(page_content=text, metadata=metadata) for text, metadata in zip(splitted_data, metadatas)]
|
|
|
|
|
|
|
| 35 |
qdrant = Qdrant.from_documents(
|
| 36 |
documents,
|
| 37 |
embeddings,
|
|
|
|
| 40 |
)
|
| 41 |
retriever = qdrant.as_retriever()
|
| 42 |
qna = RetrievalQA.from_chain_type(
|
| 43 |
+
llm=HuggingFaceHub(repo_id="ahmadmac/Trained-T5-large", model_kwargs={"temperature": 0.8, "max_length": 512},
|
| 44 |
huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"]),
|
| 45 |
chain_type="stuff",
|
| 46 |
retriever=retriever
|
|
|
|
| 49 |
response = chain.run(question)
|
| 50 |
retrieval_result = qna(question)
|
| 51 |
retrieval_answer = retrieval_result['result']
|
| 52 |
+
if retrival_answer.strip():
|
| 53 |
+
combined_response=f"Based on the information available:\n{retrieval_answer}"
|
| 54 |
+
else:
|
| 55 |
+
combined_response=f"Response through LLM:\n{response}"
|
| 56 |
+
#combined_response = f"{response}\n\nBased on the information available:\n{retrieval_answer}"
|
| 57 |
return combined_response
|
| 58 |
demo = gr.ChatInterface(
|
| 59 |
fn=chatbot,
|
| 60 |
title="Chatbot",
|
| 61 |
+
description="AI Assistant!"
|
| 62 |
)
|
| 63 |
demo.launch()
|