YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

from transformers import pipeline

Define the model name

model_name = "deepset/roberta-base-squad2"

Initialize the question-answering pipeline with the new model

nlp = pipeline("question-answering", model=model_name, tokenizer=model_name)

Function to load context from a file

def load_context(file_path): try: with open(file_path, 'r', encoding='utf-8') as file: return file.read() except FileNotFoundError: print(f"Error: Context file not found: {file_path}") return ""

Function to split long text into chunks (max 500 tokens per chunk)

def split_text_into_chunks(text, chunk_size=500): words = text.split() return [' '.join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]

Function to get the best answer from multiple chunks

def get_best_answer(question, context_chunks): best_answer = None best_score = 0

for chunk in context_chunks:
    QA_input = {'question': question, 'context': chunk}
    try:
        res = nlp(QA_input)
        answer = res.get('answer', 'No answer found')
        confidence = res.get('score', 0)

        # Keep track of the best answer
        if confidence > best_score:
            best_score = confidence
            best_answer = answer.strip()

    except Exception as e:
        print(f"Error processing chunk: {str(e)}")

# Return the best answer found or a default message
return best_answer if best_answer and best_score > 0.3 else "Sorry, I couldn't find a reliable answer."

Define path to your context file

context_file_path = 'information.txt'

Load the full context and split it into chunks

full_context = load_context(context_file_path) context_chunks = split_text_into_chunks(full_context)

Allow user to ask questions dynamically

while True: question = input("\nEnter your question (or type 'exit' to quit): ") if question.lower() == "exit": break answer = get_best_answer(question, context_chunks) print(f"Answer: {answer}")

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support