Spaces:
Sleeping
Sleeping
Update app.py
Browse filesinitial commit
app.py
CHANGED
@@ -1,11 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,49 +35,33 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
|
19 |
-
|
|
|
20 |
for val in history:
|
21 |
if val[0]:
|
22 |
-
|
23 |
if val[1]:
|
24 |
-
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a
|
49 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
],
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from langchain.chains import RetrievalQA
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.vectorstores import FAISS
|
6 |
+
from langchain.llms import HuggingFaceHub
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
+
from langchain.chains import ConversationalRetrievalChain
|
9 |
|
10 |
+
# Load the HuggingFace language model and embeddings
|
|
|
|
|
11 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
12 |
|
13 |
+
# Initialize the embeddings model for document retrieval
|
14 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
15 |
+
|
16 |
+
# Set up FAISS as the vector store for document retrieval
|
17 |
+
# (Replace 'documents' with your actual document list or corpus)
|
18 |
+
texts = ["Document 1", "Document 2", "Document 3"]
|
19 |
+
vector_store = FAISS.from_texts(texts, embeddings)
|
20 |
+
|
21 |
+
# Initialize the LangChain RAG system
|
22 |
+
retriever = vector_store.as_retriever()
|
23 |
+
|
24 |
+
# Set up ConversationalRetrievalChain using LangChain's tools
|
25 |
+
rag_chain = ConversationalRetrievalChain.from_llm(
|
26 |
+
HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta"),
|
27 |
+
retriever=retriever
|
28 |
+
)
|
29 |
|
30 |
def respond(
|
31 |
message,
|
|
|
35 |
temperature,
|
36 |
top_p,
|
37 |
):
|
38 |
+
# Combine history with the user message
|
39 |
+
conversation_history = [{"role": "system", "content": system_message}]
|
40 |
+
|
41 |
for val in history:
|
42 |
if val[0]:
|
43 |
+
conversation_history.append({"role": "user", "content": val[0]})
|
44 |
if val[1]:
|
45 |
+
conversation_history.append({"role": "assistant", "content": val[1]})
|
|
|
|
|
46 |
|
47 |
+
conversation_history.append({"role": "user", "content": message})
|
48 |
|
49 |
+
# Retrieve documents using the retriever
|
50 |
+
response = rag_chain({"question": message, "chat_history": history})
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# Format and return the response
|
53 |
+
return response['answer']
|
54 |
|
55 |
+
# Gradio interface setup
|
|
|
|
|
56 |
demo = gr.ChatInterface(
|
57 |
respond,
|
58 |
additional_inputs=[
|
59 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
60 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
61 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
62 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
],
|
64 |
)
|
65 |
|
|
|
66 |
if __name__ == "__main__":
|
67 |
+
demo.launch()
|