Spaces:
Sleeping
Sleeping
Create app.py
Browse files8 unmodified lines9101112131415161718192046 unmodified lines6768696465706667686970717172736 unmodified lines80818284838485864 unmodified lines9192939594959796979899100991001011021031041051061031071081091101111121131101141151161131141171181191201171211221231201241251261278 unmodified lines# -------------------------------embed_model = SentenceTransformer('all-MiniLM-L6-v2')
# -------------------------------# Global variables# -------------------------------index = Nonechunks = None
# -------------------------------# Load PDF# -------------------------------46 unmodified lines return "β Upload and process PDF first."
result = retrieve(query, index, chunks)
return result[:500] # limit output return result[:500]
# -------------------------------# Global variables# -------------------------------index = Nonechunks = None
# -------------------------------# Process PDF6 unmodified lines
text = load_pdf(file)
print("Text length:", len(text)) print("Text length:", len(text)) # debug
if len(text) == 0: return "β PDF has no readable text (scanned)."4 unmodified lines return "β
PDF processed successfully!"
# -------------------------------# Chat function# Chat function (FIXED)# -------------------------------def respond(message, chat_history):def respond(message, history): if history is None: history = []
reply = answer_question(message) chat_history.append((message, reply)) return "", chat_history
history.append((message, reply)) # β
correct format
return "", history
# -------------------------------# UI# 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() status = gr.Textbox(label="Status")
chatbot = gr.Chatbot() msg = gr.Textbox() clear = gr.Button("Clear") msg = gr.Textbox(placeholder="Ask a question...") clear = gr.Button("Clear Chat")
btn.click(process_pdf, inputs=file_input, outputs=status) msg.submit(respond, [msg, chatbot], [msg, chatbot]) msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) clear.click(lambda: [], None, chatbot)
app.launch()# -------------------------------# Launch# -------------------------------app.launch(server_name="0.0.0.0", server_port=7860)app.py+26-19