Spaces:
Sleeping
Sleeping
Sachin B M commited on
Commit ·
b66efdf
1
Parent(s): 67c26fd
gpu setting
Browse files12345672 unmodified lines101112131415161718192021222324252615272829309 unmodified lines40414231434445468 unmodified lines55565746585960614 unmodified lines666768576970717212 unmodified lines858687768889909155 unmodified lines1471481491381501511521536 unmodified lines16016116215116316416528 unmodified lines19419519618619719819920019 unmodified lines2202212222122232242152252262274 unmodified lines2322332342262352362374 unmodified lines24224324423724524624721 unmodified lines2692702712652722732742751 unmodified line2772782792732802812822832772842852862 unmodified lines2892902912862922932942 unmodified lines2972982992953003013025 unmodified lines3083093103073113123133143153163143173183193203193213223233233243243253263273273283293303313333323333343353383363373383393403443413463473483493503423433443451 unmodified line3473483493583503513523533633543553562 unmodified lines3593603613723623633643653663673793683693702 unmodified lines37337437538837637737837939338038138238338439938538638738838940539039139239341039439539617 unmodified lines41441541643441741841945 unmodified lines46546646748646846947048949049149249347147249547347447547647747847948048148248348448548635 unmodified lines5225235245395255265273 unmodified lines5315325335495345355365375385395405565415425432 unmodified lines54654754856556654956756855055155218 unmodified lines571572573593574575576577597578579580581582583584import os
import gradio as grimport spacesfrom dotenv import load_dotenvfrom PyPDF2 import PdfReader
2 unmodified linesfrom langchain_community.vectorstores import FAISSfrom langchain_classic.chains import ConversationalRetrievalChain
load_dotenv()
# =========================================================# 1. ZERO-GPU STARTUP CHECK# =========================================================
@spaces.GPU(duration=1)def zerogpu_check(): return None
# =========================================================# 1. EXTRACT TEXT FROM PDF FILES# 2. EXTRACT TEXT FROM PDF FILES# =========================================================
def get_pdf_text(pdf_files):9 unmodified lines
# =========================================================# 2. SPLIT TEXT INTO CHUNKS# 3. SPLIT TEXT INTO CHUNKS# =========================================================
def get_text_chunks(text):8 unmodified lines
# =========================================================# 3. CREATE COHERE EMBEDDINGS# 4. CREATE COHERE EMBEDDINGS# =========================================================
def get_embeddings():4 unmodified lines
# =========================================================# 4. CREATE CONVERSATIONAL RETRIEVAL CHAIN# 5. CREATE CONVERSATIONAL RETRIEVAL CHAIN# =========================================================
def get_conversational_chain(vectorstore):12 unmodified lines
# =========================================================# 5. PROCESS UPLOADED DOCUMENTS# 6. PROCESS UPLOADED DOCUMENTS# =========================================================
def process_documents(pdf_files):55 unmodified lines
# =========================================================# 6. CONVERT GRADIO CHAT HISTORY TO LANGCHAIN FORMAT# 7. CONVERT GRADIO CHAT HISTORY TO LANGCHAIN FORMAT# =========================================================
def convert_chat_history(history):6 unmodified lines role = message.get("role") content = message.get("content", "")
# Convert list-based content into a string if isinstance(content, list): content = " ".join( str(item.get("text", ""))28 unmodified lines
# =========================================================# 7. HANDLE USER QUESTIONS# 8. HANDLE USER QUESTIONS# =========================================================
def handle_user_question(question, history, chain):19 unmodified lines return history, ""
try: # Convert previous messages into LangChain format chat_history = convert_chat_history(history)
# Ask the question response = chain.invoke({ "question": question, "chat_history": chat_history4 unmodified lines "No answer was generated." )
# Ensure the answer is a string if isinstance(answer, list): answer = " ".join( str(item.get("text", ""))4 unmodified lines else: answer = str(answer)
# Add the new conversation to Gradio history history.append({ "role": "user", "content": question21 unmodified lines
# =========================================================# 8. CLEAR CHAT# 9. CLEAR CHAT# =========================================================
def clear_chat():1 unmodified line
# =========================================================# 9. CUSTOM CSS - RESPONSIVE DESIGN# 10. CUSTOM CSS# =========================================================
custom_css = """/* Overall application */.gradio-container { background: #0e0f14 !important; color: #f8fafc !important;2 unmodified lines padding: 16px !important;}
/* Main row */#main-row { display: flex !important; align-items: flex-start !important;2 unmodified lines margin: 0 auto !important;}
/* Sidebar */#sidebar { background: #24252f !important; border: 1px solid #343642 !important;5 unmodified lines margin-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; 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% ); 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 line transition: 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 lines margin: 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 lines overflow: 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# 11. BUILD GRADIO INTERFACE# =========================================================
with gr.Blocks( title="Chat with Documents", theme=gr.themes.Base(), css=custom_css) as demo:with gr.Blocks(title="Chat with Documents") as demo:
# Store the conversational chain # State to store the conversational chain chain_state = gr.State(None)
# Hidden event to satisfy ZeroGPU startup detection hidden_button = gr.Button(visible=False) hidden_button.click( fn=zerogpu_check, inputs=[], outputs=[] )
with gr.Row( elem_id="main-row", equal_height=False35 unmodified lines elem_id="main-panel" ):
# Header gr.HTML(""" <div id="hero"> <h1>AI Chatbot for Your Documents</h1>3 unmodified lines </div> """)
# Chatbot chatbot = gr.Chatbot( label="Chat with your PDFs", type="messages", height=500, elem_id="chatbot" )
# Question input question = gr.Textbox( label="Ask a question", placeholder="Ask a question about your PDFs",2 unmodified lines elem_id="question-box" )
# Clear chat button clear_button = gr.Button( clear_button = gr.Button("Clear Chat") "Clear Chat" )
# Process PDF documents process_button.click(18 unmodified lines
# =========================================================# 11. LAUNCH APPLICATION# 12. LAUNCH APPLICATION# =========================================================
if __name__ == "__main__": demo.queue().launch() 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