QL2023/ExplainerPlayground
0
1import os2import openai3import gradio as gr4 5OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")6 7############################8 9def generate_ai_text(prompt,temperature):10 11 query = prompt12 13 response = openai.ChatCompletion.create(14 model="gpt-4o",15 messages=[16 {17 "role": "system",18 "content": query19 },20 {21 "role": "user",22 "content": " "23 },24 ],25 temperature=temperature,26 max_tokens=3000,27 top_p=1,28 frequency_penalty=0,29 presence_penalty=030 )31 32 text = response["choices"][0]["message"]["content"]33 return text34 35def generate_prompt(risk_category,ini_prompt,no_login,low_activity,repeated_course,low_marks,WAM,forum_count,pre_fail,this_fail):36 37 body = ini_prompt38 39 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 "40 41 text = body + "\n\n" + data42 43 return text44 45############################46 47title = "Explainer v0.1"48 49with gr.Blocks(title=title) as demo:50 gr.Markdown("""51 # Explainer v0.252 Generate explanation. Select the conditions for a dummy student. Put your prompt in the text input window.53 """)54 with gr.Tab("Features"):55 with gr.Row():56 with gr.Column():57 risk_category = gr.Radio(["May need urgent support", "May fall behind","On track"],58 label="Prediction Status",59 value="May need urgent support",60 type="value"61 )62 no_login = gr.Radio(["Yes", "No"],63 label="No Moodle login in last 7 days",64 value="Yes",65 type="value"66 )67 low_activity = gr.Radio(["Yes", "No"],68 label="Low Moodle activity",69 value="Yes",70 type="value"71 )72 repeated_course = gr.Radio(["Yes", "No"],73 label="Repeated Course",74 value="No",75 type="value"76 )77 low_marks = gr.Radio(["Yes", "No"],78 label="Low marks",79 value="No",80 type="value"81 )82 WAM = gr.Radio(["Low","Medium", "High"],83 label="WAM",84 value="Low",85 type="value"86 )87 forum_count = gr.Radio(["Low","Medium", "High"],88 label="Moodle Forum Action",89 value="Low",90 type="value"91 )92 with gr.Row():93 pre_fail = gr.Slider(0, 10, value=0, step=1, label="Previous Fail (All Course)", info="Choose between 0 and 10")94 with gr.Row():95 this_fail = gr.Slider(0, 10, value=0, step=1, label="Previous Fail (This Course)", info="Choose between 0 and 10")96 with gr.Column():97 ini_prompt = gr.Textbox(label="initial prompt",98 info="Initial prompt",99 lines=15,100 value=" ",101 )102 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])103 with gr.Column():104 revised_prompt = gr.Textbox(105 label="Revised Prompt",106 info="Revised Prompt",107 lines=25,108 value=" ")109 with gr.Row():110 temperature = gr.Slider(0, 2, value=0.5, step=0.1, label="LLM temperature", info="Choose between 0 and 2")111 prompt_button = gr.Button("Generate Revised Prompt")112 with gr.Column():113 genai_text = gr.Textbox(114 label="AI-generated Text",115 info="AI-generated Text",116 lines=30,117 value="Depending on message content, please wait up to 30s, please scroll if message is long",118 )119 text_ai_button = gr.Button("Generate Text")120 121 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])122 text_ai_button.click(generate_ai_text, inputs=[revised_prompt,temperature], outputs=[genai_text], api_name="gen")123 124 125demo.launch() 