Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	All files has been added...
Browse files- bert_question_answering.py +49 -0
- checkpoint-1700/config.json +24 -0
- checkpoint-1700/model.safetensors +3 -0
- checkpoint-1700/tokenizer.json +0 -0
- checkpoint-1700/tokenizer_config.json +59 -0
- checkpoint-1700/vocab.txt +0 -0
- requirements.txt +3 -0
    	
        bert_question_answering.py
    ADDED
    
    | @@ -0,0 +1,49 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import torch
         | 
| 2 | 
            +
            from transformers import AutoTokenizer, AutoModelForQuestionAnswering
         | 
| 3 | 
            +
            import gradio as gr
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            model_name = "checkpoint-1700"
         | 
| 6 | 
            +
            tokenizer = AutoTokenizer.from_pretrained(model_name)
         | 
| 7 | 
            +
            model = AutoModelForQuestionAnswering.from_pretrained(model_name)
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            device = "cuda" if torch.cuda.is_available() else "cpu"
         | 
| 10 | 
            +
            model.to(device)
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            def answer_question(question, context):
         | 
| 13 | 
            +
                if not question.strip() or not context.strip():
         | 
| 14 | 
            +
                    return "Soru ve metin boş olamaz!"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                inputs = tokenizer(question, context, return_tensors="pt", truncation=True)
         | 
| 17 | 
            +
                inputs = {k: v.to(device) for k, v in inputs.items()}
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                with torch.no_grad():
         | 
| 20 | 
            +
                    outputs = model(**inputs)
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                answer_start = torch.argmax(outputs.start_logits)
         | 
| 23 | 
            +
                answer_end = torch.argmax(outputs.end_logits) + 1
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                input_ids = inputs["input_ids"][0]
         | 
| 26 | 
            +
                answer = tokenizer.convert_tokens_to_string(
         | 
| 27 | 
            +
                    tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end].cpu())
         | 
| 28 | 
            +
                )
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                return answer.strip()
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            demo = gr.Interface(
         | 
| 33 | 
            +
                fn=answer_question,
         | 
| 34 | 
            +
                inputs=[
         | 
| 35 | 
            +
                    gr.Textbox(label="Soru", placeholder="Örn: Türkiye'nin başkenti neresidir ?"),
         | 
| 36 | 
            +
                    gr.Textbox(
         | 
| 37 | 
            +
                        label="Metin", 
         | 
| 38 | 
            +
                        placeholder="Metni buraya girin...",
         | 
| 39 | 
            +
                        lines=10
         | 
| 40 | 
            +
                    )
         | 
| 41 | 
            +
                ],
         | 
| 42 | 
            +
                outputs=gr.Textbox(label="Cevap"),
         | 
| 43 | 
            +
                title="BERT Soru-Cevap Sistemi",
         | 
| 44 | 
            +
                description="Metin ve sorunuzu girin ve BERT modeli cevabı metin içerisinden çıkarsın.",
         | 
| 45 | 
            +
                theme="default",
         | 
| 46 | 
            +
            )
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            if __name__ == "__main__":
         | 
| 49 | 
            +
                demo.launch()
         | 
    	
        checkpoint-1700/config.json
    ADDED
    
    | @@ -0,0 +1,24 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            {
         | 
| 2 | 
            +
              "architectures": [
         | 
| 3 | 
            +
                "BertForQuestionAnswering"
         | 
| 4 | 
            +
              ],
         | 
| 5 | 
            +
              "attention_probs_dropout_prob": 0.1,
         | 
| 6 | 
            +
              "classifier_dropout": null,
         | 
| 7 | 
            +
              "hidden_act": "gelu",
         | 
| 8 | 
            +
              "hidden_dropout_prob": 0.1,
         | 
| 9 | 
            +
              "hidden_size": 768,
         | 
| 10 | 
            +
              "initializer_range": 0.02,
         | 
| 11 | 
            +
              "intermediate_size": 3072,
         | 
| 12 | 
            +
              "layer_norm_eps": 1e-12,
         | 
| 13 | 
            +
              "max_position_embeddings": 512,
         | 
| 14 | 
            +
              "model_type": "bert",
         | 
| 15 | 
            +
              "num_attention_heads": 12,
         | 
| 16 | 
            +
              "num_hidden_layers": 12,
         | 
| 17 | 
            +
              "pad_token_id": 0,
         | 
| 18 | 
            +
              "position_embedding_type": "absolute",
         | 
| 19 | 
            +
              "torch_dtype": "float32",
         | 
| 20 | 
            +
              "transformers_version": "4.52.4",
         | 
| 21 | 
            +
              "type_vocab_size": 2,
         | 
| 22 | 
            +
              "use_cache": true,
         | 
| 23 | 
            +
              "vocab_size": 32000
         | 
| 24 | 
            +
            }
         | 
    	
        checkpoint-1700/model.safetensors
    ADDED
    
    | @@ -0,0 +1,3 @@ | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            version https://git-lfs.github.com/spec/v1
         | 
