Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,92 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
import torch
|
4 |
-
from huggingface_hub import login
|
5 |
from langchain_community.document_loaders import PyMuPDFLoader, TextLoader
|
6 |
-
from langchain_text_splitters import
|
7 |
from langchain_community.vectorstores import FAISS
|
8 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
9 |
from langchain.chains import RetrievalQA
|
10 |
from langchain_community.llms import HuggingFacePipeline
|
11 |
-
from transformers import pipeline, AutoTokenizer
|
12 |
|
13 |
-
|
14 |
-
login(token=os.environ.get('HF_TOKEN'))
|
15 |
-
|
16 |
-
# Configuration
|
17 |
-
DOCS_DIR = "study_materials"
|
18 |
-
MODEL_NAME = "microsoft/phi-2"
|
19 |
-
EMBEDDINGS_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
20 |
-
MAX_TOKENS = 300
|
21 |
-
CHUNK_SIZE = 1000
|
22 |
-
|
23 |
-
def load_documents():
|
24 |
documents = []
|
25 |
-
for filename in os.listdir(
|
26 |
-
path = os.path.join(
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
print(f"Error loading {filename}: {str(e)}")
|
34 |
return documents
|
35 |
|
36 |
def create_qa_system():
|
37 |
-
# Load and split documents
|
38 |
-
documents = load_documents()
|
39 |
-
if not documents:
|
40 |
-
raise gr.Error("No documents found in 'study_materials' folder")
|
41 |
-
|
42 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
43 |
-
chunk_size=CHUNK_SIZE,
|
44 |
-
chunk_overlap=200,
|
45 |
-
separators=["\n\n", "\n", " "]
|
46 |
-
)
|
47 |
-
texts = text_splitter.split_documents(documents)
|
48 |
-
|
49 |
-
# Create vector store
|
50 |
-
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDINGS_MODEL)
|
51 |
-
db = FAISS.from_documents(texts, embeddings)
|
52 |
-
|
53 |
-
# Load Phi-2 with authentication
|
54 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
55 |
-
model = AutoModelForCausalLM.from_pretrained(
|
56 |
-
MODEL_NAME,
|
57 |
-
use_auth_token=True, # Critical change for gated models
|
58 |
-
torch_dtype=torch.float32,
|
59 |
-
trust_remote_code=True,
|
60 |
-
device_map="auto",
|
61 |
-
low_cpu_mem_usage=True
|
62 |
-
)
|
63 |
-
|
64 |
-
pipe = pipeline(
|
65 |
-
"text-generation",
|
66 |
-
model=model,
|
67 |
-
tokenizer=tokenizer,
|
68 |
-
max_new_tokens=MAX_TOKENS,
|
69 |
-
temperature=0.7,
|
70 |
-
do_sample=True,
|
71 |
-
top_k=40,
|
72 |
-
device_map="auto"
|
73 |
-
)
|
74 |
-
|
75 |
-
return RetrievalQA.from_chain_type(
|
76 |
-
llm=HuggingFacePipeline(pipeline=pipe),
|
77 |
-
chain_type="stuff",
|
78 |
-
retriever=db.as_retriever(search_kwargs={"k": 2}),
|
79 |
-
return_source_documents=True
|
80 |
-
)
|
81 |
-
|
82 |
-
def format_response(response):
|
83 |
-
answer = response["result"].split("</s>")[0].split("\nOutput:")[-1].strip()
|
84 |
-
sources = list({os.path.basename(doc.metadata["source"]) for doc in response["source_documents"]})
|
85 |
-
return f"{answer}\n\n📚 Sources: {', '.join(sources)}"
|
86 |
-
|
87 |
-
def process_query(question, history):
|
88 |
try:
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
except Exception as e:
|
94 |
-
|
95 |
-
return f"⚠️ Error: {str(e)[:100]}"
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
102 |
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
|
|
3 |
from langchain_community.document_loaders import PyMuPDFLoader, TextLoader
|
4 |
+
from langchain_text_splitters import CharacterTextSplitter
|
5 |
from langchain_community.vectorstores import FAISS
|
6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
from langchain.chains import RetrievalQA
|
8 |
from langchain_community.llms import HuggingFacePipeline
|
9 |
+
from transformers import pipeline, AutoTokenizer
|
10 |
|
11 |
+
def load_documents(file_path="study_materials"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
documents = []
|
13 |
+
for filename in os.listdir(file_path):
|
14 |
+
path = os.path.join(file_path, filename)
|
15 |
+
if filename.endswith(".pdf"):
|
16 |
+
loader = PyMuPDFLoader(path)
|
17 |
+
documents.extend(loader.load())
|
18 |
+
elif filename.endswith(".txt"):
|
19 |
+
loader = TextLoader(path)
|
20 |
+
documents.extend(loader.load())
|
|
|
21 |
return documents
|
22 |
|
23 |
def create_qa_system():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
try:
|
25 |
+
# Load documents
|
26 |
+
documents = load_documents()
|
27 |
+
if not documents:
|
28 |
+
raise ValueError("📚 No study materials found")
|
29 |
+
|
30 |
+
# Text splitting
|
31 |
+
text_splitter = CharacterTextSplitter(
|
32 |
+
chunk_size=1100,
|
33 |
+
chunk_overlap=200,
|
34 |
+
separator="\n\n"
|
35 |
+
)
|
36 |
+
texts = text_splitter.split_documents(documents)
|
37 |
+
|
38 |
+
# Embeddings
|
39 |
+
embeddings = HuggingFaceEmbeddings(
|
40 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
41 |
+
)
|
42 |
+
|
43 |
+
# Vector store
|
44 |
+
db = FAISS.from_documents(texts, embeddings)
|
45 |
+
|
46 |
+
# LLM setup with proper LangChain wrapper
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large") # ←
|
48 |
+
pipe = pipeline(
|
49 |
+
"text2text-generation",
|
50 |
+
model="google/flan-t5-large",
|
51 |
+
max_length=600,
|
52 |
+
temperature=0.7,
|
53 |
+
tokenizer=tokenizer,
|
54 |
+
do_sample=True,
|
55 |
+
top_k=50,
|
56 |
+
device=-1
|
57 |
+
)
|
58 |
+
|
59 |
+
# Wrap pipeline in LangChain component
|
60 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
61 |
+
|
62 |
+
# Create QA chain
|
63 |
+
return RetrievalQA.from_llm(
|
64 |
+
llm=llm,
|
65 |
+
retriever=db.as_retriever(search_kwargs={"k": 3}),
|
66 |
+
return_source_documents=True
|
67 |
+
)
|
68 |
except Exception as e:
|
69 |
+
raise gr.Error(f"Error: {str(e)}")
|
|
|
70 |
|
71 |
+
# Initialize system
|
72 |
+
try:
|
73 |
+
qa = create_qa_system()
|
74 |
+
except Exception as e:
|
75 |
+
print(f"Startup failed: {str(e)}")
|
76 |
+
raise
|
77 |
|
78 |
+
def ask_question(question, history):
|
79 |
+
try:
|
80 |
+
result = qa.invoke({"query": question})
|
81 |
+
answer = result["result"]
|
82 |
+
sources = list({doc.metadata['source'] for doc in result['source_documents']})
|
83 |
+
return f"{answer}\n\n📚 Sources: {', '.join(sources)}"
|
84 |
+
except Exception as e:
|
85 |
+
return f"Error: {str(e)[:150]}"
|
86 |
|
87 |
+
gr.ChatInterface(
|
88 |
+
ask_question,
|
89 |
+
title="Study Assistant",
|
90 |
+
description="Upload PDF/TXT files in 'study_materials' folder and ask questions!",
|
91 |
+
theme="soft"
|
92 |
+
).launch()
|