File size: 2,614 Bytes
3ab6b06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def highlight_answer(context, answer):
    """
    Highlight the answer in the given context.

    Parameters:
        - context (str): The context in which the answer is found.
        - answer (str): The answer to be highlighted.

    Returns:
        - str: The context with the answer highlighted by '<h>' tags.

    Example:
    >>> context = 'The quick brown fox jumps over the lazy dog.'
    >>> answer = 'fox'
    >>> highlight_answer(context, answer)
    'The quick brown <h> fox <h> jumps over the lazy dog.'
    """

    context_splits = context.split(answer)

    text = ""
    for split in context_splits:
        text += split
        text += ' <h> '
        text += answer
        text += ' <h> '
        text += split

    return text


def prepare_instruction(answer_highlighted_context):
    """
    Prepare an instruction prompt for generating a question.

    Parameters:
        - answer_highlighted_context (str): The context with the answer highlighted by '<h>' tags.

    Returns:
        - str: The instruction prompt string.

    Example:
    >>> answer_highlighted_context = 'The quick brown <h> fox <h> jumps over the lazy dog.'
    >>> prepare_instruction(answer_highlighted_context)
    'Generate a question whose answer is highlighted by <h> from the context delimited by the triple backticks.\\n    context:\\n    ```\\n    The quick brown <h> fox <h> jumps over the lazy dog.\\n    ```\\n    '
    """

    instruction_prompt = f"""Generate a question whose answer is highlighted by <h> from the context delimited by the triple backticks.
    context:
    ```
    {answer_highlighted_context}
    ```
    """

    return instruction_prompt

from transformers import pipeline

pipe = pipeline('text2text-generation', model='mohammedaly2222002/t5-small-squad-qg-v2', device_map='auto')

import gradio as gr

def processed(question,answer,num):
    # The uploaded image is a PIL image
    answer_highlighted_context = highlight_answer(context=question, answer=answer)
    prompt = prepare_instruction(answer_highlighted_context)
    outputs = pipe(prompt,num_return_sequences=int(num),num_beams=5,num_beam_groups=5,diversity_penalty=1.0)
    result="Generated questions are "+"\n"+"\n"
    number=0
    for output in outputs:
      number=number+1
      result+=str(number)+"."+output['generated_text']+"\n"
    return result


iface = gr.Interface(processed,  # Function to process the image
    inputs=[ 
        gr.Textbox(label="Question"), 
        gr.Textbox(label="Answer"),
        gr.Textbox(label="Numbers of question")],
    outputs="text"  # Image output
)

iface.launch()