Spaces:
Runtime error
Runtime error
Upload app.py
Browse files37 unmodified lines3839404142434445464753 unmodified lines10110210310410510610710810911038 unmodified lines1491501511521531541551561571581591601611621631641651661671681698 unmodified lines17817918018118218318430 unmodified lines21521621721821922022135 unmodified lines25725825926026126226337 unmodified lines"text-generation",model=model,tokenizer=tokenizer,max_new_tokens=256,temperature=0.7,top_p=0.95,repetition_penalty=1.15)self.llm = HuggingFacePipeline(pipeline=pipe)53 unmodified linesprint("No text could be extracted from PDF files!")return False# Split text into chunks with metadatatext_splitter = RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200,length_function=len,)38 unmodified linesif not self.vector_db or not self.llm:return False# Custom prompt template for Q&Aqa_template = """You are an expert programming instructor for the Inclusive World Curriculum.Use the following context to answer the student's question. If the information is not in the context,provide a helpful response based on your knowledge of programming concepts.Context: {context}Question: {question}Answer:"""self.qa_chain = RetrievalQA.from_chain_type(llm=self.llm,chain_type="stuff",retriever=self.vector_db.as_retriever(search_kwargs={"k": 5}),chain_type_kwargs={"prompt": PromptTemplate(template=qa_template,8 unmodified lines"""Find relevant pages for a given question"""try:# Search for relevant chunksresults = self.vector_db.similarity_search(question, k=5)relevant_pages = []seen_pages = set()30 unmodified lines# Sort by relevance and return top resultsrelevant_pages.sort(key=lambda x: x['relevance_score'], reverse=True)return relevant_pages[:3] # Return top 3 most relevant pagesexcept Exception as e:print(f"Error finding relevant pages: {str(e)}")35 unmodified linespage_info = "📄 **Relevant Pages Found:**\n\n"for i, page in enumerate(relevant_pages, 1):page_info += f"**{i}. {page['filename']} - Page {page['page_number']}**\n"page_info += f"```\n{page['content'][:300]}...\n```\n\n"else:page_info = "No specific pages found for this question."37 unmodified lines38394041424344454647484953 unmodified lines10310410510610710810911011111238 unmodified lines1511521531541551561571581591601611621631641651661678 unmodified lines17617717817918018118230 unmodified lines21321421521621721821935 unmodified lines25525625725825926026137 unmodified lines"text-generation",model=model,tokenizer=tokenizer,max_new_tokens=128, # Reduced from 256temperature=0.7,top_p=0.95,repetition_penalty=1.15,do_sample=True,pad_token_id=tokenizer.eos_token_id)self.llm = HuggingFacePipeline(pipeline=pipe)53 unmodified linesprint("No text could be extracted from PDF files!")return False# Split text into smaller chunks with metadatatext_splitter = RecursiveCharacterTextSplitter(chunk_size=500, # Reduced from 1000chunk_overlap=50, # Reduced from 200length_function=len,)38 unmodified linesif not self.vector_db or not self.llm:return False# Shorter prompt template for DialoGPTqa_template = """Context: {context}Question: {question}Answer:"""self.qa_chain = RetrievalQA.from_chain_type(llm=self.llm,chain_type="stuff",retriever=self.vector_db.as_retriever(search_kwargs={"k": 2}), # Reduced from 5chain_type_kwargs={"prompt": PromptTemplate(template=qa_template,8 unmodified lines"""Find relevant pages for a given question"""try:# Search for relevant chunksresults = self.vector_db.similarity_search(question, k=3) # Reduced from 5relevant_pages = []seen_pages = set()30 unmodified lines# Sort by relevance and return top resultsrelevant_pages.sort(key=lambda x: x['relevance_score'], reverse=True)return relevant_pages[:2] # Reduced from 3except Exception as e:print(f"Error finding relevant pages: {str(e)}")35 unmodified linespage_info = "📄 **Relevant Pages Found:**\n\n"for i, page in enumerate(relevant_pages, 1):page_info += f"**{i}. {page['filename']} - Page {page['page_number']}**\n"page_info += f"```\n{page['content'][:200]}...\n```\n\n" # Reduced from 300else:page_info = "No specific pages found for this question."
app.py+15-17