dukemsec/coding-interviewer
0
1import gradio as gr2import openai3 4 5from lib import generatePrompt, generate_response, identifyKeyInfo, GiveOverallScore6from key import APIKEY7 8openai.api_key = APIKEY9 10def analyze(questionLink, userCode):11 prompt = generatePrompt(questionLink, userCode)12 response = generate_response(prompt) 13 return eval(response)14 15infoStore = {}16 17phases = ['Ask For Leetcode Link', 'Ask For User Code', 'Ask For Time Complexity', 'Ask For Space Complexity', 18 'Ask FollowUp', 'Rate User for the Interview']19 20def add_text(history, text):21 history = history + [(text, None)]22 return history, ""23 24currentPhase = 025 26def bot(history):27 28 global phases29 30 global currentPhase31 32 global infoStore33 34 last_message = history[-1][0].lower()35 36 keyInfo = identifyKeyInfo(phases[currentPhase], last_message)37 38 if keyInfo == '400':39 response = "Sorry, I don't understand, please try again."40 41 else:42 43 if currentPhase == 0:44 infoStore['questionLink'] = keyInfo45 response = "Great! Please paste your code for this problem."46 47 elif currentPhase == 1: 48 infoStore['userCode'] = keyInfo49 analysis = analyze(infoStore['questionLink'], infoStore['userCode'])50 infoStore['gptOutput'] = analysis51 response = "Awesome! \n\52 Let me ask the first question: what's the time complexity for your answer?"53 54 elif currentPhase == 2:55 56 userTimeComplexity = keyInfo57 if userTimeComplexity.lower() == (infoStore['gptOutput']['time complexity']).lower():58 response = f"Correct! \n"59 else:60 response = f"Wrong! The correct answer is {infoStore['gptOutput']['time complexity']}, \61 because {infoStore['gptOutput']['why time complexity']} . \n "62 response += "\nSecond question: what's the space complexity for your answer?"63 64 elif currentPhase == 3:65 66 userSpaceComplexity = keyInfo67 if userSpaceComplexity.lower() == (infoStore['gptOutput']['space complexity']).lower():68 response = f"Correct! \n"69 else:70 response = f"Wrong! The correct answer is {infoStore['gptOutput']['space complexity']}, \71 because {infoStore['gptOutput']['why space complexity']} . \n "72 73 response += f"\nNow I will ask you a follow-up question: \n \74 {infoStore['gptOutput']['follow-up questions']} \n"75 76 elif currentPhase == 4:77 78 scoreFeedback = eval(GiveOverallScore(history))79 response = f"Thank you for your time! \n \80 Your overall score (out of 100) is {scoreFeedback['overall score out of 100']} \n \81 \n \82 You can check your detailed feedback below: \n \83 {scoreFeedback['feedbacks']}. \n"84 85 currentPhase += 186 87 history[-1][1] = response88 return history89 90with gr.Blocks() as demo:91 currentPhase = 092 chatbot = gr.Chatbot([(None, "Hello! I'm a coding interviewer powered by GPT. Please enter the LeetCode link for the problem you want to mock.")], elem_id="chatbot").style(height=600)93 94 with gr.Row():95 with gr.Column(scale=1):96 txt = gr.Textbox(97 show_label=False,98 placeholder= "Enter text and press enter",99 ).style(container=False)100 101 txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(102 bot, chatbot, chatbot103 )104 105demo.launch(debug = True)106 107 108 