Amelia-James commited on
Commit
ce8e40e
·
verified ·
1 Parent(s): f049aec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -45
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
- import streamlit as st
3
  from dotenv import load_dotenv
 
4
  from groq import Groq
5
 
6
  # Load environment variables from .env file
@@ -12,7 +12,7 @@ api_key = os.getenv("GROQ_API_KEY")
12
  # Initialize Groq client with the API key
13
  client = Groq(api_key=api_key)
14
 
15
- # Function to generate MCQs based on the input text
16
  def generate_mcqs_from_text(user_text):
17
  prompt = f"""
18
  You are a 35-year experienced educator specializing in crafting challenging and insightful MCQs.
@@ -36,13 +36,13 @@ def generate_mcqs_from_text(user_text):
36
  """
37
  chat_completion = client.chat.completions.create(
38
  messages=[{"role": "user", "content": prompt}],
39
- model="Llama-3.3-70b", # Replace with a valid Groq model
40
  )
41
  response = chat_completion.choices[0].message.content
42
  print("API Response:\n", response) # Debugging: Inspect the raw response
43
  return response
44
 
45
- # Function to parse the MCQs and correct answers from the API response
46
  def parse_mcqs_and_answers(mcqs_and_answers):
47
  try:
48
  # Try splitting on "Correct Answers:"
@@ -64,47 +64,62 @@ def parse_mcqs_and_answers(mcqs_and_answers):
64
  st.write("Raw response:", mcqs_and_answers) # Display raw output for troubleshooting
65
  return None, None
66
 
67
- # Streamlit app
68
- def main():
69
- st.title("MCQ Generator and Evaluator")
 
 
 
 
 
 
 
 
 
70
 
71
- # Input text from the user
72
- user_text = st.text_area("Enter the text for MCQ generation:", height=150)
 
 
 
73
 
74
- if st.button("Generate MCQs"):
75
- if user_text:
76
- # Generate MCQs from the input text
77
- mcqs_and_answers = generate_mcqs_from_text(user_text)
78
-
79
- # Parse the MCQs and correct answers
80
- mcqs, correct_answers = parse_mcqs_and_answers(mcqs_and_answers)
81
-
82
- if mcqs:
83
- # Display MCQs one by one and evaluate answers
84
- mcq_list = mcqs.splitlines()
85
- correct_count = 0
86
- for idx, mcq in enumerate(mcq_list):
87
- question = mcq.split("\n")[0]
88
- options = mcq.split("\n")[1:5]
89
- st.write(question)
90
- for option in options:
91
- st.write(option)
92
-
93
- user_answer = st.radio("Select your answer", options=["A", "B", "C", "D"], key=idx)
94
-
95
- if user_answer:
96
- # Evaluate the user's answer
97
- correct_answer = correct_answers.get(f"Question {idx+1}")
98
- if user_answer == correct_answer:
99
- st.success(f"Correct! The answer is {correct_answer}.")
100
- correct_count += 1
101
- else:
102
- st.error(f"Incorrect. The correct answer is {correct_answer}.")
103
-
104
- # Display final score
105
- st.write(f"Your final score is {correct_count}/{len(mcq_list)}.")
106
- else:
107
- st.error("Please enter some text to generate MCQs.")
108
 
109
- if __name__ == "__main__":
110
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
 
2
  from dotenv import load_dotenv
3
+ import streamlit as st
4
  from groq import Groq
5
 
6
  # Load environment variables from .env file
 
12
  # Initialize Groq client with the API key
13
  client = Groq(api_key=api_key)
14
 
15
+ # Function to generate MCQs from the user text
16
  def generate_mcqs_from_text(user_text):
17
  prompt = f"""
18
  You are a 35-year experienced educator specializing in crafting challenging and insightful MCQs.
 
36
  """
37
  chat_completion = client.chat.completions.create(
38
  messages=[{"role": "user", "content": prompt}],
39
+ model="gemma2-9b-it", # Replace with a valid Groq model
40
  )
41
  response = chat_completion.choices[0].message.content
42
  print("API Response:\n", response) # Debugging: Inspect the raw response
43
  return response
44
 
45
+ # Function to parse the MCQs and correct answers
46
  def parse_mcqs_and_answers(mcqs_and_answers):
47
  try:
48
  # Try splitting on "Correct Answers:"
 
64
  st.write("Raw response:", mcqs_and_answers) # Display raw output for troubleshooting
65
  return None, None
66
 
67
+ # Function to evaluate user answers
68
+ def evaluate_answers(mcqs, user_answers):
69
+ # Here we assume the user_answers is a dictionary of answers for each question
70
+ prompt = f"""
71
+ Here are the user's answers to the following multiple-choice questions.
72
+ Please evaluate them as an experienced teacher, providing feedback on correctness,
73
+ and explain why each answer is right or wrong.
74
+
75
+ MCQs: {mcqs}
76
+
77
+ User Answers: {user_answers}
78
+ """
79
 
80
+ # Make the API call to evaluate the answers
81
+ chat_completion = client.chat.completions.create(
82
+ messages=[{"role": "user", "content": prompt}],
83
+ model="gemma2-9b-it", # Replace with a valid Groq model
84
+ )
85
 
86
+ # Return the evaluation result
87
+ return chat_completion.choices[0].message.content
88
+
89
+ # Streamlit UI
90
+ st.title("MCQ Generator and Evaluator")
91
+
92
+ # User input
93
+ user_text = st.text_area("Enter text to generate MCQs:")
94
+
95
+ if st.button("Generate MCQs"):
96
+ # Generate MCQs from user-provided text
97
+ mcqs_and_answers = generate_mcqs_from_text(user_text)
98
+
99
+ # Parse the MCQs and correct answers
100
+ mcqs, correct_answers = parse_mcqs_and_answers(mcqs_and_answers)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ if mcqs and correct_answers:
103
+ # Display the first MCQ question
104
+ questions = mcqs.splitlines()
105
+ question = questions[0]
106
+
107
+ # Show question options
108
+ st.write(f"Question: {question}")
109
+
110
+ options = ["A", "B", "C", "D"] # Example options, you may adjust according to your response
111
+ user_answer = st.radio("Choose your answer:", options)
112
+
113
+ if st.button("Submit Answer"):
114
+ # Evaluate the answer
115
+ result = evaluate_answers(mcqs, {question: user_answer})
116
+ st.write(result)
117
+
118
+ # Show next question if available
119
+ if len(questions) > 1:
120
+ next_question = questions[1]
121
+ st.write(f"Next Question: {next_question}")
122
+ else:
123
+ st.write("You have completed all the questions!")
124
+ else:
125
+ st.error("Could not parse MCQs or answers.")