Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,67 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
| 54 |
maximum=1.0,
|
|
|
|
| 1 |
+
# generic libraries
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# for embeddings and indexing
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
+
|
| 9 |
+
# for data retrieval
|
| 10 |
+
from langchain.chains import RetrievalQA
|
| 11 |
+
|
| 12 |
+
# for huggingface llms
|
| 13 |
+
from langchain_community.llms import HuggingFaceHub
|
| 14 |
+
|
| 15 |
+
# define constants
|
| 16 |
+
EMB_MODEL1 = 'BAAI/bge-base-en-v1.5'
|
| 17 |
+
MISTRAL_MODEL1 = 'mistralai/Mixtral-8x7B-Instruct-v0.1'
|
| 18 |
+
HF_MODEL1 = 'HuggingFaceH4/zephyr-7b-beta'
|
| 19 |
+
# define paths
|
| 20 |
+
vector_path = 'faiss_index'
|
| 21 |
+
|
| 22 |
+
# Initialize your embedding model
|
| 23 |
+
embedding_model = HuggingFaceEmbeddings(model_name=EMB_MODEL1)
|
| 24 |
+
|
| 25 |
+
# Load FAISS from relative path
|
| 26 |
+
if os.path.exists("faiss_index"):
|
| 27 |
+
vectordb = FAISS.load_local(vector_path, embedding_model)
|
| 28 |
+
else:
|
| 29 |
+
raise FileNotFoundError("FAISS index not found in Space. Please upload it to faiss_index/")
|
| 30 |
|
| 31 |
|
| 32 |
def respond(
|
| 33 |
message,
|
| 34 |
+
#history: list[tuple[str, str]],
|
| 35 |
+
#system_message,
|
| 36 |
max_tokens,
|
| 37 |
temperature,
|
| 38 |
top_p,
|
| 39 |
+
vectordb,
|
| 40 |
+
embedding_model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# define retriever object
|
| 43 |
+
retriever = vectordb.as_retriever(search_type="similarity", search_kwargs={"k": top_p})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# initialse chatbot llm
|
| 46 |
+
llm = HuggingFaceHub(
|
| 47 |
+
repo_id=MISTRAL_MODEL1,
|
| 48 |
+
token=os.environ["HUGGINGFACEHUB_API_TOKEN"], #huggingfacehub_api_token=SECRET_TOKEN_HF,
|
| 49 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens}
|
| 50 |
+
)
|
| 51 |
+
# create a RAG pipeline
|
| 52 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
| 53 |
+
#generate results
|
| 54 |
+
result = qa_chain.invoke(query)
|
| 55 |
+
|
| 56 |
+
yield result['result']
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
demo = gr.ChatInterface(
|
| 60 |
respond,
|
| 61 |
additional_inputs=[
|
| 62 |
+
#gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 63 |
+
gr.Slider(minimum=128, maximum=1024, value=512, step=128, label="Max new tokens"),
|
| 64 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
|
| 65 |
gr.Slider(
|
| 66 |
minimum=0.1,
|
| 67 |
maximum=1.0,
|