File size: 1,444 Bytes
1883341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from course_content import (
    course_introduction,
    how_language_model_works,
    practical_application_and_usecases_of_llms,
    what_is_generative_ai,
    llms_for_writing,
    llms_for_reading,
    llms_for_chatting,
    llms_capabilities_limitations,
    llms_image_generation
)
from simpletransformers.question_answering import QuestionAnsweringModel
# Initialize a Question Answering model
model = QuestionAnsweringModel(
    "bert",               # Specify the model type, e.g., 'bert', 'roberta', etc.
    "bert-base-uncased",  # Specify the model size and configuration
    use_cuda=False        # Set to True if you have CUDA enabled GPU
)


def get_answer(question):
    input_data = [
        {
            "id": 0,
            "context": llms_image_generation,
            "qas": [{"question": question, "id": 0}]
        }
    ]
    # Perform prediction
    results = model.predict(input_data)

    # Assuming results is the output of model.predict(input_data)
    answer_dict = results[0][0]  # Get the dictionary containing answers and IDs
    answer_segments = answer_dict['answer']  # Get the list of answer segments
    probabilities = results[1][0]['probability']  # Get the list of probabilities

    # Find the index of the answer segment with the highest probability
    best_index = probabilities.index(max(probabilities))
    best_answer = answer_segments[best_index]

    return best_answer