Amelia-James commited on
Commit
142a1cd
·
verified ·
1 Parent(s): b98d07a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -41
app.py CHANGED
@@ -1,3 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  from dotenv import load_dotenv
3
  from groq import Groq
@@ -9,14 +89,18 @@ load_dotenv()
9
  # Initialize the Groq client with API key
10
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
11
 
12
- # Function to generate MCQs based on the input text
13
  def generate_mcqs_from_text(user_text):
14
  prompt = f"""
15
- Based on the following text, generate between 30 to 50 multiple-choice questions.
16
- Each question should have four answer options, with one correct answer and three distractors.
17
- Make sure the questions are clear and test the user's understanding of the content.
18
- Provide the questions in the following format:
19
-
 
 
 
 
20
  Question: [Your question here]
21
  A. [Option 1]
22
  B. [Option 2]
@@ -28,51 +112,30 @@ def generate_mcqs_from_text(user_text):
28
  """
29
  chat_completion = client.chat.completions.create(
30
  messages=[{"role": "user", "content": prompt}],
31
- model="gemma2-9b-it", # Use a valid model that supports chat completions
32
- )
33
- return chat_completion.choices[0].message.content
34
-
35
- # Function to evaluate user answers
36
- def evaluate_answers(mcqs, user_answers):
37
- prompt = f"""
38
- Here are the user's answers to the following multiple-choice questions.
39
- Please evaluate them as an experienced teacher, providing feedback on correctness,
40
- and explain why each answer is right or wrong.
41
-
42
- MCQs: {mcqs}
43
- User Answers: {user_answers}
44
- """
45
- chat_completion = client.chat.completions.create(
46
- messages=[{"role": "user", "content": prompt}],
47
- model="gemma2-9b-it", # Use a valid model
48
  )
49
  return chat_completion.choices[0].message.content
50
 
51
  # Streamlit App
52
- st.title("Study Assistant - MCQ Generator & Evaluator")
53
- st.sidebar.header("Input Settings")
54
 
55
  # Input text from user
56
- user_text = st.sidebar.text_area("Enter the text for generating MCQs:", height=200)
57
 
58
- if st.sidebar.button("Generate MCQs"):
59
  if user_text.strip():
60
  with st.spinner("Generating MCQs..."):
61
  mcqs = generate_mcqs_from_text(user_text)
62
- st.subheader("Generated MCQs:")
63
- st.text_area("MCQs", value=mcqs, height=400)
64
- st.session_state["mcqs"] = mcqs # Store MCQs for evaluation
65
  else:
66
  st.error("Please enter text for generating MCQs.")
67
 
