Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import random | |
| import time | |
| import boto3 | |
| from botocore import UNSIGNED | |
| from botocore.client import Config | |
| import zipfile | |
| from langchain.llms import HuggingFaceHub | |
| model_id = HuggingFaceHub(repo_id="tiiuae/falcon-7b-instruct", model_kwargs={"temperature":0.1, "max_new_tokens":1024}) | |
| from langchain.embeddings import HuggingFaceHubEmbeddings | |
| embeddings = HuggingFaceHubEmbeddings() | |
| from langchain.vectorstores import FAISS | |
| from langchain.chains import RetrievalQA | |
| s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED)) | |
| s3.download_file('rad-rag-demos', 'vectorstores/faiss_db_ray.zip', './chroma_db/faiss_db_ray.zip') | |
| with zipfile.ZipFile('./chroma_db/faiss_db_ray.zip', 'r') as zip_ref: | |
| zip_ref.extractall('./chroma_db/') | |
| FAISS_INDEX_PATH='./chroma_db/faiss_db_ray' | |
| #embeddings = HuggingFaceHubEmbeddings("multi-qa-mpnet-base-dot-v1") | |
| embeddings = HuggingFaceHubEmbeddings() | |
| db = FAISS.load_local(FAISS_INDEX_PATH, embeddings) | |
| retriever = db.as_retriever(search_type = "mmr") | |
| global qa | |
| qa = RetrievalQA.from_chain_type(llm=model_id, chain_type="stuff", retriever=retriever) | |
| def add_text(history, text): | |
| history = history + [(text, None)] | |
| return history, "" | |
| def bot(history): | |
| response = infer(history[-1][0]) | |
| history[-1][1] = response['result'] | |
| return history | |
| def infer(question): | |
| query = question | |
| result = qa({"query": query}) | |
| return result | |
| css=""" | |
| #col-container {max-width: 700px; margin-left: auto; margin-right: auto;} | |
| """ | |
| title = """ | |
| <div style="text-align: center;max-width: 700px;"> | |
| <h1>Chat with the RAY Docs</h1> | |
| <p style="text-align: center;">The AI bot is here to help you with the RAY Documentation, <br /> | |
| start asking questions about the open-source software </p> | |
| </div> | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.HTML(title) | |
| chatbot = gr.Chatbot([], elem_id="chatbot") | |
| with gr.Row(): | |
| question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ") | |
| question.submit(add_text, [chatbot, question], [chatbot, question]).then( | |
| bot, chatbot, chatbot | |
| ) | |
| demo.launch() |