trry / app.py
random2222's picture
Update app.py
68420b4 verified
raw
history blame
2.31 kB
# app.py
import os
import gradio as gr
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain.chains import RetrievalQA
from langchain_community.llms import HuggingFaceEndpoint
from huggingface_hub import login
# 1. Authentication with validation
if not os.environ.get('HF_TOKEN'):
raise RuntimeError("Add HF_TOKEN in Space secrets!")
login(token=os.environ.get('HF_TOKEN'))
# 2. PDF processing with error handling
def create_qa_system():
try:
# File check
if not os.path.exists("data.pdf"):
raise FileNotFoundError("PDF missing")
# Load and validate PDF
loader = PyMuPDFLoader("data.pdf")
documents = loader.load()
if len(documents) == 0:
raise ValueError("PDF content empty")
# Processing
text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=50)
texts = text_splitter.split_documents(documents)
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
db = FAISS.from_documents(texts, embeddings)
# Smaller model for free tier
llm = HuggingFaceEndpoint(
repo_id="google/flan-t5-small",
max_length=128,
temperature=0.2,
huggingfacehub_api_token=os.environ.get('HF_TOKEN')
)
return RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=db.as_retriever(search_kwargs={"k": 1})
except Exception as e:
raise gr.Error(f"Setup failed: {str(e)}")
# 3. Initialize system
try:
qa = create_qa_system()
except Exception as e:
print(f"❌ Critical error: {str(e)}")
raise
# 4. Chat interface with error messages
def chat_response(message, history):
try:
response = qa({"query": message})
return response["result"]
except Exception as e:
print(f"🚨 User-facing error: {str(e)}") # Detailed log
return f"Error: {str(e)}" # User message
gr.ChatInterface(chat_response).launch()