Spaces:
Running
Running
| # main.py | |
| # This is the main file that runs the Sanic web server. | |
| from sanic import Sanic, response | |
| from retriever import get_ensemble_retriever | |
| from llm_chain import create_rag_chain | |
| app = Sanic("VibbaBackend") | |
| async def setup_model(app_instance, loop): | |
| """ | |
| Initializes the retriever and RAG chain and attaches them | |
| to the application context before the server starts. | |
| """ | |
| print("Server starting up... Initializing model pipeline.") | |
| retriever = get_ensemble_retriever() | |
| rag_chain = create_rag_chain(retriever) | |
| app_instance.ctx.rag_chain = rag_chain | |
| print("Model pipeline is ready.") | |
| async def home(request): | |
| """ | |
| Root endpoint showing app name and description. | |
| """ | |
| html_content = """ | |
| <html> | |
| <head> | |
| <title>VibbaBackend</title> | |
| <style> | |
| body { | |
| font-family: Arial, sans-serif; | |
| margin: 40px; | |
| background-color: #f9f9f9; | |
| color: #333; | |
| } | |
| h1 { color: #0073e6; } | |
| .container { | |
| max-width: 800px; | |
| margin: auto; | |
| padding: 20px; | |
| background: #fff; | |
| border-radius: 8px; | |
| box-shadow: 0 2px 6px rgba(0,0,0,0.1); | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1>VibbaBackend</h1> | |
| <p> | |
| Welcome to the <strong>VibbaBackend</strong> service! π<br><br> | |
| This backend powers a Retrieval-Augmented Generation (RAG) pipeline | |
| using an ensemble retriever and a large language model. | |
| </p> | |
| <p> | |
| <strong>Available endpoints:</strong> | |
| <ul> | |
| <li><code>/getResponse?question=Your+query</code> β Get an answer to your question.</li> | |
| </ul> | |
| </p> | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| return response.html(html_content) | |
| async def get_response_endpoint(request): | |
| """ | |
| Endpoint to get an answer to a question using the RAG chain. | |
| Expects a 'question' query parameter. | |
| """ | |
| question = request.args.get("question") | |
| if not question: | |
| return response.json( | |
| {"error": "Please provide a 'question' query parameter."}, | |
| status=400 | |
| ) | |
| try: | |
| chain = request.app.ctx.rag_chain | |
| result = chain.invoke(question) | |
| return response.text(result) | |
| except Exception as e: | |
| print(f"An error occurred during invocation: {e}") | |
| return response.json( | |
| {"error": "An internal error occurred while processing your request."}, | |
| status=500 | |
| ) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |