Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import bs4
|
| 3 |
-
from langchain.embeddings.huggingface import HuggingFaceBgeEmbeddings
|
| 4 |
-
from langchain.document_loaders import WebBaseLoader, PyPDFDirectoryLoader
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
-
from
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Function to load, split, and retrieve documents
|
| 10 |
def load_and_retrieve_docs(url):
|
| 11 |
loader = WebBaseLoader(
|
| 12 |
web_paths=(url,),
|
|
@@ -21,9 +20,9 @@ def load_and_retrieve_docs(url):
|
|
| 21 |
vectorstore = FAISS.from_documents(documents=splits, embedding=embeddings)
|
| 22 |
return vectorstore.as_retriever()
|
| 23 |
|
| 24 |
-
# Function to format documents
|
| 25 |
def format_docs(docs):
|
| 26 |
-
return "\n\n".join(
|
| 27 |
|
| 28 |
# Function that defines the RAG chain
|
| 29 |
def rag_chain(url, question):
|
|
@@ -31,12 +30,8 @@ def rag_chain(url, question):
|
|
| 31 |
retrieved_docs = retriever.invoke(question)
|
| 32 |
formatted_context = format_docs(retrieved_docs)
|
| 33 |
formatted_prompt = f"Question: {question}\n\nContext: {formatted_context}"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
chat_pipeline = pipeline('text-generation', model='gpt-3.5-turbo') # Use the appropriate model here
|
| 37 |
-
response = chat_pipeline(formatted_prompt, max_length=512, num_return_sequences=1)
|
| 38 |
-
|
| 39 |
-
return response[0]['generated_text']
|
| 40 |
|
| 41 |
# Gradio interface
|
| 42 |
iface = gr.Interface(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
|
| 6 |
+
import ollama
|
| 7 |
|
| 8 |
+
# Function to load, split, and retrieve documents
|
| 9 |
def load_and_retrieve_docs(url):
|
| 10 |
loader = WebBaseLoader(
|
| 11 |
web_paths=(url,),
|
|
|
|
| 20 |
vectorstore = FAISS.from_documents(documents=splits, embedding=embeddings)
|
| 21 |
return vectorstore.as_retriever()
|
| 22 |
|
| 23 |
+
# Function to format documents
|
| 24 |
def format_docs(docs):
|
| 25 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
| 26 |
|
| 27 |
# Function that defines the RAG chain
|
| 28 |
def rag_chain(url, question):
|
|
|
|
| 30 |
retrieved_docs = retriever.invoke(question)
|
| 31 |
formatted_context = format_docs(retrieved_docs)
|
| 32 |
formatted_prompt = f"Question: {question}\n\nContext: {formatted_context}"
|
| 33 |
+
response = ollama.chat(model='llama3', messages=[{'role': 'user', 'content': formatted_prompt}])
|
| 34 |
+
return response['message']['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Gradio interface
|
| 37 |
iface = gr.Interface(
|