random2222 commited on
Commit
68420b4
·
verified ·
1 Parent(s): 5a6f9e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -36
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 # Updated import
9
  from huggingface_hub import login
10
 
11
- # 1. Authentication
 
 
12
  login(token=os.environ.get('HF_TOKEN'))
13
 
14
- # 2. PDF Processing
15
  def create_qa_system():
16
- if not os.path.exists("file.pdf"):
17
- raise gr.Error("❗ Upload file.pdf in Files tab")
18
-
19
- loader = PyMuPDFLoader("file.pdf")
20
- documents = loader.load()
21
-
22
- text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
23
- texts = text_splitter.split_documents(documents)
24
-
25
- embeddings = HuggingFaceEmbeddings(
26
- model_name="sentence-transformers/all-MiniLM-L6-v2"
27
- )
28
-
29
- db = FAISS.from_documents(texts, embeddings)
30
-
31
- # 3. Updated LLM initialization
32
- llm = HuggingFaceEndpoint(
33
- repo_id="google/flan-t5-base",
34
- max_length=256,
35
- temperature=0.2,
36
- huggingfacehub_api_token=os.environ.get('HF_TOKEN') # Explicit token passing
37
- )
38
-
39
- return RetrievalQA.from_chain_type(
40
- llm=llm,
41
- chain_type="stuff",
42
- retriever=db.as_retriever(search_kwargs={"k": 2})
43
- )
 
 
 
 
 
 
 
44
 
45
- # 4. Initialize system
46
- qa = create_qa_system()
 
 
 
 
47
 
48
- # 5. Chat interface
49
  def chat_response(message, history):
50
- response = qa({"query": message})
51
- return response["result"]
 
 
 
 
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()