import streamlit as st import openai import time import gspread from oauth2client.service_account import ServiceAccountCredentials # ตั้งค่า Google Sheets API scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"] creds = ServiceAccountCredentials.from_json_keyfile_name("genexam-2c8c645ecc0d.json", scope) # เปลี่ยนเป็นชื่อไฟล์ JSON ของคุณ client_gs = gspread.authorize(creds) # เปิด Google Sheets โดยใช้ชื่อของ sheet sheet = client_gs.open("GeneXam user").sheet1 # ใส่ชื่อจริงของ Google Sheet # ฟังก์ชันตรวจสอบ username ใน Google Sheets def check_user_in_sheet(username): try: users_list = sheet.col_values(1) # สมมติว่า usernames อยู่ในคอลัมน์แรก if username in users_list: return True else: return False except Exception as e: st.error(f"Error checking user: {str(e)}") return False # ฟังก์ชันอัปเดตการใช้ API ของผู้ใช้ def update_api_usage(username): try: users_list = sheet.col_values(1) row_number = users_list.index(username) + 1 # หาตำแหน่งของ user api_usage = int(sheet.cell(row_number, 2).value) # คอลัมน์ที่ 2 เป็นจำนวนการใช้ API api_usage += 1 sheet.update_cell(row_number, 2, api_usage) except Exception as e: st.error(f"Error updating API usage: {str(e)}") # รีเซ็ตการใช้ API เมื่อถึงเวลารีเซ็ตทุกวัน (ถ้าจำเป็น) def reset_api_usage_daily(): try: users_list = sheet.col_values(1) for i, user in enumerate(users_list): sheet.update_cell(i + 1, 2, 0) # รีเซ็ตคอลัมน์ที่ 2 (การใช้ API) เป็น 0 except Exception as e: st.error(f"Error resetting API usage: {str(e)}") # ตั้งค่า OpenAI API key จาก Hugging Face Secrets openai.api_key = st.secrets["OPENAI_API_KEY"] # ฟังก์ชันการสร้างข้อสอบโดยใช้ OpenAI API และมี retry logic def generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions, case_based, num_choices=None, max_retries=3): # Adjust the number of questions based on the type if question_type == "Multiple Choice": num_questions = 3 elif question_type == "Fill in the Blank": num_questions = 10 elif question_type == "True/False": num_questions = 5 # Generate 5 true/false questions else: # Open-ended num_questions = 3 # Base prompt prompt = f"Generate {num_questions} {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}. {extra_instructions}" # If case-based medical situation is selected, modify the prompt if case_based: prompt = f"Generate {num_questions} {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following medical material: {knowledge_material}. The questions should be based on case-based medical situations, such as patient scenarios. {extra_instructions}" if question_type != "Fill in the Blank": prompt += " Provide answers with short explanations." # Add specific handling for Multiple Choice and True/False if question_type == "Multiple Choice" and num_choices: prompt += f" Each multiple choice question should have {num_choices} choices." if question_type == "True/False": prompt += " Provide short explanations for each question based on the given material, without stating True or False explicitly." retries = 0 while retries < max_retries: try: response = openai.ChatCompletion.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful assistant for generating exam questions."}, {"role": "user", "content": prompt} ] ) return response.choices[0].message.content except openai.APIConnectionError: retries += 1 time.sleep(2) # Wait for 2 seconds before retrying if retries == max_retries: st.error("Failed to connect to OpenAI API after several attempts.") return None # ระบบ login if 'username' not in st.session_state: st.title("Login") username_input = st.text_input("Enter your username:") if st.button("Login"): if username_input: if check_user_in_sheet(username_input): st.session_state['username'] = username_input st.success(f"Welcome, {username_input}!") update_api_usage(username_input) else: st.warning("Username not found. Please try again.") else: st.warning("Please enter a valid username.") else: # Main App after login st.title(f"Welcome, {st.session_state['username']}! Generate your exam questions") # Input field for knowledge material (text) with 3,000-word limit knowledge_material = st.text_area("Enter knowledge material to generate exam questions:") # Word count check if len(knowledge_material.split()) > 3000: st.warning("Please limit the knowledge material to 3,000 words or fewer.") # File uploader for PDFs (limited to 5 MB) uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf") if uploaded_file is not None: if uploaded_file.size > 5 * 1024 * 1024: # 5 MB limit st.warning("File size exceeds 5 MB. Please upload a smaller file.") else: st.success("File uploaded successfully! (Text extraction not implemented yet.)") # Select question type question_type = st.selectbox("Select question type:", ["Multiple Choice", "Fill in the Blank", "Open-ended", "True/False"]) # For multiple choice, let users select the number of choices num_choices = None if question_type == "Multiple Choice": num_choices = st.selectbox("Select the number of choices for each question:", [3, 4, 5]) # Select cognitive level cognitive_level = st.selectbox("Select cognitive level:", ["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"]) # Checkbox for Case-Based Medical Situations case_based = st.checkbox("Generate case-based medical exam questions") # Extra input field for additional instructions (placed below cognitive level) extra_instructions = st.text_area("Enter additional instructions (e.g., how you want the questions to be phrased):") # Generate questions button if 'previous_questions' not in st.session_state: st.session_state['previous_questions'] = [] if st.button("Generate Questions"): if len(knowledge_material.split()) <= 3000: # Generate questions with retry logic questions = generate_questions_with_retry( knowledge_material, question_type, cognitive_level, extra_instructions, case_based, num_choices ) if questions: st.write("Generated Exam Questions:") st.write(questions) # Avoid showing repeated content in future requests st.session_state['previous_questions'].append(questions) # Option to download the questions as a text file st.download_button( label="Download Questions", data=questions, file_name='generated_questions.txt', mime='text/plain' ) else: st.warning("Please reduce the word count to 3,000 or fewer.") # Button to generate more questions based on the same material if st.button("Generate More Questions"): if len(knowledge_material.split()) <= 3000: # Regenerate new questions, trying to avoid repeated content questions = generate_questions_with_retry( knowledge_material, question_type, cognitive_level, extra_instructions, case_based, num_choices ) # Check if the new set of questions is not the same as the previous set if questions and questions not in st.session_state['previous_questions']: st.write("Generated More Exam Questions:") st.write(questions) # Append the new questions to the session state st.session_state['previous_questions'].append(questions) # Option to download the new set of questions st.download_button( label="Download More Questions", data=questions, file_name='more_generated_questions.txt', mime='text/plain' ) else: st.warning("New questions seem to overlap with the previous ones. Try adjusting the instructions.") else: st.warning("Please reduce the word count to 3,000 or fewer.")