Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
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
|
46 |
def parse_mcqs_and_answers(mcqs_and_answers):
|
47 |
try:
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
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
|
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
|
103 |
|
104 |
-
if mcqs
|
105 |
# Display the first MCQ question
|
106 |
-
questions = mcqs.
|
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
|