Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from langchain_community.vectorstores import FAISS
|
@@ -5,49 +6,66 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
5 |
from langchain_community.document_loaders import PyMuPDFLoader
|
6 |
from langchain_text_splitters import CharacterTextSplitter
|
7 |
from langchain.chains import RetrievalQA
|
8 |
-
from langchain_community.llms import HuggingFaceEndpoint
|
9 |
from huggingface_hub import login
|
10 |
|
11 |
-
# 1. Authentication
|
|
|
|
|
12 |
login(token=os.environ.get('HF_TOKEN'))
|
13 |
|
14 |
-
# 2. PDF
|
15 |
def create_qa_system():
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
#
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
#
|
49 |
def chat_response(message, history):
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
gr.ChatInterface(chat_response).launch()
|
|
|
1 |
+
# app.py
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
from langchain_community.vectorstores import FAISS
|
|
|
6 |
from langchain_community.document_loaders import PyMuPDFLoader
|
7 |
from langchain_text_splitters import CharacterTextSplitter
|
8 |
from langchain.chains import RetrievalQA
|
9 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
10 |
from huggingface_hub import login
|
11 |
|
12 |
+
# 1. Authentication with validation
|
13 |
+
if not os.environ.get('HF_TOKEN'):
|
14 |
+
raise RuntimeError("Add HF_TOKEN in Space secrets!")
|
15 |
login(token=os.environ.get('HF_TOKEN'))
|
16 |
|
17 |
+
# 2. PDF processing with error handling
|
18 |
def create_qa_system():
|
19 |
+
try:
|
20 |
+
# File check
|
21 |
+
if not os.path.exists("data.pdf"):
|
22 |
+
raise FileNotFoundError("PDF missing")
|
23 |
+
|
24 |
+
# Load and validate PDF
|
25 |
+
loader = PyMuPDFLoader("data.pdf")
|
26 |
+
documents = loader.load()
|
27 |
+
if len(documents) == 0:
|
28 |
+
raise ValueError("PDF content empty")
|
29 |
+
|
30 |
+
# Processing
|
31 |
+
text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=50)
|
32 |
+
texts = text_splitter.split_documents(documents)
|
33 |
+
|
34 |
+
embeddings = HuggingFaceEmbeddings(
|
35 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
36 |
+
)
|
37 |
+
|
38 |
+
db = FAISS.from_documents(texts, embeddings)
|
39 |
+
|
40 |
+
# Smaller model for free tier
|
41 |
+
llm = HuggingFaceEndpoint(
|
42 |
+
repo_id="google/flan-t5-small",
|
43 |
+
max_length=128,
|
44 |
+
temperature=0.2,
|
45 |
+
huggingfacehub_api_token=os.environ.get('HF_TOKEN')
|
46 |
+
)
|
47 |
+
|
48 |
+
return RetrievalQA.from_chain_type(
|
49 |
+
llm=llm,
|
50 |
+
chain_type="stuff",
|
51 |
+
retriever=db.as_retriever(search_kwargs={"k": 1})
|
52 |
+
except Exception as e:
|
53 |
+
raise gr.Error(f"Setup failed: {str(e)}")
|
54 |
|
55 |
+
# 3. Initialize system
|
56 |
+
try:
|
57 |
+
qa = create_qa_system()
|
58 |
+
except Exception as e:
|
59 |
+
print(f"❌ Critical error: {str(e)}")
|
60 |
+
raise
|
61 |
|
62 |
+
# 4. Chat interface with error messages
|
63 |
def chat_response(message, history):
|
64 |
+
try:
|
65 |
+
response = qa({"query": message})
|
66 |
+
return response["result"]
|
67 |
+
except Exception as e:
|
68 |
+
print(f"🚨 User-facing error: {str(e)}") # Detailed log
|
69 |
+
return f"Error: {str(e)}" # User message
|
70 |
|
71 |
gr.ChatInterface(chat_response).launch()
|