karthikmns commited on
Commit
ff34f2d
·
verified ·
1 Parent(s): 9199e24

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ from transformers import AutoTokenizer, AutoModelForQuestionAnswering, TrainingArguments, Trainer, pipeline
4
+
5
+ # Load your dataset
6
+ dataset = load_dataset("your-username/your-dataset-name")
7
+
8
+ # Load a pre-trained model and tokenizer
9
+ model_name = "distilbert-base-uncased-distilled-squad"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForQuestionAnswering.from_pretrained(model_name)
12
+
13
+ # Tokenize the dataset
14
+ def tokenize_function(examples):
15
+ return tokenizer(examples["question"], examples["text"], truncation=True, padding="max_length")
16
+
17
+ tokenized_datasets = dataset.map(tokenize_function, batched=True)
18
+
19
+ # Set up training arguments
20
+ training_args = TrainingArguments(
21
+ output_dir="./results",
22
+ evaluation_strategy="epoch",
23
+ learning_rate=2e-5,
24
+ per_device_train_batch_size=16,
25
+ per_device_eval_batch_size=16,
26
+ num_train_epochs=3,
27
+ weight_decay=0.01,
28
+ )
29
+
30
+ # Create Trainer instance
31
+ trainer = Trainer(
32
+ model=model,
33
+ args=training_args,
34
+ train_dataset=tokenized_datasets["train"],
35
+ eval_dataset=tokenized_datasets["validation"],
36
+ )
37
+
38
+ # Fine-tune the model
39
+ trainer.train()
40
+
41
+ # Save the model
42
+ model.save_pretrained("./fine_tuned_model")
43
+
44
+ # Create a question-answering pipeline
45
+ qa_pipeline = pipeline("question-answering", model="./fine_tuned_model")
46
+
47
+ # Define the Gradio interface function
48
+ def answer_question(context, question):
49
+ result = qa_pipeline(question=question, context=context)
50
+ return result['answer']
51
+
52
+ # Create and launch the Gradio interface
53
+ iface = gr.Interface(
54
+ fn=answer_question,
55
+ inputs=["text", "text"],
56
+ outputs="text",
57
+ title="Textbook Q&A",
58
+ description="Ask a question about your textbook!"
59
+ )
60
+
61
+ iface.launch()