import os import openai import gradio as gr OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") ############################ def generate_ai_text(prompt,temperature): query = prompt response = openai.ChatCompletion.create( model="gpt-4o", messages=[ { "role": "system", "content": query }, { "role": "user", "content": " " }, ], temperature=temperature, max_tokens=3000, top_p=1, frequency_penalty=0, presence_penalty=0 ) text = response["choices"][0]["message"]["content"] return text def generate_prompt(risk_category,ini_prompt,no_login,low_activity,repeated_course,low_marks,WAM,forum_count,pre_fail,this_fail): body = ini_prompt data = f"The student is predicted to be '{risk_category}'\n Here are the indicators for the student:\n No Moodle Login: {no_login}\n Low Moodle Activity: {low_activity}\n Student is repeating this course: {repeated_course}\n Student has low marks: {low_marks}\n Student's WAM across all courses: {WAM}\n Student's forum activity: {forum_count}\n Count of student's previous fail of all courses: {pre_fail}\n Count of student's previous fail of this courses: {this_fail}\n " text = body + "\n\n" + data return text ############################ title = "Explainer v0.1" with gr.Blocks(title=title) as demo: gr.Markdown(""" # Explainer v0.2 Generate explanation. Select the conditions for a dummy student. Put your prompt in the text input window. """) with gr.Tab("Features"): with gr.Row(): with gr.Column(): risk_category = gr.Radio(["May need urgent support", "May fall behind","On track"], label="Prediction Status", value="May need urgent support", type="value" ) no_login = gr.Radio(["Yes", "No"], label="No Moodle login in last 7 days", value="Yes", type="value" ) low_activity = gr.Radio(["Yes", "No"], label="Low Moodle activity", value="Yes", type="value" ) repeated_course = gr.Radio(["Yes", "No"], label="Repeated Course", value="No", type="value" ) low_marks = gr.Radio(["Yes", "No"], label="Low marks", value="No", type="value" ) WAM = gr.Radio(["Low","Medium", "High"], label="WAM", value="Low", type="value" ) forum_count = gr.Radio(["Low","Medium", "High"], label="Moodle Forum Action", value="Low", type="value" ) with gr.Row(): pre_fail = gr.Slider(0, 10, value=0, step=1, label="Previous Fail (All Course)", info="Choose between 0 and 10") with gr.Row(): this_fail = gr.Slider(0, 10, value=0, step=1, label="Previous Fail (This Course)", info="Choose between 0 and 10") with gr.Column(): ini_prompt = gr.Textbox(label="initial prompt", info="Initial prompt", lines=15, value=" ", ) gr.Examples(["A machine learning prediction model was developed to predict student's academic performance for a course. A list of data points will be given later, please analyze and explain the potential reasons for the student's success or failure. Be concise, reply under 200 words."], inputs=[ini_prompt]) with gr.Column(): revised_prompt = gr.Textbox( label="Revised Prompt", info="Revised Prompt", lines=25, value=" ") with gr.Row(): temperature = gr.Slider(0, 2, value=0.5, step=0.1, label="LLM temperature", info="Choose between 0 and 2") prompt_button = gr.Button("Generate Revised Prompt") with gr.Column(): genai_text = gr.Textbox( label="AI-generated Text", info="AI-generated Text", lines=30, value="Depending on message content, please wait up to 30s, please scroll if message is long", ) text_ai_button = gr.Button("Generate Text") prompt_button.click(generate_prompt, inputs=[risk_category,ini_prompt,no_login,low_activity,repeated_course,low_marks,WAM,forum_count,pre_fail,this_fail], outputs=[revised_prompt]) text_ai_button.click(generate_ai_text, inputs=[revised_prompt,temperature], outputs=[genai_text], api_name="gen") demo.launch()