Spaces:
Runtime error
Runtime error
Download app.py from waygazer/qa_roberta: direct link, hf CLI and curl.
- Browser
- Download file 4.55 kB
-
https://huggingface.co/spaces/waygazer/qa_roberta/resolve/main/app.py
- Command line
-
hf download hf://spaces/waygazer/qa_roberta/app.py
-
curl -L -o app.py https://huggingface.co/spaces/waygazer/qa_roberta/resolve/main/app.py
4.55 kB
| import spaces | |
| import torch | |
| import gradio as gr | |
| from transformers import ( | |
| AutoModelForQuestionAnswering, | |
| AutoTokenizer | |
| ) | |
| # -------------------------------------------------- | |
| # 1. Load the RoBERTa SQuAD2 model | |
| # -------------------------------------------------- | |
| mdl_name = "deepset/roberta-base-squad2" | |
| device = torch.device( | |
| "cuda" if torch.cuda.is_available() else "cpu" | |
| ) | |
| print("Loading model:", mdl_name) | |
| print("Using device:", device) | |
| tokenizer = AutoTokenizer.from_pretrained(mdl_name) | |
| model = AutoModelForQuestionAnswering.from_pretrained( | |
| mdl_name | |
| ) | |
| model.to(device) | |
| model.eval() | |
| print("Model loaded successfully!") | |
| # -------------------------------------------------- | |
| # 2. Question answering function | |
| # -------------------------------------------------- | |
| def answer_question(question, context): | |
| if not question or not context: | |
| return "Please enter both a question and context." | |
| # Tokenize and split long contexts into overlapping chunks | |
| encoded = tokenizer( | |
| question, | |
| context, | |
| max_length=512, | |
| truncation="only_second", | |
| stride=128, | |
| return_overflowing_tokens=True, | |
| return_offsets_mapping=True, | |
| padding="max_length", | |
| return_tensors="pt" | |
| ) | |
| # Save character offsets and sequence information | |
| offsets = encoded.pop("offset_mapping").tolist() | |
| sequence_ids = [ | |
| encoded.sequence_ids(i) | |
| for i in range(encoded["input_ids"].shape[0]) | |
| ] | |
| # Remove metadata not accepted by the model | |
| encoded.pop("overflow_to_sample_mapping", None) | |
| # Move model inputs to CPU/GPU | |
| model_inputs = { | |
| key: value.to(device) | |
| for key, value in encoded.items() | |
| } | |
| # Run model inference | |
| with torch.inference_mode(): | |
| outputs = model(**model_inputs) | |
| best_difference = float("-inf") | |
| best_answer = "" | |
| # Examine every context chunk | |
| for i in range(len(sequence_ids)): | |
| start_logits = outputs.start_logits[i] | |
| end_logits = outputs.end_logits[i] | |
| input_ids = encoded["input_ids"][i] | |
| # Find CLS token for the no-answer score | |
| cls_index = ( | |
| input_ids == tokenizer.cls_token_id | |
| ).nonzero(as_tuple=True)[0][0].item() | |
| null_score = ( | |
| start_logits[cls_index].item() | |
| + end_logits[cls_index].item() | |
| ) | |
| valid_positions = [ | |
| j for j, seq_id in enumerate(sequence_ids[i]) | |
| if seq_id == 1 and offsets[i][j][1] > offsets[i][j][0] | |
| ] | |
| chunk_best_score = float("-inf") | |
| chunk_best_answer = "" | |
| # Find the best answer span | |
| for start in valid_positions: | |
| for end in valid_positions: | |
| if end < start: | |
| continue | |
| if end - start + 1 > 30: | |
| break | |
| score = ( | |
| start_logits[start].item() | |
| + end_logits[end].item() | |
| ) | |
| if score > chunk_best_score: | |
| start_char = offsets[i][start][0] | |
| end_char = offsets[i][end][1] | |
| chunk_best_score = score | |
| chunk_best_answer = context[ | |
| start_char:end_char | |
| ] | |
| # Compare answer score against no-answer score | |
| difference = chunk_best_score - null_score | |
| if difference > best_difference: | |
| best_difference = difference | |
| best_answer = chunk_best_answer | |
| # SQuAD2 supports questions without an answer | |
| if best_difference <= 0 or not best_answer.strip(): | |
| return "No answer found in the provided context." | |
| return best_answer.strip() | |
| # -------------------------------------------------- | |
| # 3. Gradio user interface | |
| # -------------------------------------------------- | |
| demo = gr.Interface( | |
| fn=answer_question, | |
| inputs=[ | |
| gr.Textbox( | |
| label="Question", | |
| placeholder="Enter your question..." | |
| ), | |
| gr.Textbox( | |
| label="Context", | |
| lines=10, | |
| placeholder="Enter the information to search..." | |
| ) | |
| ], | |
| outputs=gr.Textbox(label="Answer"), | |
| title="RoBERTa Question Answering", | |
| description="Ask questions about the supplied context using deepset/roberta-base-squad2." | |
| ) | |
| # -------------------------------------------------- | |
| # 4. Launch application | |
| # -------------------------------------------------- | |
| if __name__ == "__main__": | |
| demo.launch() | |