Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from langchain.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain_community.chat_models import ChatOpenAI
|
| 5 |
+
from langchain.schema import StrOutputParser
|
| 6 |
+
|
| 7 |
+
def create_the_question_prompt_template(num_questions, questions_type, context):
|
| 8 |
+
"""Create the prompt template for the questions generator app."""
|
| 9 |
+
template = f"""Create {num_questions} {questions_type} questions about the following concept/contents: {context}.
|
| 10 |
+
The format of the question could be one of the following:
|
| 11 |
+
- Multiple-choice:
|
| 12 |
+
Questions:
|
| 13 |
+
<Question1>:<a. Answer 1>,<b. Answer 2>,<c. Answer 3>,<d. Answer 4>
|
| 14 |
+
<Question2>:<a. Answer 1>,<b. Answer 2>,<c. Answer 3>,<d. Answer 4>
|
| 15 |
+
...
|
| 16 |
+
Answers:
|
| 17 |
+
<Answer1>:<a|b|c|d>
|
| 18 |
+
<Answer2>:<a|b|c|d>
|
| 19 |
+
...
|
| 20 |
+
Example:
|
| 21 |
+
-Questions:
|
| 22 |
+
-1.What is the time complexity of a binary search tree?
|
| 23 |
+
a.O(n)
|
| 24 |
+
b.O(log n)
|
| 25 |
+
c.O(n^2)
|
| 26 |
+
d.O(1)
|
| 27 |
+
-Answers:
|
| 28 |
+
1.b
|
| 29 |
+
- True-false:
|
| 30 |
+
Questions:
|
| 31 |
+
<Question1>: <True|False>
|
| 32 |
+
<Question2>: <True|False>
|
| 33 |
+
...
|
| 34 |
+
Answers:
|
| 35 |
+
<Answer1>: <True|False>
|
| 36 |
+
<Answer2>: <True|False>
|
| 37 |
+
...
|
| 38 |
+
Example:
|
| 39 |
+
- Questions:
|
| 40 |
+
- 1. Binary search trees are implemented using linked lists.
|
| 41 |
+
- 2. The time complexity of a binary search tree is O(n).
|
| 42 |
+
- Answers:
|
| 43 |
+
- 1. False
|
| 44 |
+
- 2. True
|
| 45 |
+
- Open-ended:
|
| 46 |
+
Questions:
|
| 47 |
+
<Question1>:
|
| 48 |
+
<Question2>:
|
| 49 |
+
...
|
| 50 |
+
Answers:
|
| 51 |
+
<Answer1>:
|
| 52 |
+
<Answer2>:
|
| 53 |
+
Example:
|
| 54 |
+
Questions:
|
| 55 |
+
- 1. What is a binary search tree?
|
| 56 |
+
- 2. Binary search trees are implemented using linked lists.
|
| 57 |
+
|
| 58 |
+
- Answers:
|
| 59 |
+
1. A binary search tree is a data structure that is used to store data in a sorted manner.
|
| 60 |
+
2. Binary search trees are implemented using linked lists.
|
| 61 |
+
"""
|
| 62 |
+
return ChatPromptTemplate.from_template(template)
|
| 63 |
+
|
| 64 |
+
def create_question_chain(prompt_template, llm):
|
| 65 |
+
"""Creates the chain for the question generator app."""
|
| 66 |
+
return prompt_template | llm | StrOutputParser()
|
| 67 |
+
|
| 68 |
+
def split_questions_answers(question_response):
|
| 69 |
+
print("question_response",question_response)
|
| 70 |
+
"""Function that splits the questions and answers from the question response."""
|
| 71 |
+
try:
|
| 72 |
+
questions_section = question_response.split("Answers:")[0].strip()
|
| 73 |
+
|
| 74 |
+
if "Answers:" in question_response:
|
| 75 |
+
answers_section = question_response.split("Answers:")[1].strip()
|
| 76 |
+
else:
|
| 77 |
+
answers_section = ""
|
| 78 |
+
|
| 79 |
+
# Format answers for better alignment
|
| 80 |
+
formatted_answers = format_answers(answers_section)
|
| 81 |
+
print("formatted_answers:",formatted_answers)
|
| 82 |
+
return questions_section, formatted_answers
|
| 83 |
+
except IndexError:
|
| 84 |
+
return "Error: Unable to parse the response.", ""
|
| 85 |
+
|
| 86 |
+
def format_answers(answers):
|
| 87 |
+
"""Format answers to display with consistent alignment."""
|
| 88 |
+
lines = answers.split('\n')
|
| 89 |
+
formatted = []
|
| 90 |
+
|
| 91 |
+
for line in lines:
|
| 92 |
+
if line.strip(): # Only process non-empty lines
|
| 93 |
+
formatted.append(line.strip()) # Add the line directly without additional numbering
|
| 94 |
+
|
| 95 |
+
return "\n".join(formatted)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def generate_questions(api_key, context, num_questions, questions_type):
|
| 99 |
+
"""Function to generate questions."""
|
| 100 |
+
if not api_key.strip():
|
| 101 |
+
return "Error: Please provide a valid OpenAI API key.", ""
|
| 102 |
+
|
| 103 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 104 |
+
|
| 105 |
+
llm = ChatOpenAI(temperature=0.0)
|
| 106 |
+
prompt_template = create_the_question_prompt_template(num_questions, questions_type, context)
|
| 107 |
+
chain = create_question_chain(prompt_template, llm)
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
question_response = chain.invoke({"questions_type": questions_type, "num_questions": num_questions, "context": context})
|
| 111 |
+
|
| 112 |
+
# Log the entire response for debugging
|
| 113 |
+
print("Question Response:", question_response)
|
| 114 |
+
|
| 115 |
+
questions, answers = split_questions_answers(question_response)
|
| 116 |
+
return questions, answers
|
| 117 |
+
except Exception as e:
|
| 118 |
+
return f"Error: {str(e)}", ""
|
| 119 |
+
|
| 120 |
+
# Define Gradio interface
|
| 121 |
+
# Define Gradio interface
|
| 122 |
+
with gr.Blocks() as demo:
|
| 123 |
+
gr.Markdown("# Quiz App - Generate Questions")
|
| 124 |
+
with gr.Row():
|
| 125 |
+
api_key_input = gr.Textbox(label="OpenAI API Key", type="password", placeholder="Enter your OpenAI API key")
|
| 126 |
+
context_input = gr.Textbox(label="Context/Concept", placeholder="Enter the concept for the questions")
|
| 127 |
+
num_questions_input = gr.Slider(label="Number of Questions", minimum=1, maximum=5, value=2, step=1)
|
| 128 |
+
question_type_input = gr.Radio(label="Quiz Type", choices=["multiple-choice", "true-false", "open-ended"], value="multiple-choice")
|
| 129 |
+
generate_btn = gr.Button("Generate Questions")
|
| 130 |
+
with gr.Row():
|
| 131 |
+
questions_output = gr.Textbox(label="Generated Questions", lines=5)
|
| 132 |
+
answers_output = gr.Textbox(label="Generated Answers", lines=5)
|
| 133 |
+
|
| 134 |
+
generate_btn.click(
|
| 135 |
+
generate_questions,
|
| 136 |
+
inputs=[api_key_input, context_input, num_questions_input, question_type_input],
|
| 137 |
+
outputs=[questions_output, answers_output],
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Launch the app
|
| 141 |
+
demo.launch()
|