Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,89 +1,244 @@
|
|
1 |
-
from flask import Flask,
|
2 |
-
import random
|
3 |
-
import string
|
4 |
from flask_cors import CORS
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
CORS(app)
|
8 |
|
9 |
-
|
10 |
-
rooms = {}
|
11 |
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
# Generate a random 6-character room code.
|
16 |
-
return ''.join(random.choices(string.ascii_uppercase, k=6))
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
room_code = generate_room_code()
|
21 |
-
rooms[room_code] = {'players': [], 'started': False}
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
return jsonify({'error': 'Player name is required'})
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
return jsonify({'room_code': room_code})
|
32 |
|
33 |
-
@app.route('/
|
34 |
-
def
|
35 |
if request.method == 'POST':
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
if request.method == 'GET':
|
53 |
-
|
54 |
-
return jsonify({'error': 'Room not found'}), 404
|
55 |
-
|
56 |
-
ready = False
|
57 |
-
|
58 |
-
if len(rooms[room_code]['players']) == 2:
|
59 |
-
ready = True
|
60 |
-
|
61 |
-
return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started']})
|
62 |
-
|
63 |
-
|
64 |
-
@app.route('/start-room/<room_code>', methods=['POST'])
|
65 |
-
def start_room(room_code):
|
66 |
-
if room_code not in rooms:
|
67 |
-
return jsonify({'error': 'Room not found'}), 404
|
68 |
-
|
69 |
-
rooms[room_code]['started'] = True
|
70 |
-
|
71 |
-
return jsonify({'success': True})
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
@app.route('/leave-room/<room_code>/<int:player_id>', methods=['POST'])
|
76 |
-
def leave_room(room_code, player_id):
|
77 |
-
if room_code not in rooms:
|
78 |
-
return jsonify({'error': 'Room not found'}), 404
|
79 |
-
|
80 |
-
rooms[room_code]['players'] = [player for player in rooms[room_code]['players'] if player['id'] != player_id]
|
81 |
-
|
82 |
-
if not rooms[room_code]['players']:
|
83 |
-
del rooms[room_code]
|
84 |
-
|
85 |
-
return jsonify({'message': 'Player has left the room'})
|
86 |
|
87 |
|
88 |
if __name__ == '__main__':
|
89 |
-
app.run(
|
|
|
1 |
+
from flask import Flask, jsonify, request
|
|
|
|
|
2 |
from flask_cors import CORS
|
3 |
+
import google.generativeai as genai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
import json
|
7 |
|
8 |
+
load_dotenv()
|
|
|
9 |
|
10 |
+
app = Flask(__name__)
|
|
|
11 |
|
12 |
+
CORS(app)
|
13 |
|
14 |
+
GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
|
|
|
|
|
15 |
|
16 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
17 |
+
model = genai.GenerativeModel('gemini-pro')
|
|
|
|
|
18 |
|
19 |
+
user_id_ping = 0
|
20 |
+
user_chats = {}
|
|
|
21 |
|
22 |
+
@app.route('/')
|
23 |
+
def index():
|
24 |
+
return "Hello 👋."
|
25 |
|
|
|
26 |
|
27 |
+
@app.route('/tree', methods=["POST", "GET"])
|
28 |
+
def tree():
|
29 |
if request.method == 'POST':
|
30 |
+
data = request.get_json()
|
31 |
+
query = data.get('query')
|
32 |
+
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:
|
33 |
+
{
|
34 |
+
"name": "Java",
|
35 |
+
"children": [
|
36 |
+
{
|
37 |
+
"name": "Development Environment",
|
38 |
+
"children": [
|
39 |
+
{
|
40 |
+
"name": "Java Source Code",
|
41 |
+
"value": ".java files",
|
42 |
+
"description": "Human-readable code written with Java syntax."
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"name": "Java Development Kit (JDK)",
|
46 |
+
"children": [
|
47 |
+
{
|
48 |
+
"name": "Compiler",
|
49 |
+
"value": "translates to bytecode",
|
50 |
+
"description": "Transforms Java source code into bytecode instructions understood by the JVM."
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"name": "Java Class Library (JCL)",
|
54 |
+
"value": "predefined classes and functions",
|
55 |
+
"description": "Provides a collection of reusable code for common functionalities."
|
56 |
+
}
|
57 |
+
]
|
58 |
+
}
|
59 |
+
]
|
60 |
+
},
|
61 |
+
{
|
62 |
+
"name": "Execution",
|
63 |
+
"children": [
|
64 |
+
{
|
65 |
+
"name": "Java Runtime Environment (JRE)",
|
66 |
+
"children": [
|
67 |
+
{
|
68 |
+
"name": "Java Virtual Machine (JVM)",
|
69 |
+
"value": "executes bytecode",
|
70 |
+
"description": "Software program that interprets and executes bytecode instructions."
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"name": "Class Loader",
|
74 |
+
"value": "loads bytecode into memory",
|
75 |
+
"description": "Loads .class files containing bytecode into JVM memory for execution."
|
76 |
+
}
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"name": "Bytecode",
|
81 |
+
"value": ".class files (platform-independent)",
|
82 |
+
"description": "Machine-independent instructions generated by the compiler, executable on any system with JVM."
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"name": "Just-In-Time (JIT) Compilation (optional)",
|
86 |
+
"value": "improves performance by translating bytecode to machine code",
|
87 |
+
"description": "Technique that translates frequently used bytecode sections into native machine code for faster execution."
|
88 |
+
}
|
89 |
+
]
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"name": "Key Features",
|
93 |
+
"children": [
|
94 |
+
{
|
95 |
+
"name": "Object-Oriented Programming",
|
96 |
+
"value": "uses objects and classes",
|
97 |
+
"description": "Programs are structured around objects that encapsulate data and behavior."
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"name": "Platform Independent (write once, run anywhere)",
|
101 |
+
"value": "bytecode runs on any system with JVM",
|
102 |
+
"description": "Java code can be compiled once and run on any platform with a JVM installed."
|
103 |
+
},
|
104 |
+
{
|
105 |
+
"name": "Garbage Collection",
|
106 |
+
"value": "automatic memory management",
|
107 |
+
"description": "JVM automatically reclaims memory from unused objects, simplifying memory management for developers."
|
108 |
+
}
|
109 |
+
]
|
110 |
+
}
|
111 |
+
]
|
112 |
+
}
|
113 |
+
Topic is: ''' + query)
|
114 |
+
|
115 |
+
# print(response.text)
|
116 |
+
return jsonify({'success': True, 'data': response.text})
|
117 |
+
# return temp
|
118 |
+
|
119 |
+
|
120 |
+
@app.route('/interview', methods=["POST", "GET"])
|
121 |
+
def interview():
|
122 |
+
if request.method == 'POST':
|
123 |
+
data = request.get_json()
|
124 |
+
print(data)
|
125 |
+
if data.get('from') == 'client':
|
126 |
+
user_id = data.get('user_id')
|
127 |
+
request_type = data.get('type')
|
128 |
+
|
129 |
+
if request_type == 1: # Initialize Questionarrie.
|
130 |
+
chat = model.start_chat(history=[])
|
131 |
+
user_chats[user_id] = chat
|
132 |
+
user_chats[user_id].processed = False
|
133 |
+
|
134 |
+
position = data.get('position')
|
135 |
+
difficulty_level = data.get('difficulty_level')
|
136 |
+
company_name = data.get('company_name')
|
137 |
+
|
138 |
+
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.
|
139 |
+
You need to generate atmost 3 interview questions one after another.
|
140 |
+
The questions may consists of writing a small code along with text as well.
|
141 |
+
|
142 |
+
Now generate first question in following JSON format:
|
143 |
+
{
|
144 |
+
"question": "What is ...?"
|
145 |
+
}
|
146 |
+
|
147 |
+
I will respond to the question in the following JSON format:
|
148 |
+
{
|
149 |
+
"text_answer": "answer ...",
|
150 |
+
"code": "if any...."
|
151 |
+
}
|
152 |
+
|
153 |
+
Now after evaluating the answers you need to respond in the following JSON format:
|
154 |
+
{
|
155 |
+
"next_question": "What is ...?",
|
156 |
+
"text_correctness": "Test the correctness of text and return a range from 1 to 5 of correctness of text.",
|
157 |
+
"text_suggestions": "Some suggestions regarding the text_answer...."
|
158 |
+
"code_correctness": "Test the correctness of code and return a range from 1 to 5 of correctness of code",
|
159 |
+
"code_suggestions": "Any suggestions or optimizations to the code...",
|
160 |
+
}
|
161 |
+
|
162 |
+
At the end of the interview if no Questions are required then respond in the following format:
|
163 |
+
{
|
164 |
+
"text_correctness": "Test the correctness of text and return a range from 1 to 5 of correctness of text.",
|
165 |
+
"text_suggestions": "Some suggestions regarding the text_answer...."
|
166 |
+
"code_correctness": "Test the correctness of code and return a range from 1 to 5 of correctness of code",
|
167 |
+
"code_suggestions": "Any suggestions or optimizations to the code...",
|
168 |
+
"end": "No more questions. Thanks for your time!"
|
169 |
+
}
|
170 |
+
|
171 |
+
Position : '''+ position + '''
|
172 |
+
Difficullty Level : '''+ difficulty_level + '''
|
173 |
+
Company Interview : ''' + company_name)
|
174 |
+
print(response.text)
|
175 |
+
return jsonify({'success': True, 'data': response.text})
|
176 |
+
|
177 |
+
if request_type == 2:
|
178 |
+
text_data = data.get('text_data')
|
179 |
+
code = data.get('code')
|
180 |
+
chat = user_chats[user_id]
|
181 |
+
response = chat.send_message('''{"text_answer": "''' + text_data + '''", "code": "''' + code + '''"}''')
|
182 |
+
|
183 |
+
print(response.text)
|
184 |
+
|
185 |
+
json_text = json.loads(response.text)
|
186 |
+
try:
|
187 |
+
if json_text['end']:
|
188 |
+
user_id_ping = user_id
|
189 |
+
return jsonify({'success': True, 'data': response.text, 'end': True})
|
190 |
+
except Exception as e:
|
191 |
+
print(e)
|
192 |
+
|
193 |
+
return jsonify({'success': True, 'data': response.text, 'end': False})
|
194 |
+
|
195 |
+
|
196 |
+
elif data.get('from') == 'gradio':
|
197 |
+
print(data)
|
198 |
+
user_id = data.get('user_id')
|
199 |
+
user_chats[user_id].processed = True
|
200 |
+
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')}
|
201 |
+
return "Success"
|
202 |
+
|
203 |
+
|
204 |
+
@app.route('/result', methods=['POST', 'GET'])
|
205 |
+
def result():
|
206 |
+
if request.method == 'POST':
|
207 |
+
data = request.get_json()
|
208 |
+
user_id = data.get('user_id')
|
209 |
+
|
210 |
+
if user_chats[user_id].processed:
|
211 |
+
avg_text = 0
|
212 |
+
avg_code = 0
|
213 |
+
count = 0
|
214 |
+
for i, ele in enumerate(user_chats[user_id].history):
|
215 |
+
if i == 0:
|
216 |
+
continue
|
217 |
+
|
218 |
+
if ele['role'] == 'model':
|
219 |
+
temp = json.loads(ele['parts'][0]['text'])
|
220 |
+
if 'question' in temp.keys():
|
221 |
+
continue
|
222 |
+
elif 'next_question' in temp.keys() or 'end' in temp.keys():
|
223 |
+
count += 1
|
224 |
+
avg_text += temp['text_correctness']
|
225 |
+
avg_code += temp['code_correctness']
|
226 |
+
print(json.loads(ele['parts'][0]['text']), end='\n\n')
|
227 |
+
|
228 |
+
avg_text /= count
|
229 |
+
avg_code /= count
|
230 |
+
|
231 |
+
return jsonify({'success': True, 'avgText': avg_text, 'avgCode': avg_code, 'chatHistory': user_chats[user_id].history})
|
232 |
+
|
233 |
+
else:
|
234 |
+
return jsonify({'processing': True})
|
235 |
+
|
236 |
+
|
237 |
+
@app.route('/useridping', methods=['GET'])
|
238 |
+
def useridping():
|
239 |
if request.method == 'GET':
|
240 |
+
return jsonify(user_id_ping)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
|
243 |
if __name__ == '__main__':
|
244 |
+
app.run()
|