|
import os |
|
import zipfile |
|
import gradio as gr |
|
|
|
from langchain_community.llms import HuggingFaceEndpoint |
|
from langchain_community.vectorstores import Chroma |
|
from langchain.chains import RetrievalQA |
|
|
|
|
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
os.environ["HF_TOKEN"] = HF_TOKEN |
|
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN |
|
|
|
|
|
|
|
|
|
llm = HuggingFaceEndpoint( |
|
repo_id="HuggingFaceH4/zephyr-7b-beta", |
|
task="text-generation", |
|
max_new_tokens = 512, |
|
top_k = 30, |
|
temperature = 0.1, |
|
repetition_penalty = 1.03, |
|
) |
|
|
|
|
|
|
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
|
|
modelPath ="mixedbread-ai/mxbai-embed-large-v1" |
|
|
|
|
|
model_kwargs = {'device':'cpu'} |
|
|
|
|
|
encode_kwargs = {'normalize_embeddings': False} |
|
|
|
embedding = HuggingFaceEmbeddings( |
|
model_name=modelPath, |
|
model_kwargs=model_kwargs, |
|
encode_kwargs=encode_kwargs |
|
) |
|
|
|
|
|
|
|
with zipfile.ZipFile('docs.zip', 'r') as zip_ref: |
|
zip_ref.extractall() |
|
|
|
persist_directory = 'docs/chroma/' |
|
|
|
vectordb = Chroma( |
|
persist_directory=persist_directory, |
|
embedding_function=embedding |
|
) |
|
|
|
|
|
title = "Q&A on enterprise data" |
|
description = "Implementation of Open Source RAG on Private Document" |
|
|
|
def quena(question): |
|
qa_chain = RetrievalQA.from_chain_type(llm, retriever=vectordb.as_retriever(), return_source_documents=True) |
|
result = qa_chain.invoke({"query": question}) |
|
return result["result"] |
|
|
|
demo=gr.Interface(fn=quena, |
|
inputs=gr.Textbox(lines=10,placeholder='''Write your question inside double quotation..Type the Sample Question:\n |
|
What are the procedures to move from research to production environment?? Reply in step-wise pointers.'''), |
|
outputs="text", |
|
title=title, |
|
description=description,) |
|
|
|
demo.launch() |
|
|
|
|
|
|
|
|