Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
from langchain.chains import RetrievalQA
|
| 4 |
from langchain.vectorstores import FAISS
|
| 5 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
-
from langchain.document_loaders import TextLoader
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 13 |
-
qa_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=512)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
# Knowledge base for Crustdata APIs
|
| 17 |
docs = """
|
| 18 |
# Crustdata Dataset API
|
| 19 |
|
|
@@ -153,35 +148,71 @@ The Crustdata Discovery and Enrichment API allows users to enrich their datasets
|
|
| 153 |
- Base URL: `https://api.crustdata.com`
|
| 154 |
"""
|
| 155 |
|
| 156 |
-
# Split the documentation into chunks
|
| 157 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=
|
| 158 |
doc_chunks = text_splitter.create_documents([docs])
|
| 159 |
|
| 160 |
-
#
|
| 161 |
embedding_model = "sentence-transformers/all-MiniLM-L6-v2"
|
| 162 |
embeddings = HuggingFaceEmbeddings(model_name=embedding_model)
|
| 163 |
docsearch = FAISS.from_documents(doc_chunks, embeddings)
|
| 164 |
|
| 165 |
-
# Create a QA chain
|
| 166 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 167 |
-
llm=qa_pipeline,
|
| 168 |
-
retriever=docsearch.as_retriever(),
|
| 169 |
-
return_source_documents=True
|
| 170 |
-
)
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
description="Ask any technical questions about Crustdata’s Dataset and Discovery APIs.",
|
| 184 |
)
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
from langchain.vectorstores import FAISS
|
| 4 |
from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain.document_loaders import TextLoader
|
| 7 |
|
| 8 |
+
# Initialize the Hugging Face Inference client with an open-source LLM
|
| 9 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") # You can use any supported model
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Sample knowledge base for Crustdata APIs
|
|
|
|
| 12 |
docs = """
|
| 13 |
# Crustdata Dataset API
|
| 14 |
|
|
|
|
| 148 |
- Base URL: `https://api.crustdata.com`
|
| 149 |
"""
|
| 150 |
|
| 151 |
+
# Split the documentation into smaller chunks
|
| 152 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 153 |
doc_chunks = text_splitter.create_documents([docs])
|
| 154 |
|
| 155 |
+
# Create embeddings and initialize FAISS vector store
|
| 156 |
embedding_model = "sentence-transformers/all-MiniLM-L6-v2"
|
| 157 |
embeddings = HuggingFaceEmbeddings(model_name=embedding_model)
|
| 158 |
docsearch = FAISS.from_documents(doc_chunks, embeddings)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
def retrieve_context(query):
|
| 162 |
+
"""Retrieve the most relevant context from the knowledge base."""
|
| 163 |
+
results = docsearch.similarity_search(query, k=2) # Retrieve top 2 most similar chunks
|
| 164 |
+
context = "\n".join([res.page_content for res in results])
|
| 165 |
+
return context
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def respond(
|
| 169 |
+
message,
|
| 170 |
+
history: list[tuple[str, str]],
|
| 171 |
+
system_message,
|
| 172 |
+
max_tokens,
|
| 173 |
+
temperature,
|
| 174 |
+
top_p,
|
| 175 |
+
):
|
| 176 |
+
"""Generate a response using the Hugging Face Inference API."""
|
| 177 |
+
# Retrieve relevant context from the knowledge base
|
| 178 |
+
context = retrieve_context(message)
|
| 179 |
+
prompt = f"{system_message}\n\nContext:\n{context}\n\nUser: {message}\nAssistant:"
|
| 180 |
+
|
| 181 |
+
messages = [{"role": "system", "content": system_message}]
|
| 182 |
+
for val in history:
|
| 183 |
+
if val[0]:
|
| 184 |
+
messages.append({"role": "user", "content": val[0]})
|
| 185 |
+
if val[1]:
|
| 186 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 187 |
+
|
| 188 |
+
messages.append({"role": "user", "content": prompt})
|
| 189 |
+
|
| 190 |
+
response = ""
|
| 191 |
+
|
| 192 |
+
for message in client.chat_completion(
|
| 193 |
+
messages,
|
| 194 |
+
max_tokens=max_tokens,
|
| 195 |
+
stream=True,
|
| 196 |
+
temperature=temperature,
|
| 197 |
+
top_p=top_p,
|
| 198 |
+
):
|
| 199 |
+
token = message.choices[0].delta.content
|
| 200 |
+
response += token
|
| 201 |
+
yield response
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
# Gradio interface
|
| 205 |
+
demo = gr.ChatInterface(
|
| 206 |
+
respond,
|
| 207 |
+
additional_inputs=[
|
| 208 |
+
gr.Textbox(value="You are a technical assistant for Crustdata APIs.", label="System message"),
|
| 209 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 210 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 211 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 212 |
+
],
|
| 213 |
+
title="Crustdata API Chatbot",
|
| 214 |
description="Ask any technical questions about Crustdata’s Dataset and Discovery APIs.",
|
| 215 |
)
|
| 216 |
|
| 217 |
+
if __name__ == "__main__":
|
| 218 |
+
demo.launch(share=True)
|