import gradio as gr import os from pipeline.rag_pipeline import RAGPipeline from dotenv import load_dotenv load_dotenv() # Initialize pipeline print("Initializing RAG Pipeline...") pipeline = RAGPipeline() def chat_fn(message, history): try: # Pipeline expects message, history is handled by ChatInterface result = pipeline.answer(message) answer = result["answer"] # We can also append sources to the answer if we want sources = [c["doc_id"] for c in result["chunks"]] if sources: answer += f"\n\n---\n**Sources:** {', '.join(set(sources))}" return answer except Exception as e: return f"Error: {str(e)}" # Customizing ChatInterface for a better look demo = gr.ChatInterface( fn=chat_fn, title="🐍 Python RAG Assistant", description="Ask questions about Python documentation. This assistant uses Hybrid Search and Reranking for high-accuracy retrieval.", examples=[ "What are mutable default arguments in Python?", "How does Python's garbage collection work?", "Explain the difference between multiprocessing and multithreading.", "How do closures work in Python?" ], cache_examples=False, ) if __name__ == "__main__": # server_name="0.0.0.0" allows access within a local network # share=False avoids Windows Defender false positive blocks demo.launch( server_name="0.0.0.0", server_port=7860, share=True, theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="slate") )