zhuminghui/coding-interviewer
0
1import gradio as gr2import openai3 4from test import generatePrompt, generate_response, APIKEY 5 6openai.api_key = APIKEY7 8 9 10 11 12 13def analyze(questionLink, userCode):14 prompt = generatePrompt(questionLink, userCode)15 response = generate_response(prompt)16 # try:17 # input_dict = eval(response)18 # if (type(input_dict) != dict):19 # gr.Error("invalid input! Try it again!")20 # except:21 # gr.Error("invalid input!")22 23 return eval(response)24 25infoStore = {}26 27def add_text(history, text):28 history = history + [(text, None)]29 return history, ""30 31def bot(history):32 global infoStore33 last_message = history[-1][0].lower()34 if "leetcode.com" in last_message:35 36 infoStore['questionLink'] = last_message37 response = "Great! Please paste your code for this problem."38 39 elif "return" in last_message:40 41 infoStore['userCode'] = last_message42 analysis = analyze(infoStore['questionLink'], infoStore['userCode'])43 44 infoStore['gptOutput'] = analysis45 response = "Awesome! Let me ask the first question: what's the time complexity for your answer?"46 47 elif "time" in last_message:48 response = f"Time complexity of your code: {infoStore['gptOutput']['time complexity']}, \49 because {infoStore['gptOutput']['why time complexity']} . \n \50 Second question: what's the space complexity for your answer?"51 52 elif "space" in last_message:53 response = f"Space complexity of your code: {infoStore['gptOutput']['space complexity']}, \54 because {infoStore['gptOutput']['why space complexity']} . \n \55 Now I have some follow-up questions for you: {infoStore['gptOutput']['follow-up questions']} \n"56 57 else:58 response = "**COOL!**"59 60 history[-1][1] = response61 return history62 63with gr.Blocks() as demo:64 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=500)65 66 with gr.Row():67 with gr.Column(scale=1):68 txt = gr.Textbox(69 show_label=False,70 placeholder= "Enter text and press enter",71 ).style(container=False)72 73 txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(74 bot, chatbot, chatbot75 )76 77demo.launch(debug = True)78 