Spaces:
Sleeping
Sleeping
Create app.py
Browse files8 unmodified lines9101112131446 unmodified lines61626364656667686970717273746 unmodified lines818283848586874 unmodified lines92939495969798991001011021031041051061071081091101111121131141151161171181191208 unmodified lines# -------------------------------embed_model = SentenceTransformer('all-MiniLM-L6-v2')# -------------------------------# Load PDF# -------------------------------46 unmodified linesreturn "β Upload and process PDF first."result = retrieve(query, index, chunks)return result[:500] # limit output# -------------------------------# Global variables# -------------------------------index = Nonechunks = None# -------------------------------# Process PDF6 unmodified linestext = load_pdf(file)print("Text length:", len(text))if len(text) == 0:return "β PDF has no readable text (scanned)."4 unmodified linesreturn "β PDF processed successfully!"# -------------------------------# Chat function# -------------------------------def respond(message, chat_history):reply = answer_question(message)chat_history.append((message, reply))return "", chat_history# -------------------------------# UI# -------------------------------with gr.Blocks() as app:gr.Markdown("# π Course Material Chatbot")file_input = gr.File(label="Upload PDF")btn = gr.Button("Process PDF")status = gr.Textbox()chatbot = gr.Chatbot()msg = gr.Textbox()clear = gr.Button("Clear")btn.click(process_pdf, inputs=file_input, outputs=status)msg.submit(respond, [msg, chatbot], [msg, chatbot])clear.click(lambda: [], None, chatbot)app.launch()8 unmodified lines9101112131415161718192046 unmodified lines676869707172736 unmodified lines808182838485864 unmodified lines9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261278 unmodified lines# -------------------------------embed_model = SentenceTransformer('all-MiniLM-L6-v2')# -------------------------------# Global variables# -------------------------------index = Nonechunks = None# -------------------------------# Load PDF# -------------------------------46 unmodified linesreturn "β Upload and process PDF first."result = retrieve(query, index, chunks)return result[:500]# -------------------------------# Process PDF6 unmodified linestext = load_pdf(file)print("Text length:", len(text)) # debugif len(text) == 0:return "β PDF has no readable text (scanned)."4 unmodified linesreturn "β PDF processed successfully!"# -------------------------------# Chat function (FIXED)# -------------------------------def respond(message, history):if history is None:history = []reply = answer_question(message)history.append((message, reply)) # β correct formatreturn "", history# -------------------------------# UI (NO ERRORS)# -------------------------------with gr.Blocks() as app:gr.Markdown("# π Course Material Chatbot")file_input = gr.File(label="Upload PDF")btn = gr.Button("Process PDF")status = gr.Textbox(label="Status")chatbot = gr.Chatbot()msg = gr.Textbox(placeholder="Ask a question...")clear = gr.Button("Clear Chat")btn.click(process_pdf, inputs=file_input, outputs=status)msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])clear.click(lambda: [], None, chatbot)# -------------------------------# Launch# -------------------------------app.launch(server_name="0.0.0.0", server_port=7860)
app.py+26-19