Amelia-James commited on
Commit
25ac852
·
verified ·
1 Parent(s): 1ad69d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -35
app.py CHANGED
@@ -1,56 +1,55 @@
1
- import streamlit as st
 
2
  from groq import Groq
 
 
 
 
3
 
4
- # Initialize Groq client with API key
5
- api_key = "your-groq-api-key-here"
6
- client = Groq(api_key=api_key)
7
 
8
- # Function to generate MCQs without showing answers
9
  def generate_mcqs_from_text(user_text):
10
  prompt = f"""
11
  You are a 35-year experienced educator specializing in crafting challenging and insightful MCQs.
12
  Based on the following text, generate between 30 to 50 multiple-choice questions (MCQs).
13
  Each question should:
14
  1. Test critical thinking and understanding of the content.
15
- 2. Include four options (A, B, C, D).
16
- 3. Format the output as follows:
17
 
 
18
  Question: [Your question here]
19
  A. [Option 1]
20
  B. [Option 2]
21
  C. [Option 3]
22
- D. [Option 4]
 
23
 
24
- Additionally, provide the correct answers in a separate section titled "Correct Answers" in the format:
25
- Question 1: [Correct Option]
26
- Question 2: [Correct Option], etc.
27
-
28
  Text: {user_text}
29
  """
30
  chat_completion = client.chat.completions.create(
31
  messages=[{"role": "user", "content": prompt}],
32
- model="gemma2-9b-it", # Replace with a valid Groq model
33
  )
34
  return chat_completion.choices[0].message.content
35
 
36
- # Function to evaluate user answers
37
- def evaluate_answers(mcqs, correct_answers, user_answers):
38
  prompt = f"""
39
  You are a 35-year experienced educator evaluating student answers.
40
- Here are the generated MCQs, the correct answers, and the student's responses. Please:
41
  1. Check the correctness of each answer.
42
- 2. Provide feedback for each question, explaining why the student's choice is correct or incorrect.
43
- 3. Assign an overall score and include suggestions for improvement.
44
 
45
  MCQs: {mcqs}
46
-
47
- Correct Answers: {correct_answers}
48
-
49
  User Answers: {user_answers}
50
  """
51
  chat_completion = client.chat.completions.create(
52
  messages=[{"role": "user", "content": prompt}],
53
- model="gemma2-9b-it", # Replace with a valid Groq model
54
  )
55
  return chat_completion.choices[0].message.content
56
 
@@ -64,30 +63,22 @@ user_text = st.sidebar.text_area("Enter the text for generating MCQs:", height=2
64
  if st.sidebar.button("Generate MCQs"):
65
  if user_text.strip():
66
  with st.spinner("Generating MCQs..."):
67
- mcqs_and_answers = generate_mcqs_from_text(user_text)
68
- # Split the output into MCQs and correct answers
69
- mcqs, correct_answers = mcqs_and_answers.split("Correct Answers:")
70
- st.session_state["mcqs"] = mcqs.strip()
71
- st.session_state["correct_answers"] = correct_answers.strip()
72
-
73
  st.subheader("Generated MCQs:")
74
- st.text_area("MCQs", value=st.session_state["mcqs"], height=400)
 
75
  else:
76
  st.error("Please enter text for generating MCQs.")
77
 
78
  # Input user answers for evaluation
79
- if "mcqs" in st.session_state and "correct_answers" in st.session_state:
80
  st.subheader("Solve the MCQs")
81
  user_answers = st.text_area("Enter your answers (e.g., A, B, C, D for each question):", height=100)
82
 
83
  if st.button("Evaluate Answers"):
84
  if user_answers.strip():
85
  with st.spinner("Evaluating answers..."):
86
- evaluation_result = evaluate_answers(
87
- st.session_state["mcqs"],
88
- st.session_state["correct_answers"],
89
- user_answers
90
- )
91
  st.subheader("Evaluation Result:")
92
  st.text_area("Evaluation", value=evaluation_result, height=400)
93
  else:
 
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 as a 35-year experienced educator
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), with one correct answer and three well-designed distractors.
20
+ 3. Provide clear, concise language and avoid ambiguity.
21
 
22
+ Format the output as:
23
  Question: [Your question here]
24
  A. [Option 1]
25
  B. [Option 2]
26
  C. [Option 3]
27
+ D. [Option 4]
28
+ Correct Answer: [Correct Option]
29
 
 
 
 
 
30
  Text: {user_text}
31
  """
32
  chat_completion = client.chat.completions.create(
33
  messages=[{"role": "user", "content": prompt}],
34
+ model="gemma2-9b-it", # Use a valid Groq-supported model
35
  )
36
  return chat_completion.choices[0].message.content
37
 
38
+ # Function to evaluate MCQ answers like an experienced teacher
39
+ def evaluate_answers(mcqs, user_answers):
40
  prompt = f"""
41
  You are a 35-year experienced educator evaluating student answers.
42
+ The student has completed the following MCQs. Please:
43
  1. Check the correctness of each answer.
44
+ 2. Provide detailed feedback for each question, explaining why the student's choice is correct or incorrect.
45
+ 3. Assign an overall score and include advice for improvement.
46
 
47
  MCQs: {mcqs}
 
 
 
48
  User Answers: {user_answers}
49
  """
50
  chat_completion = client.chat.completions.create(
51
  messages=[{"role": "user", "content": prompt}],
52
+ model="gemma2-9b-it", # Use a valid Groq-supported model
53
  )
54
  return chat_completion.choices[0].message.content
55
 
 
63
  if st.sidebar.button("Generate MCQs"):
64
  if user_text.strip():
65
  with st.spinner("Generating MCQs..."):
66
+ mcqs = generate_mcqs_from_text(user_text)
 
 
 
 
 
67
  st.subheader("Generated MCQs:")
68
+ st.text_area("MCQs", value=mcqs, height=400)
69
+ st.session_state["mcqs"] = mcqs # Store MCQs for evaluation
70
  else:
71
  st.error("Please enter text for generating MCQs.")
72
 
73
  # Input user answers for evaluation
74
+ if "mcqs" in st.session_state:
75
  st.subheader("Solve the MCQs")
76
  user_answers = st.text_area("Enter your answers (e.g., A, B, C, D for each question):", height=100)
77
 
78
  if st.button("Evaluate Answers"):
79
  if user_answers.strip():
80
  with st.spinner("Evaluating answers..."):
81
+ evaluation_result = evaluate_answers(st.session_state["mcqs"], user_answers)
 
 
 
 
82
  st.subheader("Evaluation Result:")
83
  st.text_area("Evaluation", value=evaluation_result, height=400)
84
  else: