Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def highlight_answer(context, answer):
|
| 2 |
+
"""
|
| 3 |
+
Highlight the answer in the given context.
|
| 4 |
+
|
| 5 |
+
Parameters:
|
| 6 |
+
- context (str): The context in which the answer is found.
|
| 7 |
+
- answer (str): The answer to be highlighted.
|
| 8 |
+
|
| 9 |
+
Returns:
|
| 10 |
+
- str: The context with the answer highlighted by '<h>' tags.
|
| 11 |
+
|
| 12 |
+
Example:
|
| 13 |
+
>>> context = 'The quick brown fox jumps over the lazy dog.'
|
| 14 |
+
>>> answer = 'fox'
|
| 15 |
+
>>> highlight_answer(context, answer)
|
| 16 |
+
'The quick brown <h> fox <h> jumps over the lazy dog.'
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
context_splits = context.split(answer)
|
| 20 |
+
|
| 21 |
+
text = ""
|
| 22 |
+
for split in context_splits:
|
| 23 |
+
text += split
|
| 24 |
+
text += ' <h> '
|
| 25 |
+
text += answer
|
| 26 |
+
text += ' <h> '
|
| 27 |
+
text += split
|
| 28 |
+
|
| 29 |
+
return text
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def prepare_instruction(answer_highlighted_context):
|
| 33 |
+
"""
|
| 34 |
+
Prepare an instruction prompt for generating a question.
|
| 35 |
+
|
| 36 |
+
Parameters:
|
| 37 |
+
- answer_highlighted_context (str): The context with the answer highlighted by '<h>' tags.
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
- str: The instruction prompt string.
|
| 41 |
+
|
| 42 |
+
Example:
|
| 43 |
+
>>> answer_highlighted_context = 'The quick brown <h> fox <h> jumps over the lazy dog.'
|
| 44 |
+
>>> prepare_instruction(answer_highlighted_context)
|
| 45 |
+
'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 '
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
instruction_prompt = f"""Generate a question whose answer is highlighted by <h> from the context delimited by the triple backticks.
|
| 49 |
+
context:
|
| 50 |
+
```
|
| 51 |
+
{answer_highlighted_context}
|
| 52 |
+
```
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
return instruction_prompt
|
| 56 |
+
|
| 57 |
+
from transformers import pipeline
|
| 58 |
+
|
| 59 |
+
pipe = pipeline('text2text-generation', model='mohammedaly2222002/t5-small-squad-qg-v2', device_map='auto')
|
| 60 |
+
|
| 61 |
+
import gradio as gr
|
| 62 |
+
|
| 63 |
+
def processed(question,answer,num):
|
| 64 |
+
# The uploaded image is a PIL image
|
| 65 |
+
answer_highlighted_context = highlight_answer(context=question, answer=answer)
|
| 66 |
+
prompt = prepare_instruction(answer_highlighted_context)
|
| 67 |
+
outputs = pipe(prompt,num_return_sequences=int(num),num_beams=5,num_beam_groups=5,diversity_penalty=1.0)
|
| 68 |
+
result="Generated questions are "+"\n"+"\n"
|
| 69 |
+
number=0
|
| 70 |
+
for output in outputs:
|
| 71 |
+
number=number+1
|
| 72 |
+
result+=str(number)+"."+output['generated_text']+"\n"
|
| 73 |
+
return result
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
iface = gr.Interface(processed, # Function to process the image
|
| 77 |
+
inputs=[
|
| 78 |
+
gr.Textbox(label="Question"),
|
| 79 |
+
gr.Textbox(label="Answer"),
|
| 80 |
+
gr.Textbox(label="Numbers of question")],
|
| 81 |
+
outputs="text" # Image output
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
iface.launch()
|