Serhan Yılmaz commited on
Commit
bfe5bbd
·
1 Parent(s): e65e67f

add manual input

Browse files
Files changed (1) hide show
  1. app.py +48 -44
app.py CHANGED
@@ -12,7 +12,7 @@ from transformers import pipeline
12
  import asyncio
13
 
14
  # Import the required functions from the pipeline file
15
- from pipeline_gradio_experimental import generate_basic_question, rank_questions_with_details
16
 
17
  # Set up logging
18
  logging.basicConfig(level=logging.INFO)
@@ -85,68 +85,72 @@ Score each question-answer pair on a scale of 0 to 10 based on the quality and r
85
  logger.error(f"Error in comparing questions: {e}")
86
  return {"question1_score": 0, "question2_score": 0, "explanation": "Failed to compare questions"}
87
 
88
- async def process_random_entry(progress=gr.Progress()):
89
- context, original_answer, original_question = get_random_entry()
90
-
91
- # Yield the original context, question, and answer immediately
92
- yield context, original_question, original_answer, gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
93
-
94
- # Simulate some processing time
95
  await asyncio.sleep(1)
96
- progress(0.3, desc="Generating questions...")
97
 
98
- basic_question = generate_basic_question(context, original_answer)
99
- _, _, enhanced_question = rank_questions_with_details(context, original_answer)
100
-
101
- await asyncio.sleep(1)
102
- progress(0.6, desc="Generating answers...")
103
 
 
104
  basic_answer = generate_answer(context, basic_question)
105
  enhanced_answer = generate_answer(context, enhanced_question)
106
 
107
- await asyncio.sleep(1)
108
- progress(0.9, desc="Comparing questions...")
109
-
110
- comparison_result = compare_questions(context, original_answer, basic_question, basic_answer, enhanced_question, enhanced_answer)
111
 
112
  winner = "Basic" if comparison_result["question1_score"] > comparison_result["question2_score"] else "Enhanced"
113
 
114
- # Yield the final results
115
- yield (
116
- context,
117
- original_question,
118
- original_answer,
119
- gr.update(visible=True),
120
- gr.update(visible=True, value=f"Question: {basic_question}\nAnswer: {basic_answer}"),
121
- gr.update(visible=True, value=f"Question: {enhanced_question}\nAnswer: {enhanced_answer}"),
122
- gr.update(visible=True, value=f"Question 1 Score: {comparison_result['question1_score']}\n"
123
- f"Question 2 Score: {comparison_result['question2_score']}\n"
124
- f"Explanation: {comparison_result['explanation']}\n"
125
- f"Winner: {winner} Generation")
126
  )
127
 
128
  # Create Gradio interface
129
  with gr.Blocks(theme=gr.themes.Default()) as iface:
130
  gr.Markdown("# Question Generation and Comparison")
131
- gr.Markdown("Click the button to get a random entry from the SQuAD dataset and compare basic and enhanced question generation.")
132
 
133
- random_button = gr.Button("Get Random Question")
134
-
135
- with gr.Column(visible=False) as output_column:
136
- context_output = gr.Textbox(label="Original Context")
137
- original_question_output = gr.Textbox(label="Original Question")
138
- original_answer_output = gr.Textbox(label="Original Answer")
139
- basic_generation_output = gr.Textbox(label="Basic Generation", visible=False)
140
- enhanced_generation_output = gr.Textbox(label="Enhanced Generation", visible=False)
141
- comparison_result_output = gr.Textbox(label="Comparison Result", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  random_button.click(
144
- fn=process_random_entry,
145
  outputs=[
146
- context_output,
 
147
  original_question_output,
148
- original_answer_output,
149
- output_column,
150
  basic_generation_output,
151
  enhanced_generation_output,
152
  comparison_result_output
 
12
  import asyncio
13
 
14
  # Import the required functions from the pipeline file
15
+ from pipeline_gradio_experimental import generate_single_question, rank_questions_with_details
16
 
17
  # Set up logging
18
  logging.basicConfig(level=logging.INFO)
 
85
  logger.error(f"Error in comparing questions: {e}")
86
  return {"question1_score": 0, "question2_score": 0, "explanation": "Failed to compare questions"}
87
 
88
+ async def process_entry(context, answer, progress=gr.Progress()):
89
+ progress(0, desc="Starting process...")
 
 
 
 
 
90
  await asyncio.sleep(1)
 
91
 
92
+ progress(0.2, desc="Generating questions...")
93
+ basic_question = generate_single_question(context, answer, [])
94
+ _, _, enhanced_question = rank_questions_with_details(context, answer)
 
 
95
 
96
+ progress(0.4, desc="Generating answers...")
97
  basic_answer = generate_answer(context, basic_question)
98
  enhanced_answer = generate_answer(context, enhanced_question)
99
 
100
+ progress(0.6, desc="Comparing questions...")
101
+ comparison_result = compare_questions(context, answer, basic_question, basic_answer, enhanced_question, enhanced_answer)
 
 
102
 
103
  winner = "Basic" if comparison_result["question1_score"] > comparison_result["question2_score"] else "Enhanced"
104
 
105
+ progress(1.0, desc="Process complete!")
106
+ return (
107
+ f"Question: {basic_question}\nAnswer: {basic_answer}",
108
+ f"Question: {enhanced_question}\nAnswer: {enhanced_answer}",
109
+ f"Question 1 Score: {comparison_result['question1_score']}\n"
110
+ f"Question 2 Score: {comparison_result['question2_score']}\n"
111
+ f"Explanation: {comparison_result['explanation']}\n"
112
+ f"Winner: {winner} Generation"
 
 
 
 
113
  )
114
 
115
  # Create Gradio interface
116
  with gr.Blocks(theme=gr.themes.Default()) as iface:
117
  gr.Markdown("# Question Generation and Comparison")
118
+ gr.Markdown("Enter a context and answer, or click 'Random' to get a random entry from the SQuAD dataset.")
119
 
120
+ with gr.Row():
121
+ with gr.Column(scale=2):
122
+ context_input = gr.Textbox(label="Context", lines=10)
123
+ answer_input = gr.Textbox(label="Answer", lines=2)
124
+ with gr.Row():
125
+ submit_button = gr.Button("Submit")
126
+ random_button = gr.Button("Random")
127
+
128
+ with gr.Column(scale=3):
129
+ original_question_output = gr.Textbox(label="Original Question from Dataset", lines=2)
130
+ basic_generation_output = gr.Textbox(label="Basic Generation", lines=4)
131
+ enhanced_generation_output = gr.Textbox(label="Enhanced Generation", lines=4)
132
+ comparison_result_output = gr.Textbox(label="Comparison Result", lines=6)
133
+
134
+ async def on_submit(context, answer):
135
+ return await process_entry(context, answer)
136
+
137
+ async def on_random():
138
+ context, answer, question = get_random_entry()
139
+ results = await process_entry(context, answer)
140
+ return [context, answer, question] + list(results)
141
+
142
+ submit_button.click(
143
+ fn=on_submit,
144
+ inputs=[context_input, answer_input],
145
+ outputs=[basic_generation_output, enhanced_generation_output, comparison_result_output]
146
+ )
147
 
148
  random_button.click(
149
+ fn=on_random,
150
  outputs=[
151
+ context_input,
152
+ answer_input,
153
  original_question_output,
 
 
154
  basic_generation_output,
155
  enhanced_generation_output,
156
  comparison_result_output