import os from dotenv import load_dotenv from groq import Groq import streamlit as st # Load environment variables load_dotenv() # Initialize the Groq client with API key client = Groq(api_key=os.getenv("GROQ_API_KEY")) # Function to generate MCQs as a 35-year experienced educator def generate_mcqs_from_text(user_text): prompt = f""" You are a 35-year experienced educator specializing in crafting challenging and insightful MCQs. Based on the following text, generate between 30 to 50 multiple-choice questions (MCQs). Each question should: 1. Test critical thinking and understanding of the content. 2. Include four options (A, B, C, D), with one correct answer and three well-designed distractors. 3. Provide clear, concise language and avoid ambiguity. 4. Avoid using any special characters or formatting like '*' or '#' in the output. Format the output as: Question: [Your question here] A. [Option 1] B. [Option 2] C. [Option 3] D. [Option 4] Correct Answer: [Correct Option] Text: {user_text} """ chat_completion = client.chat.completions.create( messages=[{"role": "user", "content": prompt}], model="gemma2-9b-it", # Use a valid Groq-supported model ) return chat_completion.choices[0].message.content # Function to clean the output and remove unwanted characters def clean_output(mcqs): # Remove special characters like '*' and '#' mcqs = mcqs.replace("*", "").replace("#", "") return mcqs # Streamlit App st.title("MCQ Generator with Correct Answers") st.write("Paste your text below, and the app will generate MCQs with the correct answers.") # Input text from user user_text = st.text_area("Enter the text for generating MCQs:", height=200) if st.button("Generate MCQs"): if user_text.strip(): with st.spinner("Generating MCQs..."): mcqs = generate_mcqs_from_text(user_text) cleaned_mcqs = clean_output(mcqs) # Clean the MCQs output st.subheader("Generated MCQs with Correct Answers:") st.text_area("MCQs", value=cleaned_mcqs, height=600) else: st.error("Please enter text for generating MCQs.")