Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_community.document_loaders import WebBaseLoader, PyMuPDFLoader
|
| 4 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
@@ -6,14 +9,9 @@ from langchain_community.vectorstores import FAISS
|
|
| 6 |
from langchain_community.llms import HuggingFaceHub
|
| 7 |
from langchain.chains.question_answering import load_qa_chain
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
# We get the token from the Space's secret environment variables
|
| 11 |
hf_token = os.environ.get("HF_TOKEN")
|
| 12 |
|
| 13 |
-
if not hf_token:
|
| 14 |
-
raise ValueError("HF_TOKEN not found in environment variables. Please set it in Space Settings.")
|
| 15 |
-
|
| 16 |
-
# --- LOGIC ---
|
| 17 |
def load_pdf(file_path):
|
| 18 |
loader = PyMuPDFLoader(file_path)
|
| 19 |
docs = loader.load()
|
|
@@ -34,7 +32,6 @@ def ask_question(query, vector_store):
|
|
| 34 |
retriever = vector_store.as_retriever()
|
| 35 |
docs = retriever.get_relevant_documents(query)
|
| 36 |
|
| 37 |
-
# Using HuggingFaceEndpoint (newer) or Hub to call Mixtral
|
| 38 |
llm = HuggingFaceHub(
|
| 39 |
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 40 |
model_kwargs={"temperature": 0.7, "max_length": 512},
|
|
@@ -48,7 +45,6 @@ def ask_question(query, vector_store):
|
|
| 48 |
def process_input(weblink, pdf_file, question):
|
| 49 |
docs = []
|
| 50 |
|
| 51 |
-
# Error handling for empty inputs
|
| 52 |
if not weblink and not pdf_file:
|
| 53 |
return "Please provide a website link or upload a PDF."
|
| 54 |
if not question:
|
|
@@ -58,7 +54,7 @@ def process_input(weblink, pdf_file, question):
|
|
| 58 |
if weblink:
|
| 59 |
docs.extend(load_website(weblink))
|
| 60 |
if pdf_file:
|
| 61 |
-
docs.extend(load_pdf(pdf_file.name))
|
| 62 |
|
| 63 |
vector_store = setup_vector_store(docs)
|
| 64 |
response = ask_question(question, vector_store)
|
|
@@ -75,8 +71,7 @@ demo = gr.Interface(
|
|
| 75 |
gr.Textbox(label="Ask a Question")
|
| 76 |
],
|
| 77 |
outputs=gr.Textbox(label="Final Answer"),
|
| 78 |
-
title="Web & PDF QA System"
|
| 79 |
-
description="Upload a PDF or enter a website URL to chat with the content."
|
| 80 |
)
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
+
# --- FIX 1: Set User Agent to prevent WebBaseLoader crash ---
|
| 3 |
+
os.environ["USER_AGENT"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
from langchain_community.document_loaders import WebBaseLoader, PyMuPDFLoader
|
| 7 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
| 9 |
from langchain_community.llms import HuggingFaceHub
|
| 10 |
from langchain.chains.question_answering import load_qa_chain
|
| 11 |
|
| 12 |
+
# Get the token from the secrets
|
|
|
|
| 13 |
hf_token = os.environ.get("HF_TOKEN")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def load_pdf(file_path):
|
| 16 |
loader = PyMuPDFLoader(file_path)
|
| 17 |
docs = loader.load()
|
|
|
|
| 32 |
retriever = vector_store.as_retriever()
|
| 33 |
docs = retriever.get_relevant_documents(query)
|
| 34 |
|
|
|
|
| 35 |
llm = HuggingFaceHub(
|
| 36 |
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 37 |
model_kwargs={"temperature": 0.7, "max_length": 512},
|
|
|
|
| 45 |
def process_input(weblink, pdf_file, question):
|
| 46 |
docs = []
|
| 47 |
|
|
|
|
| 48 |
if not weblink and not pdf_file:
|
| 49 |
return "Please provide a website link or upload a PDF."
|
| 50 |
if not question:
|
|
|
|
| 54 |
if weblink:
|
| 55 |
docs.extend(load_website(weblink))
|
| 56 |
if pdf_file:
|
| 57 |
+
docs.extend(load_pdf(pdf_file.name))
|
| 58 |
|
| 59 |
vector_store = setup_vector_store(docs)
|
| 60 |
response = ask_question(question, vector_store)
|
|
|
|
| 71 |
gr.Textbox(label="Ask a Question")
|
| 72 |
],
|
| 73 |
outputs=gr.Textbox(label="Final Answer"),
|
| 74 |
+
title="Web & PDF QA System"
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|