File size: 1,916 Bytes
6b3fef0
87f49ae
d2ddedd
ad38076
8904ae0
6b3fef0
 
ad38076
6b3fef0
 
ad38076
 
8904ae0
ad38076
8904ae0
6b3fef0
 
 
 
ad38076
526f25a
8904ae0
ad38076
 
6b3fef0
 
 
 
 
 
 
 
 
 
 
 
e3a71c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b3fef0
ad38076
6b3fef0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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']}