-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
150 lines (110 loc) · 4.37 KB
/
app.py
File metadata and controls
150 lines (110 loc) · 4.37 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
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
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
from face_det import detect_faces
from prediction import predict_emotion
import subprocess
from voice import extract_audio
from speechrec import transcribe
from llama_model import generate_questions
import joblib
from emotion_confidence import calculate_emotion_score
from voice_emotion_confidence import calculate_voice_score
from evaluate_answers import calc_text_score
app = Flask(__name__)
CORS(app)
UPLOAD_FOLDER = "uploads"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
AUDIO_FOLDER = "audio"
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
def get_next_filename():
existing_files = [f for f in os.listdir(UPLOAD_FOLDER) if f.startswith("qn") and f.endswith(".webm")]
existing_numbers = [int(f[2:-5]) for f in existing_files if f[2:-5].isdigit()]
next_number = max(existing_numbers, default=0) + 1
return f"qn{next_number}.webm"
def delete_images_in_directory(directory_path):
try:
files = os.listdir(directory_path)
for file in files:
file_path = os.path.join(directory_path, file)
if os.path.isfile(file_path):
os.remove(file_path)
print("All files deleted successfully.")
except OSError:
print("Error occurred while deleting files.")
def merge_webm_to_mp4(folder, output):
video_files = sorted([f for f in os.listdir(folder) if f.endswith(".webm")])
if len(video_files) < 2:
print("Not enough .webm files to merge.")
return
list_file_path = os.path.join(folder, "file_list.txt")
with open(list_file_path, "w") as f:
for video in video_files:
f.write(f"file '{os.path.join(folder, video)}'\n")
try:
subprocess.run([
"ffmpeg", "-f", "concat", "-safe", "0",
"-i", list_file_path, "-c:v", "libx264", "-preset", "fast",
"-crf", "23", "-c:a", "aac", "-b:a", "192k",
output, "-y"
], check=True)
print(f"Merged video saved as {output}")
except subprocess.CalledProcessError as e:
print("Error during merging:", e)
finally:
os.remove(list_file_path)
@app.route("/uploadvideo", methods=["POST"])
def upload_video():
if "file" not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files["file"]
filename = get_next_filename()
file_path = os.path.join(UPLOAD_FOLDER, filename)
file.save(file_path)
try:
output_dir = detect_faces(file_path)
if output_dir is None:
return jsonify({"error": "Face detection failed"}), 500
results = predict_emotion(output_dir)
delete_images_in_directory(output_dir)
score = calculate_emotion_score()
print("Predictions:", results)
return jsonify({"message": "Success", "predictions": results, "score": score})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/results", methods=["GET"])
def process_videos():
os.makedirs(AUDIO_FOLDER, exist_ok=True)
video_files = sorted([f for f in os.listdir(UPLOAD_FOLDER) if f.endswith(".webm")])
selected_videos = video_files[:3]
processed_files = []
for video in selected_videos:
video_path = os.path.join(UPLOAD_FOLDER, video)
audio_path = os.path.join(AUDIO_FOLDER, video.replace(".webm", ".mp3"))
extract_audio(video_path, audio_path)
transcribe()
score = calculate_emotion_score()
vscore = calculate_voice_score()
tscore = calc_text_score()
processed_files.append(audio_path)
return jsonify({"Face score": score, "Voice score": vscore, "Text score": tscore})
@app.route("/question", methods=["GET"])
def gen_qn():
try:
job_role = request.args.get("job_role")
if not job_role:
return jsonify({"error": "Missing job_role"}), 400
payload = generate_questions(job_role)
return jsonify(payload)
except Exception as e:
return jsonify({"error": str(e)}), 500
# try:
# face_count = count_face(file_path)
# if face_count is None:
# return jsonify({"error": "Face counting failed"}), 500
# return jsonify({"message": "Success", "face_count": face_count})
# except Exception as e:
# return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)