-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
122 lines (107 loc) · 4.41 KB
/
server.py
File metadata and controls
122 lines (107 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from crypt import methods
from flask import Flask
from flask import render_template
from flask import Response, request, jsonify
import textwrap as tw
import openai
import json
import time
from dotenv import load_dotenv
import os
app = Flask(__name__)
load_dotenv()
OPENAI_KEY = os.environ.get("API_KEY")
openai.api_key = OPENAI_KEY
topics = {
"0" : "Behavioral",
"1" : "Brain-teaser",
"2" : "Communication",
"3" : "Opinion",
"4" : "Situational",
"5" : "Technical",
}
fields = {
"acc" : "Accounting",
"bio" : "Biology",
"bsn" : "Business",
"cs" : "Computer Science",
"eng" : "Engineering",
"fin" : "Finance",
"he" : "Health",
}
chosen_topic = None
chosen_field = None
prompt = None
questions = None
chosen_question = None
user_answer = None
# Helper function to ensure output in a particular format
def convertResponseToList(response):
whitespaced_list = list(map(lambda option: option.lstrip('0123456789.-) '), response.split("\n")))
result = list(filter(None, whitespaced_list))
return result
# Initial route called when page loaded via GET request
# Send the data for topics and fields to display on home page
@app.route('/')
def index():
return render_template("home.html", topics= topics,fields = fields)
# Ajax call which gets the chosen topic and field, and returns all the questions
@app.route('/generateQuestions', methods = ['GET','POST'])
def generateQuestions():
global chosen_topic
global chosen_field
global prompt
global questions
json_data = request.get_json()
chosen_topic = topics[json_data['topic_index']]
chosen_field = fields[json_data['field_index']]
prompt = f"List 5 {chosen_topic.lower()} interview questions for a role in {chosen_field.lower()} numbered 1 to 5 "
completion = openai.Completion.create(engine="text-davinci-002",
max_tokens=256,
prompt=prompt)
result = completion.choices[0].text.strip()
questions = convertResponseToList(result)
returnData = {'prompt': prompt, 'questions': questions}
return jsonify("Success"), 200
# Evaluates the user's answer to the selected question and sends the feedback and keywords
# Called again when user re-enters a new answer to incorporate the feedback
@app.route('/evaluateAnswer', methods = ['GET','POST'])
def evaluateAnswer():
grading = 'give a grade out of 5 with reasoning and useful feedback'
prompt = f"Evaluate the answer '{user_answer}' to the interview question '{chosen_question}' and {grading}."
completion = openai.Completion.create(engine="text-davinci-002",
max_tokens=256,
prompt=prompt)
result = completion.choices[0].text.strip()
chained_prompt = f"Find keywords from the interview answer '{prompt}'"
completion = openai.Completion.create(engine="text-davinci-002",
max_tokens=256,
prompt=chained_prompt)
keyword_result = completion.choices[0].text.strip()
returnData = {'prompt': prompt, 'feedback': result, 'keywords': keyword_result}
return render_template("display_feedback.html", topics = topics, fields = fields, return_data = returnData, chosen_question = chosen_question, user_answer = user_answer)
@app.route('/get-started')
def get_started():
return render_template("get_started.html", topics = topics, fields = fields)
@app.route('/generated-questions')
def generated_questions():
global prompt
global questions
return render_template("generated_questions.html", topics = topics, fields = fields, prompt = prompt, questions = questions, chosen_topic = chosen_topic, chosen_field = chosen_field)
@app.route('/store_user_data', methods = ['GET','POST'])
def store_user_data():
global chosen_question
global user_answer
json_data = request.get_json()
json_data_keys = json_data.keys()
if 'chosen_question' in json_data_keys :
chosen_question = json_data['chosen_question']
if 'user_answer' in json_data_keys:
user_answer = json_data['user_answer']
return jsonify("Success"), 200
@app.route('/answer-question')
def answer_question():
global chosen_question
return render_template("answer_question.html", topics = topics, fields = fields, chosen_question = chosen_question)
if __name__ == "__main__":
app.run(debug=True)