File size: 3,212 Bytes
4ad99c8
 
 
 
 
 
 
 
 
 
992173c
 
 
 
4ad99c8
9ca188f
4ad99c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86a4f8b
4ad99c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio.components import Textbox
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5ForConditionalGeneration
from peft import PeftModel, PeftConfig
import torch
import datasets

# Load your fine-tuned model and tokenizer
model_name = "google/flan-t5-large"
tokenizer = AutoTokenizer.from_pretrained(model_name)
peft_name = "checkiejan/flan-t5-prefix-25-9-2"
peft_config = PeftConfig.from_pretrained(peft_name)
model = AutoModelForSeq2SeqLM.from_pretrained(peft_config.base_model_name_or_path)
model = PeftModel.from_pretrained(model, peft_name)

peft_name = "checkiejan/prefix-paraphase-50-20-auto"
peft_config = PeftConfig.from_pretrained(peft_name)
paraphrase_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
paraphrase_model = PeftModel.from_pretrained(paraphrase_model, peft_name)

max_length = 512
max_target_length = 128

# Load your dataset
dataset = datasets.load_dataset("minh21/cpgQA-v1.0-unique-context-test-10-percent-validation-10-percent", split="test")
dataset = dataset.shuffle()
dataset = dataset.select(range(5))


def paraphrase_answer(question, answer):
    # Combine question and context
    input_text = f"question: {question}. Paraphrase the answer to make it more natural answer: {answer}"

    # Tokenize the input text
    input_ids = tokenizer(
        input_text,
        return_tensors="pt",
        padding="max_length",
        truncation=True,
        max_length=max_length,
    ).input_ids

    # Generate the answer
    with torch.no_grad():
        generated_ids = paraphrase_model.generate(input_ids=input_ids, max_new_tokens=max_target_length)

    # Decode and return the generated answer
    paraphrased_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True)

    return paraphrased_answer


# Define your function to generate answers
def generate_answer(question, context):
    # Combine question and context
    input_text = f"question: {question} context: {context}"

    # Tokenize the input text
    input_ids = tokenizer(
        input_text,
        return_tensors="pt",
        padding="max_length",
        truncation=True,
        max_length=max_length,
    ).input_ids

    # Generate the answer
    with torch.no_grad():
        generated_ids = model.generate( input_ids=input_ids, max_new_tokens=max_target_length)

    # Decode and return the generated answer
    generated_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True)

    # Paraphrase answer
    paraphrased_answer = paraphrase_answer(question, generated_answer)

    return generated_answer, paraphrased_answer


# Define a function to list examples from the dataset
def list_examples():
    examples = []
    for example in dataset:
        context = example["context"]
        question = example["question"]
        examples.append([question, context])
    return examples


# Create a Gradio interface
iface = gr.Interface(
    fn=generate_answer,
    inputs=[
        Textbox(label="Question"),
        Textbox(label="Context")
    ],
    outputs=[
        Textbox(label="Generated Answer"),
        Textbox(label="Natural Answer")
    ],
    examples=list_examples()
)

# Launch the Gradio interface
iface.launch()