File size: 4,607 Bytes
bacc64d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import streamlit as st
from transformers import AutoModelForQuestionAnswering, AutoTokenizer

def main():
    # Custom CSS for styling
    custom_css = """
        <style>
            body {
                font-family: 'Arial', sans-serif;
                background-color: #FF0000; /* Set your desired background color here */
            }
            .title {
                font-size: 2.5em;
                color: #333;
                text-align: center;
                padding: 1em;
                background-color: #3498db;
                color: #fff;
                border-radius: 10px;
                margin-bottom: 1em;
            }
            .input-container {
                margin: 2em;
            }
            .button-container {
                text-align: center;
            }
            .result-container {
                margin: 2em;
                padding: 1em;
                background-color: #fff;
                border-radius: 10px;
                box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            }
            .answer {
                background-color: #3498db;
                padding: 10px;
                border-radius: 5px;
                margin-top: 10px;
            }
            img {
                max-width: 100%;
                height: auto;
                border-radius: 10px;
                box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            }
        </style>
    """
    st.markdown(custom_css, unsafe_allow_html=True)

    # Title
    st.markdown("<div class='title'>Question Answering with Transformers</div>", unsafe_allow_html=True)

    # Model Selection Dropdown
    model_name = st.selectbox("Select Model", ["deepset/roberta-base-squad2", "bert-large-uncased-whole-word-masking-finetuned-squad", "distilbert-base-cased-distilled-squad",
    "bert-base-uncased",
    "albert-base-v2"])
    model = AutoModelForQuestionAnswering.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)

    # Get user input
    st.markdown("<div class='input-container'>", unsafe_allow_html=True)
    context = st.text_area("Enter the context (max 400 words):")
    question = st.text_input("Enter your question:")
    st.markdown("</div>", unsafe_allow_html=True)

    if st.button("Get Answer"):
        #st.markdown("<div class='result-container'>", unsafe_allow_html=True)
        if not question or not context:
            st.warning("Please enter both a question and a context.")
        else:
            # Tokenize input
            try:
                # Check word count in the context
                if len(context.split()) > 400:
                    raise ValueError("Context exceeds 400 words limit.")

                inputs = tokenizer.encode_plus(question, context, return_tensors='pt')

                # Get predictions
                outputs = model(**inputs)
                start_logits = outputs.start_logits
                end_logits = outputs.end_logits

                # Get top N answer spans
                top_n = 3
                start_indexes = start_logits.argsort(dim=1, descending=True)[:, :top_n]
                end_indexes = end_logits.argsort(dim=1, descending=True)[:, :top_n]

                # Display detailed answers
                st.subheader(f"Question: {question}")
                for i in range(top_n):
                    start_index = start_indexes[0, i].item()
                    end_index = end_indexes[0, i].item()
                    answer = tokenizer.decode(inputs['input_ids'][0, start_index:end_index + 1])

                    # Highlight answer in context
                    highlighted_context = f"{context[:start_index]}**{context[start_index:end_index+1]}**{context[end_index+1:]}"

                    # Display confidence scores
                    confidence_start = start_logits[0, start_index].item()
                    confidence_end = end_logits[0, end_index].item()
                    if answer == "":
                        continue
                    else:
                      st.markdown(f"<div class='answer'><strong>Answer:</strong> {answer}<br>"
                                f"<strong>Confidence (Start):</strong> {confidence_start:.4f}<br>"
                                f"<strong>Confidence (End):</strong> {confidence_end:.4f}</div>", unsafe_allow_html=True)

            except ValueError as ve:
                st.error(str(ve))
            except Exception as e:
                st.error(f"An error occurred: {e}")
        st.markdown("</div>", unsafe_allow_html=True)

if __name__ == "__main__":
    main()