File size: 6,154 Bytes
726f229 55cccf4 726f229 55cccf4 726f229 13b26b3 726f229 13b26b3 726f229 13b26b3 726f229 13b26b3 726f229 13b26b3 726f229 13b26b3 726f229 13b26b3 726f229 13b26b3 5ecd91c 13b26b3 5ecd91c 13b26b3 5ecd91c 13b26b3 5ecd91c 13b26b3 5ecd91c 55cccf4 726f229 13b26b3 726f229 55cccf4 726f229 13b26b3 726f229 55cccf4 726f229 55cccf4 726f229 5ecd91c 726f229 55cccf4 726f229 55cccf4 726f229 e103e61 |
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import os
import gradio as gr
from langchain.prompts import ChatPromptTemplate
from langchain_community.chat_models import ChatOpenAI
from langchain.schema import StrOutputParser
def create_the_question_prompt_template(num_questions, questions_type, difficulty_level, context):
"""Create the prompt template for the questions generator app."""
template = f"""Create {num_questions} {questions_type} questions keeping difficulty level as {difficulty_level} about the following concept/contents: {context}.
The format of the question could be one of the following:
- Multiple-choice:
Questions:
<Question1>:<A. Answer 1>,<B. Answer 2>,<C. Answer 3>,<D. Answer 4>
<Question2>:<A. Answer 1>,<B. Answer 2>,<C. Answer 3>,<D. Answer 4>
...
Answers:
<Answer1>:<A|B|C|D>
<Answer2>:<A|B|C|D>
...
Example:
-Questions:
-1.What is the time complexity of a binary search tree?
A.O(n)
B.O(log n)
C.O(n^2)
D.O(1)
-Answers:
1.B
- True-false:
Questions:
<Question1>: <True|False>
<Question2>: <True|False>
...
Answers:
<Answer1>: <True|False>
<Answer2>: <True|False>
...
Example:
- Questions:
- 1. Binary search trees are implemented using linked lists.
- 2. The time complexity of a binary search tree is O(n).
- Answers:
- 1. False
- 2. True
- Open-ended:
Questions:
<Question1>:
<Question2>:
...
Answers:
<Answer1>:
<Answer2>:
Example:
Questions:
- 1. What is a binary search tree?
- 2. Binary search trees are implemented using linked lists.
- Answers:
1. A binary search tree is a data structure that is used to store data in a sorted manner.
2. Binary search trees are implemented using linked lists.
"""
return ChatPromptTemplate.from_template(template)
def create_question_chain(prompt_template, llm):
"""Creates the chain for the question generator app."""
return prompt_template | llm | StrOutputParser()
def split_questions_answers(question_response):
"""Function that splits the questions and answers from the question response."""
try:
# Separate questions and answers sections
questions_section = question_response.split("Answers:")[0].strip()
if "Answers:" in question_response:
answers_section = question_response.split("Answers:")[1].strip()
else:
answers_section = ""
# Format questions and answers
formatted_questions = format_questions(questions_section)
formatted_answers = format_answers(answers_section)
return formatted_questions, formatted_answers
except IndexError:
return "Error: Unable to parse the response.", ""
def format_questions(questions):
"""Format questions to display with proper alignment and structure."""
lines = questions.split("\n")
formatted = []
current_question = ""
for line in lines:
line = line.strip()
if line.startswith(("1.", "2.", "3.", "4.", "5.")): # New question detected
if current_question: # Add the previous question to the formatted list
formatted.append(current_question.strip())
current_question = f"\n{line}" # Start a new question block
elif line.startswith(("A.", "B.", "C.", "D.")): # Answer option detected
current_question += f"\n {line}" # Add with proper indentation
elif line: # Any additional text
current_question += f"\n {line}" # Continue the current question
# Append the last question
if current_question:
formatted.append(current_question.strip())
return("\n".join(formatted))
def format_answers(answers):
"""Format answers to display with consistent alignment."""
lines = answers.split('\n')
formatted = []
for line in lines:
if line.strip(): # Process non-empty lines
formatted.append(line.strip())
return "\n".join(formatted)
def generate_questions(context, num_questions, questions_type,difficulty_level):
"""Function to generate questions."""
os.environ["OPENAI_API_KEY"] = "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"
llm = ChatOpenAI(temperature=0.0)
prompt_template = create_the_question_prompt_template(num_questions, questions_type, difficulty_level, context)
chain = create_question_chain(prompt_template, llm)
try:
question_response = chain.invoke({"questions_type": questions_type, "num_questions": num_questions,"difficulty_level": difficulty_level, "context": context})
# Log the entire response for debugging
print("Question Response:", question_response)
questions, answers = split_questions_answers(question_response)
return questions, answers
except Exception as e:
return f"Error: {str(e)}", ""
# Define Gradio interface
# Define Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Quiz App - Generate Questions")
context_input = gr.Textbox(label="Context/Concept", placeholder="Enter the concept for the questions")
num_questions_input = gr.Slider(label="Number of Questions", minimum=1, maximum=5, value=2, step=1)
question_type_input = gr.Radio(label="Quiz Type", choices=["multiple-choice", "true-false", "open-ended"], value="multiple-choice")
dropdown = gr.Dropdown(
choices=["Easy", "Medium", "Hard"], # Options in the selection box
label="Select difficulty level ",
value="Medium" # Default value
)
generate_btn = gr.Button("Generate Questions")
with gr.Row():
questions_output = gr.Textbox(label="Generated Questions", lines=5)
answers_output = gr.Textbox(label="Generated Answers", lines=5)
generate_btn.click(
generate_questions,
inputs=[context_input, num_questions_input, question_type_input,dropdown],
outputs=[questions_output, answers_output],
)
# Launch the app
demo.launch() |