Amelia-James commited on
Commit
523c362
·
verified ·
1 Parent(s): 3caaaa1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -2,6 +2,7 @@ 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
7
  load_dotenv()
@@ -27,8 +28,8 @@ def generate_mcqs_from_text(user_text):
27
  B. [Option 2]
28
  C. [Option 3]
29
  D. [Option 4]
30
-
31
- Additionally, provide the correct answers in a separate section titled "Correct Answers" in the format:
32
  Question 1: [Correct Option]
33
  Question 2: [Correct Option], etc.
34
 
@@ -42,33 +43,37 @@ def generate_mcqs_from_text(user_text):
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
- # Split based on the "Correct Answers" section
49
- correct_answers_start = mcqs_and_answers.find("Correct Answers")
50
- if correct_answers_start == -1:
51
- raise ValueError("Could not find 'Correct Answers:' in the response.")
52
-
53
- # Extract MCQs and correct answers
54
- mcqs = mcqs_and_answers[:correct_answers_start].strip()
55
- correct_answers_section = mcqs_and_answers[correct_answers_start:].strip()
56
-
57
- # Extract the correct answers
58
- correct_answers = {}
59
- for line in correct_answers_section.splitlines():
60
- if line.startswith("Question"):
61
- q, ans = line.split(":")
62
- correct_answers[q.strip()] = ans.strip()
63
-
64
- return mcqs, correct_answers
 
 
 
65
  except Exception as e:
66
  st.error(f"Failed to parse response: {e}")
67
  st.write("Raw response:", mcqs_and_answers) # Display raw output for troubleshooting
68
- return None, None
69
 
70
  # Function to evaluate user answers
71
  def evaluate_answers(mcqs, user_answers):
 
72
  prompt = f"""
73
  Here are the user's answers to the following multiple-choice questions.
74
  Please evaluate them as an experienced teacher, providing feedback on correctness,
@@ -99,14 +104,14 @@ if st.button("Generate MCQs"):
99
  mcqs_and_answers = generate_mcqs_from_text(user_text)
100
 
101
  # Parse the MCQs and correct answers
102
- mcqs, correct_answers = parse_mcqs_and_answers(mcqs_and_answers)
103
 
104
- if mcqs and correct_answers:
105
  # Display the first MCQ question
106
- questions = mcqs.splitlines()
107
  question = questions[0]
108
 
109
- # Show question options
110
  st.write(f"Question: {question}")
111
 
112
  options = ["A", "B", "C", "D"] # Example options, you may adjust according to your response
 
2
  from dotenv import load_dotenv
3
  import streamlit as st
4
  from groq import Groq
5
+ import re
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
 
28
  B. [Option 2]
29
  C. [Option 3]
30
  D. [Option 4]
31
+
32
+ Correct Answers:
33
  Question 1: [Correct Option]
34
  Question 2: [Correct Option], etc.
35
 
 
43
  print("API Response:\n", response) # Debugging: Inspect the raw response
44
  return response
45
 
46
+ # Function to parse MCQs and answers with a flexible approach
47
  def parse_mcqs_and_answers(mcqs_and_answers):
48
  try:
49
+ # Regular expression to match MCQs and answers
50
+ mcq_pattern = r"(Question \d+:.+?)(?=Question \d+|$)"
51
+ answer_pattern = r"(Question \d+: .+?)\n(A|B|C|D)\. .+"
52
+
53
+ # Extract questions
54
+ questions = re.findall(mcq_pattern, mcqs_and_answers)
55
+ # Extract answers
56
+ answers = re.findall(answer_pattern, mcqs_and_answers)
57
+
58
+ if len(questions) == 0 or len(answers) == 0:
59
+ raise ValueError("Failed to extract questions or answers.")
60
+
61
+ # Parse questions and answers
62
+ mcqs = {}
63
+ for q, ans in zip(questions, answers):
64
+ question_text = q.strip()
65
+ answer_text = ans.strip()
66
+ mcqs[question_text] = answer_text
67
+
68
+ return mcqs
69
  except Exception as e:
70
  st.error(f"Failed to parse response: {e}")
71
  st.write("Raw response:", mcqs_and_answers) # Display raw output for troubleshooting
72
+ return None
73
 
74
  # Function to evaluate user answers
75
  def evaluate_answers(mcqs, user_answers):
76
+ # Here we assume the user_answers is a dictionary of answers for each question
77
  prompt = f"""
78
  Here are the user's answers to the following multiple-choice questions.
79
  Please evaluate them as an experienced teacher, providing feedback on correctness,
 
104
  mcqs_and_answers = generate_mcqs_from_text(user_text)
105
 
106
  # Parse the MCQs and correct answers
107
+ mcqs = parse_mcqs_and_answers(mcqs_and_answers)
108
 
109
+ if mcqs:
110
  # Display the first MCQ question
111
+ questions = list(mcqs.keys())
112
  question = questions[0]
113
 
114
+ # Show question options (Here we assume options are A, B, C, D)
115
  st.write(f"Question: {question}")
116
 
117
  options = ["A", "B", "C", "D"] # Example options, you may adjust according to your response