Spaces:
Sleeping
Sleeping
from flask import Flask, jsonify, request | |
from flask_cors import CORS | |
import google.generativeai as genai | |
import os | |
import json | |
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 = {'current': 0} | |
user_chats = {} | |
def index(): | |
return "Hello π." | |
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 | |
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['current'] = 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 jsonify({'success': True}) | |
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}) | |
def useridping(): | |
if request.method == 'GET': | |
return jsonify(user_id_ping['current']) | |
if __name__ == '__main__': | |
app.run() |