68
- # Input user answers for evaluation
69
- if "mcqs" in st.session_state:
70
- user_answers = st.text_area("Enter your answers (e.g., A, B, C, D for each question):", height=100)
71
- if st.button("Evaluate Answers"):
72
- if user_answers.strip():
73
- with st.spinner("Evaluating answers..."):
74
- evaluation_result = evaluate_answers(st.session_state["mcqs"], user_answers)
75
- st.subheader("Evaluation Result:")
76
- st.text_area("Evaluation", value=evaluation_result, height=400)
77
- else:
78
- st.error("Please enter your answers for evaluation.")
 
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 based on the input text
13
+ # def generate_mcqs_from_text(user_text):
14
+ # prompt = f"""
15
+ # Based on the following text, generate between 30 to 50 multiple-choice questions.
16
+ # Each question should have four answer options, with one correct answer and three distractors.
17
+ # Make sure the questions are clear and test the user's understanding of the content.
18
+ # Provide the questions in the following format:
19
+
20
+ # Question: [Your question here]
21
+ # A. [Option 1]
22
+ # B. [Option 2]
23
+ # C. [Option 3]
24
+ # D. [Option 4]
25
+ # Correct Answer: [Correct Option]
26
+
27
+ # Text: {user_text}
28
+ # """
29
+ # chat_completion = client.chat.completions.create(
30
+ # messages=[{"role": "user", "content": prompt}],
31
+ # model="gemma2-9b-it", # Use a valid model that supports chat completions
32
+ # )
33
+ # return chat_completion.choices[0].message.content
34
+
35
+ # # Function to evaluate user answers
36
+ # def evaluate_answers(mcqs, user_answers):
37
+ # prompt = f"""
38
+ # Here are the user's answers to the following multiple-choice questions.
39
+ # Please evaluate them as an experienced teacher, providing feedback on correctness,
40
+ # and explain why each answer is right or wrong.
41
+
42
+ # MCQs: {mcqs}
43
+ # User Answers: {user_answers}
44
+ # """
45
+ # chat_completion = client.chat.completions.create(
46
+ # messages=[{"role": "user", "content": prompt}],
47
+ # model="gemma2-9b-it", # Use a valid model
48
+ # )
49
+ # return chat_completion.choices[0].message.content
50
+
51
+ # # Streamlit App
52
+ # st.title("Study Assistant - MCQ Generator & Evaluator")
53
+ # st.sidebar.header("Input Settings")
54
+
55
+ # # Input text from user
56
+ # user_text = st.sidebar.text_area("Enter the text for generating MCQs:", height=200)
57
+
58
+ # if st.sidebar.button("Generate MCQs"):
59
+ # if user_text.strip():
60
+ # with st.spinner("Generating MCQs..."):
61
+ # mcqs = generate_mcqs_from_text(user_text)
62
+ # st.subheader("Generated MCQs:")
63
+ # st.text_area("MCQs", value=mcqs, height=400)
64
+ # st.session_state["mcqs"] = mcqs # Store MCQs for evaluation
65
+ # else:
66
+ # st.error("Please enter text for generating MCQs.")
67
+
68
+ # # Input user answers for evaluation
69
+ # if "mcqs" in st.session_state:
70
+ # user_answers = st.text_area("Enter your answers (e.g., A, B, C, D for each question):", height=100)
71
+ # if st.button("Evaluate Answers"):
72
+ # if user_answers.strip():
73
+ # with st.spinner("Evaluating answers..."):
74
+ # evaluation_result = evaluate_answers(st.session_state["mcqs"], user_answers)
75
+ # st.subheader("Evaluation Result:")
76
+ # st.text_area("Evaluation", value=evaluation_result, height=400)
77
+ # else:
78
+ # st.error("Please enter your answers for evaluation.")
79
+
80
+
81
  import os
82
  from dotenv import load_dotenv
83
  from groq import Groq
 
89
  # Initialize the Groq client with API key
90
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
91
 
92
+ # Function to generate MCQs as a 35-year experienced educator
93
  def generate_mcqs_from_text(user_text):
94
  prompt = f"""
95
+ You are a 35-year experienced educator specializing in crafting challenging and insightful MCQs.
96
+ Based on the following text, generate between 30 to 50 multiple-choice questions (MCQs).
97
+ Each question should:
98
+ 1. Test critical thinking and understanding of the content.
99
+ 2. Include four options (A, B, C, D), with one correct answer and three well-designed distractors.
100
+ 3. Provide clear, concise language and avoid ambiguity.
101
+ 4. Provide the correct answer with each MCQ.
102
+
103
+ Format the output as:
104
  Question: [Your question here]
105
  A. [Option 1]
106
  B. [Option 2]
 
112
  """
113
  chat_completion = client.chat.completions.create(
114
  messages=[{"role": "user", "content": prompt}],
115
+ model="gemma2-9b-it", # Use a valid Groq-supported model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  )
117
  return chat_completion.choices[0].message.content
118
 
119
  # Streamlit App
120
+ st.title("MCQ Generator with Correct Answers")
121
+ st.write("Paste your text below, and the app will generate MCQs with the correct answers.")
122
 
123
  # Input text from user
124
+ user_text = st.text_area("Enter the text for generating MCQs:", height=200)
125
 
126
+ if st.button("Generate MCQs"):
127
  if user_text.strip():
128
  with st.spinner("Generating MCQs..."):
129
  mcqs = generate_mcqs_from_text(user_text)
130
+ st.subheader("Generated MCQs with Correct Answers:")
131
+ st.text_area("MCQs", value=mcqs, height=600)
 
132
  else:
133
  st.error("Please enter text for generating MCQs.")
134
 
135
+
136
+
137
+
138
+
139
+
140
+
141
+