Spaces:
Sleeping
Sleeping
File size: 11,265 Bytes
f785540 b33ffc9 f785540 b33ffc9 f785540 b33ffc9 f785540 f8de470 f785540 f8de470 f785540 f8de470 f785540 f8de470 f785540 f8de470 f785540 f8de470 f785540 f8de470 f785540 f8de470 f785540 b33ffc9 f785540 |
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
from flask import Flask, jsonify, request
from flask_cors import CORS
import google.generativeai as genai
from dotenv import load_dotenv
import os
import json
load_dotenv()
app = Flask(__name__)
CORS(app)
GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-pro')
user_id_ping = 0
user_chats = {}
@app.route('/')
def index():
return "Hello π."
@app.route('/tree', methods=["POST", "GET"])
def tree():
if request.method == 'POST':
data = request.get_json()
query = data.get('query')
response = model.generate_content('''I will give you a topic and you have to generate an explanation of the topic in points in hierarchical tree structure and respond with JSON structure as follows:
{
"name": "Java",
"children": [
{
"name": "Development Environment",
"children": [
{
"name": "Java Source Code",
"value": ".java files",
"description": "Human-readable code written with Java syntax."
},
{
"name": "Java Development Kit (JDK)",
"children": [
{
"name": "Compiler",
"value": "translates to bytecode",
"description": "Transforms Java source code into bytecode instructions understood by the JVM."
},
{
"name": "Java Class Library (JCL)",
"value": "predefined classes and functions",
"description": "Provides a collection of reusable code for common functionalities."
}
]
}
]
},
{
"name": "Execution",
"children": [
{
"name": "Java Runtime Environment (JRE)",
"children": [
{
"name": "Java Virtual Machine (JVM)",
"value": "executes bytecode",
"description": "Software program that interprets and executes bytecode instructions."
},
{
"name": "Class Loader",
"value": "loads bytecode into memory",
"description": "Loads .class files containing bytecode into JVM memory for execution."
}
]
},
{
"name": "Bytecode",
"value": ".class files (platform-independent)",
"description": "Machine-independent instructions generated by the compiler, executable on any system with JVM."
},
{
"name": "Just-In-Time (JIT) Compilation (optional)",
"value": "improves performance by translating bytecode to machine code",
"description": "Technique that translates frequently used bytecode sections into native machine code for faster execution."
}
]
},
{
"name": "Key Features",
"children": [
{
"name": "Object-Oriented Programming",
"value": "uses objects and classes",
"description": "Programs are structured around objects that encapsulate data and behavior."
},
{
"name": "Platform Independent (write once, run anywhere)",
"value": "bytecode runs on any system with JVM",
"description": "Java code can be compiled once and run on any platform with a JVM installed."
},
{
"name": "Garbage Collection",
"value": "automatic memory management",
"description": "JVM automatically reclaims memory from unused objects, simplifying memory management for developers."
}
]
}
]
}
Topic is: ''' + query)
# print(response.text)
return jsonify({'success': True, 'data': response.text})
# return temp
@app.route('/interview', methods=["POST", "GET"])
def interview():
if request.method == 'POST':
data = request.get_json()
print(data)
if data.get('from') == 'client':
user_id = data.get('user_id')
request_type = data.get('type')
if request_type == 1: # Initialize Questionarrie.
chat = model.start_chat(history=[])
user_chats[user_id] = chat
user_chats[user_id].processed = False
position = data.get('position')
difficulty_level = data.get('difficulty_level')
company_name = data.get('company_name')
response = chat.send_message('''You are a Interviewer. I am providing you with the the position for which the inerview is, difficulty level of the interview to be conducted, Company name.
You need to generate atmost 3 interview questions one after another.
The questions may consists of writing a small code along with text as well.
Now generate first question in following JSON format:
{
"question": "What is ...?"
}
I will respond to the question in the following JSON format:
{
"text_answer": "answer ...",
"code": "if any...."
}
Now after evaluating the answers you need to respond in the following JSON format:
{
"next_question": "What is ...?",
"text_correctness": "Test the correctness of text and return a range from 1 to 5 of correctness of text.",
"text_suggestions": "Some suggestions regarding the text_answer...."
"code_correctness": "Test the correctness of code and return a range from 1 to 5 of correctness of code",
"code_suggestions": "Any suggestions or optimizations to the code...",
}
At the end of the interview if no Questions are required then respond in the following format:
{
"text_correctness": "Test the correctness of text and return a range from 1 to 5 of correctness of text.",
"text_suggestions": "Some suggestions regarding the text_answer...."
"code_correctness": "Test the correctness of code and return a range from 1 to 5 of correctness of code",
"code_suggestions": "Any suggestions or optimizations to the code...",
"end": "No more questions. Thanks for your time!"
}
Position : '''+ position + '''
Difficullty Level : '''+ difficulty_level + '''
Company Interview : ''' + company_name)
print(response.text)
return jsonify({'success': True, 'data': response.text})
if request_type == 2:
text_data = data.get('text_data')
code = data.get('code')
chat = user_chats[user_id]
response = chat.send_message('''{"text_answer": "''' + text_data + '''", "code": "''' + code + '''"}''')
print(response.text)
json_text = json.loads(response.text)
try:
if json_text['end']:
user_id_ping = user_id
return jsonify({'success': True, 'data': response.text, 'end': True})
except Exception as e:
print(e)
return jsonify({'success': True, 'data': response.text, 'end': False})
elif data.get('from') == 'gradio':
print(data)
user_id = data.get('user_id')
user_chats[user_id].processed = True
user_chats[user_id].results = {'total_video_emotions': data.get('total_video_emotions'), 'emotions_final': data.get('emotions_final'), 'body_language': data.get('body_language'), 'distraction_rate': data.get('distraction_rate'), 'formatted_response': data.get('formatted_response'), 'total_transcript_sentiment': data.get('total_transcript_sentiment')}
return "Success"
@app.route('/result', methods=['POST', 'GET'])
def result():
if request.method == 'POST':
data = request.get_json()
user_id = data.get('user_id')
if user_chats[user_id].processed:
avg_text = 0
avg_code = 0
count = 0
for i, ele in enumerate(user_chats[user_id].history):
if i == 0:
continue
if ele['role'] == 'model':
temp = json.loads(ele['parts'][0]['text'])
if 'question' in temp.keys():
continue
elif 'next_question' in temp.keys() or 'end' in temp.keys():
count += 1
avg_text += temp['text_correctness']
avg_code += temp['code_correctness']
print(json.loads(ele['parts'][0]['text']), end='\n\n')
avg_text /= count
avg_code /= count
return jsonify({'success': True, 'avgText': avg_text, 'avgCode': avg_code, 'chatHistory': user_chats[user_id].history})
else:
return jsonify({'processing': True})
@app.route('/useridping', methods=['GET'])
def useridping():
if request.method == 'GET':
return jsonify(user_id_ping)
if __name__ == '__main__':
app.run() |