| 2 | 
            +
            oid sha256:41b05b8c64e566185dea3939a20c6b7ab5f2a6949828cfb90b745762b46c4850
         | 
| 3 | 
            +
            size 440136504
         | 
    	
        checkpoint-1700/tokenizer.json
    ADDED
    
    | The diff for this file is too large to render. 
		See raw diff | 
|  | 
    	
        checkpoint-1700/tokenizer_config.json
    ADDED
    
    | @@ -0,0 +1,59 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            {
         | 
| 2 | 
            +
              "added_tokens_decoder": {
         | 
| 3 | 
            +
                "0": {
         | 
| 4 | 
            +
                  "content": "[PAD]",
         | 
| 5 | 
            +
                  "lstrip": false,
         | 
| 6 | 
            +
                  "normalized": false,
         | 
| 7 | 
            +
                  "rstrip": false,
         | 
| 8 | 
            +
                  "single_word": false,
         | 
| 9 | 
            +
                  "special": true
         | 
| 10 | 
            +
                },
         | 
| 11 | 
            +
                "1": {
         | 
| 12 | 
            +
                  "content": "[UNK]",
         | 
| 13 | 
            +
                  "lstrip": false,
         | 
| 14 | 
            +
                  "normalized": false,
         | 
| 15 | 
            +
                  "rstrip": false,
         | 
| 16 | 
            +
                  "single_word": false,
         | 
| 17 | 
            +
                  "special": true
         | 
| 18 | 
            +
                },
         | 
| 19 | 
            +
                "2": {
         | 
| 20 | 
            +
                  "content": "[CLS]",
         | 
| 21 | 
            +
                  "lstrip": false,
         | 
| 22 | 
            +
                  "normalized": false,
         | 
| 23 | 
            +
                  "rstrip": false,
         | 
| 24 | 
            +
                  "single_word": false,
         | 
| 25 | 
            +
                  "special": true
         | 
| 26 | 
            +
                },
         | 
| 27 | 
            +
                "3": {
         | 
| 28 | 
            +
                  "content": "[SEP]",
         | 
| 29 | 
            +
                  "lstrip": false,
         | 
| 30 | 
            +
                  "normalized": false,
         | 
| 31 | 
            +
                  "rstrip": false,
         | 
| 32 | 
            +
                  "single_word": false,
         | 
| 33 | 
            +
                  "special": true
         | 
| 34 | 
            +
                },
         | 
| 35 | 
            +
                "4": {
         | 
| 36 | 
            +
                  "content": "[MASK]",
         | 
| 37 | 
            +
                  "lstrip": false,
         | 
| 38 | 
            +
                  "normalized": false,
         | 
| 39 | 
            +
                  "rstrip": false,
         | 
| 40 | 
            +
                  "single_word": false,
         | 
| 41 | 
            +
                  "special": true
         | 
| 42 | 
            +
                }
         | 
| 43 | 
            +
              },
         | 
| 44 | 
            +
              "clean_up_tokenization_spaces": true,
         | 
| 45 | 
            +
              "cls_token": "[CLS]",
         | 
| 46 | 
            +
              "do_basic_tokenize": true,
         | 
| 47 | 
            +
              "do_lower_case": false,
         | 
| 48 | 
            +
              "extra_special_tokens": {},
         | 
| 49 | 
            +
              "mask_token": "[MASK]",
         | 
| 50 | 
            +
              "max_len": 512,
         | 
| 51 | 
            +
              "model_max_length": 512,
         | 
| 52 | 
            +
              "never_split": null,
         | 
| 53 | 
            +
              "pad_token": "[PAD]",
         | 
| 54 | 
            +
              "sep_token": "[SEP]",
         | 
| 55 | 
            +
              "strip_accents": null,
         | 
| 56 | 
            +
              "tokenize_chinese_chars": true,
         | 
| 57 | 
            +
              "tokenizer_class": "BertTokenizer",
         | 
| 58 | 
            +
              "unk_token": "[UNK]"
         | 
| 59 | 
            +
            }
         | 
    	
        checkpoint-1700/vocab.txt
    ADDED
    
    | The diff for this file is too large to render. 
		See raw diff | 
|  | 
    	
        requirements.txt
    ADDED
    
    | @@ -0,0 +1,3 @@ | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            gradio 
         | 
| 2 | 
            +
            transformers
         | 
| 3 | 
            +
            torch
         |