File size: 4,845 Bytes
453a2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6a8e75
453a2ee
 
 
 
c6a8e75
453a2ee
 
 
c6a8e75
 
 
453a2ee
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
import torch
import logging

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

class QuestionAnsweringSystem:
    def __init__(self, model_name):
        """Initialize the QA system with the specified model."""
        logger.info(f"Loading model and tokenizer from {model_name}")
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.model = AutoModelForQuestionAnswering.from_pretrained(model_name)
        self.max_length = 384
        
    def answer_question(self, context, question):
        """Process the question and context to generate an answer."""
        try:
            # Tokenize input text
            inputs = self.tokenizer(
                question,
                context,
                max_length=self.max_length,
                truncation="only_second",
                padding="max_length",
                return_tensors="pt"
            )
            
            # Generate model predictions
            with torch.no_grad():
                outputs = self.model(**inputs)
            
            # Extract answer span
            answer_start = torch.argmax(outputs.start_logits)
            answer_end = torch.argmax(outputs.end_logits)
            
            # Decode tokens to get the answer
            tokens = inputs.input_ids[0][answer_start:answer_end + 1]
            answer = self.tokenizer.decode(tokens, skip_special_tokens=True)
            
            # Calculate confidence score
            confidence_start = torch.softmax(outputs.start_logits, dim=1).max().item()
            confidence_end = torch.softmax(outputs.end_logits, dim=1).max().item()
            confidence = (confidence_start + confidence_end) / 2
            
            return answer, float(confidence)
            
        except Exception as e:
            logger.error(f"Error in answer generation: {str(e)}")
            return "An error occurred while processing your question.", 0.0

# Initialize the QA system
qa_system = QuestionAnsweringSystem("aman-augurs/bert-fine-tuned-qa3e")  # Replace with your model name

def process_query(context, question):
    """Handle the user query and return formatted results."""
    if not context or not question:
        return "Please provide both a context and a question."
    
    try:
        answer, confidence = qa_system.answer_question(context, question)
        
        if confidence < 0.1:
            return "I'm not confident enough to provide an answer based on the given context."
            
        response = f"Answer: {answer}\nConfidence: {confidence:.2%}"
        return response
        
    except Exception as e:
        logger.error(f"Error processing query: {str(e)}")
        return "An error occurred while processing your request."

# Create the Gradio interface
def create_interface():
    """Create and configure the Gradio interface."""
    return gr.Interface(
        fn=process_query,
        inputs=[
            gr.Textbox(
                label="Context",
                placeholder="Введите контекстный отрывок здесь...",
                lines=10
            ),
            gr.Textbox(
                label="Question",
                placeholder="Введите свой вопрос здесь..."
            )
        ],
        outputs=gr.Textbox(label="Response"),
        title="Вопросно-ответная система",
        description="""Это приложение использует тонко настроенную модель BERT для ответа на вопросы на основе предоставленного контекста. 
и задайте конкретный вопрос об этом.""",
        examples=[
            ["The Golden Gate Bridge is a suspension bridge spanning the Golden Gate strait, the one-mile-wide strait connecting San Francisco Bay and the Pacific Ocean. The structure links the U.S. city of San Francisco, California to Marin County, carrying both U.S. Route 101 and California State Route 1 across the strait.", 
             "How wide is the Golden Gate strait?"],
            ["Python is a high-level, interpreted programming language. Python's design philosophy emphasizes code readability with the use of significant indentation. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.",
             "What is Python's design philosophy?"]
        ],
        theme=gr.themes.Base()
    )

# Create app.py for Hugging Face Spaces
if __name__ == "__main__":
    interface = create_interface()
    interface.launch()