Spaces:
Sleeping
Sleeping
Sachin B M commited on
Commit ·
b66efdf
1
Parent(s): 67c26fd
gpu setting
Browse files123452 unmodified lines891011121314151617189 unmodified lines282930313233348 unmodified lines434445464748494 unmodified lines5455565758596012 unmodified lines7374757677787955 unmodified lines1351361371381391401416 unmodified lines14814915015115215315428 unmodified lines18318418518618718818919 unmodified lines2092102112122132142152162172184 unmodified lines2232242252262272282294 unmodified lines23423523623723823924021 unmodified lines2622632642652662672681 unmodified line2702712722732742752762772782792802 unmodified lines2832842852862872882892 unmodified lines2922932942952962972985 unmodified lines3043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523531 unmodified line3553563573583593603613623633643653662 unmodified lines3693703713723733743753763773783793803813822 unmodified lines38538638738838939039139239339439539639739839940040140240340440540640740840941041141241317 unmodified lines43143243343443543643745 unmodified lines48348448548648748848949049149249349449549649749849950035 unmodified lines5365375385395405415423 unmodified lines5465475485495505515525535545555565575585592 unmodified lines56256356456556656756856957057118 unmodified lines590591592593594595596597import osimport gradio as grfrom dotenv import load_dotenvfrom PyPDF2 import PdfReader2 unmodified linesfrom langchain_community.vectorstores import FAISSfrom langchain_classic.chains import ConversationalRetrievalChainload_dotenv()# =========================================================# 1. EXTRACT TEXT FROM PDF FILES# =========================================================def get_pdf_text(pdf_files):9 unmodified lines# =========================================================# 2. SPLIT TEXT INTO CHUNKS# =========================================================def get_text_chunks(text):8 unmodified lines# =========================================================# 3. CREATE COHERE EMBEDDINGS# =========================================================def get_embeddings():4 unmodified lines# =========================================================# 4. CREATE CONVERSATIONAL RETRIEVAL CHAIN# =========================================================def get_conversational_chain(vectorstore):12 unmodified lines# =========================================================# 5. PROCESS UPLOADED DOCUMENTS# =========================================================def process_documents(pdf_files):55 unmodified lines# =========================================================# 6. CONVERT GRADIO CHAT HISTORY TO LANGCHAIN FORMAT# =========================================================def convert_chat_history(history):6 unmodified linesrole = message.get("role")content = message.get("content", "")# Convert list-based content into a stringif isinstance(content, list):content = " ".join(str(item.get("text", ""))28 unmodified lines# =========================================================# 7. HANDLE USER QUESTIONS# =========================================================def handle_user_question(question, history, chain):19 unmodified linesreturn history, ""try:# Convert previous messages into LangChain formatchat_history = convert_chat_history(history)# Ask the questionresponse = chain.invoke({"question": question,"chat_history": chat_history4 unmodified lines"No answer was generated.")# Ensure the answer is a stringif isinstance(answer, list):answer = " ".join(str(item.get("text", ""))4 unmodified lineselse:answer = str(answer)# Add the new conversation to Gradio historyhistory.append({"role": "user","content": question21 unmodified lines# =========================================================# 8. CLEAR CHAT# =========================================================def clear_chat():1 unmodified line# =========================================================# 9. CUSTOM CSS - RESPONSIVE DESIGN# =========================================================custom_css = """/* Overall application */.gradio-container {background: #0e0f14 !important;color: #f8fafc !important;2 unmodified linespadding: 16px !important;}/* Main row */#main-row {display: flex !important;align-items: flex-start !important;2 unmodified linesmargin: 0 auto !important;}/* Sidebar */#sidebar {background: #24252f !important;border: 1px solid #343642 !important;5 unmodified linesmargin-top: 40px;}/* Sidebar heading */#sidebar h2 {font-size: 1.15rem !important;margin: 0 0 12px 0 !important;color: #ffffff !important;}/* Compact PDF uploader */#sidebar [data-testid="file-upload"] {min-height: 110px !important;}/* Process button */#process-button {margin-top: 10px !important;border-radius: 8px !important;transition: transform 0.2s ease,box-shadow 0.2s ease;}/* Button hover effect */#process-button:hover {transform: translateY(-1px);box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);}/* Status box */#status {margin-top: 10px !important;}/* Main content */#main-panel {padding: 0 8px !important;min-width: 0 !important;}/* Hero header */#hero {background: linear-gradient(120deg,#0f172a 0%,#1d4ed8 100%);padding: 28px 32px;border-radius: 18px;box-shadow: 0 10px 30px rgba(29, 78, 216, 0.18);1 unmodified linetransition: box-shadow 0.2s ease;}/* Hero hover effect */#hero:hover {box-shadow: 0 12px 34px rgba(29, 78, 216, 0.28);}/* Hero title */#hero h1 {color: white !important;font-size: clamp(1.5rem, 2.5vw, 2.3rem) !important;2 unmodified linesmargin: 0 !important;}/* Hero description */#hero p {color: #dbeafe !important;font-size: 1rem !important;margin: 10px 0 0 0 !important;}/* Chat window */#chatbot {border-radius: 12px !important;min-height: 280px !important;2 unmodified linesoverflow: auto !important;}/* Question input */#question-box {margin-top: 12px !important;}/* Input field */#question-box textarea {min-height: 48px !important;border-radius: 10px !important;}/* Clear button */#main-panel button {border-radius: 8px !important;transition: background 0.2s ease;}/* Hide Gradio footer */footer {display: none !important;}/* Tablet screens */@media (max-width: 900px) {.gradio-container {padding: 12px !important;17 unmodified lines}}/* Mobile screens */@media (max-width: 700px) {.gradio-container {padding: 8px !important;45 unmodified lines# =========================================================# 10. BUILD GRADIO INTERFACE# =========================================================with gr.Blocks(title="Chat with Documents",theme=gr.themes.Base(),css=custom_css) as demo:# Store the conversational chainchain_state = gr.State(None)with gr.Row(elem_id="main-row",equal_height=False35 unmodified lineselem_id="main-panel"):# Headergr.HTML("""<div id="hero"><h1>AI Chatbot for Your Documents</h1>3 unmodified lines</div>""")# Chatbotchatbot = gr.Chatbot(label="Chat with your PDFs",height=500,elem_id="chatbot")# Question inputquestion = gr.Textbox(label="Ask a question",placeholder="Ask a question about your PDFs",2 unmodified lineselem_id="question-box")# Clear chat buttonclear_button = gr.Button("Clear Chat")# Process PDF documentsprocess_button.click(18 unmodified lines# =========================================================# 11. LAUNCH APPLICATION# =========================================================if __name__ == "__main__":demo.queue().launch()12345672 unmodified lines1011121314151617181920212223242526272829309 unmodified lines404142434445468 unmodified lines555657585960614 unmodified lines6667686970717212 unmodified lines8586878889909155 unmodified lines1471481491501511521536 unmodified lines16016116216316416528 unmodified lines19419519619719819920019 unmodified lines2202212222232242252262274 unmodified lines2322332342352362374 unmodified lines24224324424524624721 unmodified lines2692702712722732742751 unmodified line2772782792802812822832842852862 unmodified lines2892902912922932942 unmodified lines2972982993003013025 unmodified lines3083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443451 unmodified line3473483493503513523533543553562 unmodified lines3593603613623633643653663673683693702 unmodified lines37337437537637737837938038138238338438538638738838939039139239339439539617 unmodified lines41441541641741841945 unmodified lines46546646746846947047147247347447547647747847948048148248348448548635 unmodified lines5225235245255265273 unmodified lines5315325335345355365375385395405415425432 unmodified lines54654754854955055155218 unmodified lines571572573574575576577578579580581582583584import osimport gradio as grimport spacesfrom dotenv import load_dotenvfrom PyPDF2 import PdfReader2 unmodified linesfrom langchain_community.vectorstores import FAISSfrom langchain_classic.chains import ConversationalRetrievalChainload_dotenv()# =========================================================# 1. ZERO-GPU STARTUP CHECK# =========================================================@spaces.GPU(duration=1)def zerogpu_check():return None# =========================================================# 2. EXTRACT TEXT FROM PDF FILES# =========================================================def get_pdf_text(pdf_files):9 unmodified lines# =========================================================# 3. SPLIT TEXT INTO CHUNKS# =========================================================def get_text_chunks(text):8 unmodified lines# =========================================================# 4. CREATE COHERE EMBEDDINGS# =========================================================def get_embeddings():4 unmodified lines# =========================================================# 5. CREATE CONVERSATIONAL RETRIEVAL CHAIN# =========================================================def get_conversational_chain(vectorstore):12 unmodified lines# =========================================================# 6. PROCESS UPLOADED DOCUMENTS# =========================================================def process_documents(pdf_files):55 unmodified lines# =========================================================# 7. CONVERT GRADIO CHAT HISTORY TO LANGCHAIN FORMAT# =========================================================def convert_chat_history(history):6 unmodified linesrole = message.get("role")content = message.get("content", "")if isinstance(content, list):content = " ".join(str(item.get("text", ""))28 unmodified lines# =========================================================# 8. HANDLE USER QUESTIONS# =========================================================def handle_user_question(question, history, chain):19 unmodified linesreturn history, ""try:chat_history = convert_chat_history(history)response = chain.invoke({"question": question,"chat_history": chat_history4 unmodified lines"No answer was generated.")if isinstance(answer, list):answer = " ".join(str(item.get("text", ""))4 unmodified lineselse:answer = str(answer)history.append({"role": "user","content": question21 unmodified lines# =========================================================# 9. CLEAR CHAT# =========================================================def clear_chat():1 unmodified line# =========================================================# 10. CUSTOM CSS# =========================================================custom_css = """.gradio-container {background: #0e0f14 !important;color: #f8fafc !important;2 unmodified linespadding: 16px !important;}#main-row {display: flex !important;align-items: flex-start !important;2 unmodified linesmargin: 0 auto !important;}#sidebar {background: #24252f !important;border: 1px solid #343642 !important;5 unmodified linesmargin-top: 40px;}#sidebar h2 {font-size: 1.15rem !important;margin: 0 0 12px 0 !important;color: #ffffff !important;}#sidebar [data-testid="file-upload"] {min-height: 110px !important;}#process-button {margin-top: 10px !important;border-radius: 8px !important;transition: transform 0.2s ease, box-shadow 0.2s ease;}#process-button:hover {transform: translateY(-1px);box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);}#status {margin-top: 10px !important;}#main-panel {padding: 0 8px !important;min-width: 0 !important;}#hero {background: linear-gradient(120deg, #0f172a 0%, #1d4ed8 100%);padding: 28px 32px;border-radius: 18px;box-shadow: 0 10px 30px rgba(29, 78, 216, 0.18);1 unmodified linetransition: box-shadow 0.2s ease;}#hero:hover {box-shadow: 0 12px 34px rgba(29, 78, 216, 0.28);}#hero h1 {color: white !important;font-size: clamp(1.5rem, 2.5vw, 2.3rem) !important;2 unmodified linesmargin: 0 !important;}#hero p {color: #dbeafe !important;font-size: 1rem !important;margin: 10px 0 0 0 !important;}#chatbot {border-radius: 12px !important;min-height: 280px !important;2 unmodified linesoverflow: auto !important;}#question-box {margin-top: 12px !important;}#question-box textarea {min-height: 48px !important;border-radius: 10px !important;}#main-panel button {border-radius: 8px !important;transition: background 0.2s ease;}footer {display: none !important;}@media (max-width: 900px) {.gradio-container {padding: 12px !important;17 unmodified lines}}@media (max-width: 700px) {.gradio-container {padding: 8px !important;45 unmodified lines# =========================================================# 11. BUILD GRADIO INTERFACE# =========================================================with gr.Blocks(title="Chat with Documents") as demo:# State to store the conversational chainchain_state = gr.State(None)# Hidden event to satisfy ZeroGPU startup detectionhidden_button = gr.Button(visible=False)hidden_button.click(fn=zerogpu_check,inputs=[],outputs=[])with gr.Row(elem_id="main-row",equal_height=False35 unmodified lineselem_id="main-panel"):gr.HTML("""<div id="hero"><h1>AI Chatbot for Your Documents</h1>3 unmodified lines</div>""")chatbot = gr.Chatbot(label="Chat with your PDFs",type="messages",height=500,elem_id="chatbot")question = gr.Textbox(label="Ask a question",placeholder="Ask a question about your PDFs",2 unmodified lineselem_id="question-box")clear_button = gr.Button("Clear Chat")# Process PDF documentsprocess_button.click(18 unmodified lines# =========================================================# 12. LAUNCH APPLICATION# =========================================================if __name__ == "__main__":demo.queue().launch(theme=gr.themes.Base(),css=custom_css,server_name="0.0.0.0",server_port=7860,ssr_mode=False)
app.py+44-57