Amelia-James commited on
Commit
4cabe54
·
verified ·
1 Parent(s): 471877f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from groq import Groq
4
+ import streamlit as st
5
+
6
+ # Load environment variables
7
+ load_dotenv()
8
+
9
+ # Initialize the Groq client with API key
10
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
11
+
12
+ # Function to generate MCQs from the user text
13
+ def generate_mcqs_from_text(user_text):
14
+ prompt = f"""
15
+ You are a 35-year experienced educator specializing in crafting challenging and insightful MCQs.
16
+ Based on the following text, generate between 30 to 50 multiple-choice questions (MCQs).
17
+ Each question should:
18
+ 1. Test critical thinking and understanding of the content.
19
+ 2. Include four options (A, B, C, D).
20
+ 3. Format the output as follows:
21
+
22
+ Question: [Your question here]
23
+ A. [Option 1]
24
+ B. [Option 2]
25
+ C. [Option 3]
26
+ D. [Option 4]
27
+
28
+ Additionally, provide the correct answers in a separate section titled "Correct Answers" in the format:
29
+ Question 1: [Correct Option]
30
+ Question 2: [Correct Option], etc.
31
+
32
+ Text: {user_text}
33
+ """
34
+ chat_completion = client.chat.completions.create(
35
+ messages=[{"role": "user", "content": prompt}],
36
+ model="gemma2-9b-it", # Replace with a valid Groq model
37
+ )
38
+ return chat_completion.choices[0].message.content
39
+
40
+ # Function to evaluate user's answers
41
+ def evaluate_answers(mcqs, correct_answers, user_answers):
42
+ prompt = f"""
43
+ You are a 35-year experienced teacher evaluating a student's answers to the following MCQs.
44
+ For each question, provide detailed feedback on whether the student's answer is correct or incorrect,
45
+ and explain the reasoning.
46
+
47
+ MCQs: {mcqs}
48
+
49
+ Correct Answers: {correct_answers}
50
+
51
+ User Answers: {user_answers}
52
+ """
53
+ chat_completion = client.chat.completions.create(
54
+ messages=[{"role": "user", "content": prompt}],
55
+ model="gemma2-9b-it", # Replace with a valid Groq model
56
+ )
57
+ return chat_completion.choices[0].message.content
58
+
59
+ # Streamlit app
60
+ st.title("Experienced Teacher's MCQ Generator and Evaluator")
61
+ st.sidebar.title("MCQ Generator & Evaluator")
62
+
63
+ # User input for text data
64
+ user_text = st.sidebar.text_area("Enter the text for generating MCQs:")
65
+
66
+ if "mcqs" not in st.session_state:
67
+ st.session_state["mcqs"] = None
68
+ if "correct_answers" not in st.session_state:
69
+ st.session_state["correct_answers"] = None
70
+
71
+ # Generate MCQs
72
+ if st.sidebar.button("Generate MCQs"):
73
+ if user_text.strip():
74
+ with st.spinner("Generating MCQs..."):
75
+ mcqs_and_answers = generate_mcqs_from_text(user_text)
76
+
77
+ # Debugging the response
78
+ st.write("Raw Response from API:")
79
+ st.text(mcqs_and_answers)
80
+
81
+ # Try to split into MCQs and answers
82
+ try:
83
+ mcqs, correct_answers = mcqs_and_answers.split("Correct Answers:")
84
+ st.session_state["mcqs"] = mcqs.strip()
85
+ st.session_state["correct_answers"] = correct_answers.strip()
86
+ st.success("MCQs generated successfully!")
87
+ except ValueError:
88
+ st.error(
89
+ "The API response is not in the expected format. Please check the output or adjust the prompt."
90
+ )
91
+ st.text(mcqs_and_answers) # Show full response for debugging
92
+ else:
93
+ st.error("Please enter text for generating MCQs.")
94
+
95
+ # Display generated MCQs
96
+ if st.session_state["mcqs"]:
97
+ st.subheader("Generated MCQs")
98
+ st.text(st.session_state["mcqs"])
99
+
100
+ # User input for their answers
101
+ user_answers = st.text_area(
102
+ "Enter your answers for the MCQs (e.g., A, B, C, D for each question):"
103
+ )
104
+
105
+ # Evaluate answers
106
+ if st.button("Evaluate Answers"):
107
+ if st.session_state["mcqs"] and st.session_state["correct_answers"]:
108
+ with st.spinner("Evaluating answers..."):
109
+ evaluation_result = evaluate_answers(
110
+ st.session_state["mcqs"], st.session_state["correct_answers"], user_answers
111
+ )
112
+ st.subheader("Evaluation Result")
113
+ st.text(evaluation_result)
114
+ else:
115
+ st.error("Please generate MCQs first!")