from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from transformers import pipeline # Set the title of your FastAPI application app = FastAPI(title="Saphir AI Chatbot") app.mount('/static', StaticFiles(directory='app/static'), name='static') # Initialize your model question_answerer = pipeline(task='question-answering', model='distilbert-base-cased-distilled-squad') @app.get('/', response_class=HTMLResponse) def read_root(): """ Root Endpoint Returns the main HTML page for the chatbot interface. """ with open('app/index.html', 'r') as html_file: return html_file.read() @app.post('/chatbot') async def ask_question(question: str, context: str): """ Chatbot Endpoint Takes a question and context, returns the answer from the chatbot. Parameters: - question: A string containing the question to be answered. - context: A string containing the context in which the question should be answered. Returns: A JSON object with the 'answer' field containing the response from the chatbot. Example: 1. Go to the Swagger UI. 2. Scroll down to the /chatbot POST method section. 3. Click on the Try it out button. 4. In the question field, enter: "What is the capital of France?" 5. In the context field, enter: "France is a country in Western Europe. It has several beautiful cities, including Paris, which is its capital. France is known for its wine, sophisticated cuisine, and landmarks like the Eiffel Tower." 6. Click the Execute button to submit the request. 7. Scroll down to see the server response, where you will find the answer. """ result = question_answerer(question=question, context=context) return {'answer': result['answer']}