Update app.py
Browse files
app.py
CHANGED
|
@@ -20,9 +20,9 @@ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
| 20 |
|
| 21 |
TRANSCRIPTION_MODEL = "whisper-large-v3-turbo"
|
| 22 |
CHAT_MODEL = "llama-3.3-70b-versatile"
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
SCORING_WEIGHTS = {
|
| 27 |
"technical_skills": 0.30,
|
| 28 |
"soft_skills": 0.20,
|
|
@@ -63,158 +63,55 @@ def transcribe_video(video_file_path, output_language="English"):
|
|
| 63 |
)
|
| 64 |
transcript_text = transcription.text
|
| 65 |
confidence = 0.95
|
| 66 |
-
|
| 67 |
-
if hasattr(transcription, 'segments') and transcription.segments:
|
| 68 |
-
confidences = []
|
| 69 |
-
for segment in transcription.segments:
|
| 70 |
-
if hasattr(segment, 'confidence'):
|
| 71 |
-
confidences.append(segment.confidence)
|
| 72 |
-
elif hasattr(segment, 'avg_logprob'):
|
| 73 |
-
confidences.append(min(1.0, max(0.0, 1.0 + segment.avg_logprob)))
|
| 74 |
-
if confidences:
|
| 75 |
-
confidence = sum(confidences) / len(confidences)
|
| 76 |
-
for seg in transcription.segments:
|
| 77 |
-
segments.append({
|
| 78 |
-
"start": getattr(seg, 'start', 0),
|
| 79 |
-
"end": getattr(seg, 'end', 0),
|
| 80 |
-
"text": getattr(seg, 'text', "")
|
| 81 |
-
})
|
| 82 |
-
return {
|
| 83 |
-
"transcript": transcript_text,
|
| 84 |
-
"confidence": confidence,
|
| 85 |
-
"duration": duration,
|
| 86 |
-
"segments": segments,
|
| 87 |
-
"language": getattr(transcription, 'language', 'en')
|
| 88 |
-
}
|
| 89 |
except Exception as e:
|
| 90 |
-
error_msg =
|
| 91 |
-
print(f"Transcription error: {str(e)}\n{traceback.format_exc()}")
|
| 92 |
return {"error": error_msg}
|
| 93 |
finally:
|
| 94 |
if audio_path and os.path.exists(audio_path):
|
| 95 |
-
try:
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
# ANALYSIS MODULE
|
| 100 |
-
# ============================================================================
|
| 101 |
-
|
| 102 |
-
def get_system_prompt(language):
|
| 103 |
-
if language == "English":
|
| 104 |
-
return """You are an expert HR professional and career counselor specializing in graduate recruitment. Provide structured, JSON-formatted insights."""
|
| 105 |
-
else:
|
| 106 |
-
return """آپ گریجویٹ بھرتی میں ماہر HR پروفیشنل ہیں۔ JSON فارمیٹ میں تجزیہ فراہم کریں۔"""
|
| 107 |
-
|
| 108 |
-
def create_analysis_prompt(transcript, language):
|
| 109 |
-
if language == "English":
|
| 110 |
-
return f"""Analyze the candidate's transcript and provide JSON analysis.\n{transcript}"""
|
| 111 |
-
else:
|
| 112 |
-
return f"""امیدوار کے ٹرانسکرپٹ کا تجزیہ کریں اور JSON فارمیٹ میں تجزیہ فراہم کریں۔\n{transcript}"""
|
| 113 |
-
|
| 114 |
-
def analyze_candidate(transcript, output_language="English"):
|
| 115 |
-
try:
|
| 116 |
-
prompt = create_analysis_prompt(transcript, output_language)
|
| 117 |
-
completion = client.chat.completions.create(
|
| 118 |
-
model=CHAT_MODEL,
|
| 119 |
-
messages=[
|
| 120 |
-
{"role": "system", "content": get_system_prompt(output_language)},
|
| 121 |
-
{"role": "user", "content": prompt}
|
| 122 |
-
],
|
| 123 |
-
temperature=0.4,
|
| 124 |
-
max_completion_tokens=2048,
|
| 125 |
-
stream=False
|
| 126 |
-
)
|
| 127 |
-
response_text = completion.choices[0].message.content
|
| 128 |
-
return parse_analysis_response(response_text, output_language)
|
| 129 |
-
except Exception as e:
|
| 130 |
-
error_msg = (f"Analysis failed: {str(e)}" if output_language == "English" else f"تجزیہ ناکام: {str(e)}")
|
| 131 |
-
print(f"Analysis error: {str(e)}\n{traceback.format_exc()}")
|
| 132 |
-
return {"error": error_msg}
|
| 133 |
-
|
| 134 |
-
def parse_analysis_response(response_text, language):
|
| 135 |
-
try:
|
| 136 |
-
start_idx = response_text.find('{')
|
| 137 |
-
end_idx = response_text.rfind('}') + 1
|
| 138 |
-
if start_idx != -1 and end_idx > start_idx:
|
| 139 |
-
return json.loads(response_text[start_idx:end_idx])
|
| 140 |
-
else:
|
| 141 |
-
return {"candidate_summary": response_text}
|
| 142 |
-
except:
|
| 143 |
-
return {"candidate_summary": response_text}
|
| 144 |
|
| 145 |
# ============================================================================
|
| 146 |
-
#
|
| 147 |
# ============================================================================
|
| 148 |
|
| 149 |
-
def
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
report += f"Transcript Confidence: {confidence:.2%}\n\n"
|
| 160 |
-
report += f"Candidate Summary:\n{analysis.get('candidate_summary', 'N/A')}\n\n"
|
| 161 |
-
report += "Top 10 Job Matches:\n"
|
| 162 |
-
for job in job_matches[:10]:
|
| 163 |
-
report += f"- {job['title']} (Score: {job['score']})\n"
|
| 164 |
-
report += f"\nTranscript:\n{transcript}\n"
|
| 165 |
-
return report
|
| 166 |
-
|
| 167 |
-
# ============================================================================
|
| 168 |
-
# PROCESSING PIPELINE
|
| 169 |
-
# ============================================================================
|
| 170 |
-
|
| 171 |
-
def process_video(video_file, output_language):
|
| 172 |
-
if video_file is None:
|
| 173 |
-
return "", "", "", "", "", "Please upload a video."
|
| 174 |
-
transcription_result = transcribe_video(video_file, output_language)
|
| 175 |
-
if "error" in transcription_result:
|
| 176 |
-
return "", "", "", "", "", transcription_result["error"]
|
| 177 |
-
transcript_text = transcription_result.get("transcript", "")
|
| 178 |
-
confidence = transcription_result.get("confidence", 0)
|
| 179 |
-
analysis_result = analyze_candidate(transcript_text, output_language)
|
| 180 |
-
job_matches = match_jobs(analysis_result, output_language)
|
| 181 |
-
final_report = generate_report(transcript_text, analysis_result, job_matches, confidence, output_language)
|
| 182 |
-
return transcript_text, str(analysis_result), str(analysis_result.get('candidate_summary','')), str(job_matches), final_report, ""
|
| 183 |
-
|
| 184 |
-
# ============================================================================
|
| 185 |
-
# GRADIO UI
|
| 186 |
-
# ============================================================================
|
| 187 |
|
| 188 |
def create_ui():
|
| 189 |
with gr.Blocks(title="AI Graduate Job Matcher") as demo:
|
| 190 |
-
gr.HTML("<h1>🎓 AI Graduate Job-Matching Platform</h1><
|
| 191 |
-
video_input = gr.Video(label="Upload Video / ویڈیو اپ لوڈ کریں")
|
| 192 |
language_dropdown = gr.Dropdown(choices=["English", "Urdu / اردو"], value="English", label="Output Language / آؤٹ پٹ زبان")
|
| 193 |
-
process_btn = gr.Button("🚀 Generate
|
| 194 |
-
transcript_output = gr.Textbox(label="Transcript")
|
| 195 |
-
|
| 196 |
-
summary_output = gr.Textbox(label="Summary")
|
| 197 |
-
jobs_output = gr.Textbox(label="Job Matches")
|
| 198 |
-
report_output = gr.Textbox(label="Complete Report")
|
| 199 |
-
error_output = gr.Textbox(label="Status / حیثیت")
|
| 200 |
-
|
| 201 |
-
def run_pipeline(video, language):
|
| 202 |
-
lang = "English" if "English" in language else "Urdu"
|
| 203 |
-
return process_video(video, lang)
|
| 204 |
|
| 205 |
process_btn.click(
|
| 206 |
-
fn=
|
| 207 |
inputs=[video_input, language_dropdown],
|
| 208 |
-
outputs=[transcript_output,
|
| 209 |
)
|
|
|
|
| 210 |
return demo
|
| 211 |
|
| 212 |
# ============================================================================
|
| 213 |
-
# MAIN
|
| 214 |
# ============================================================================
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
if not os.getenv("GROQ_API_KEY"):
|
| 218 |
-
print("⚠️ GROQ_API_KEY not found!")
|
| 219 |
demo = create_ui()
|
| 220 |
-
demo.launch(server_name="0.0.0.0", server_port=7860
|
|
|
|
| 20 |
|
| 21 |
TRANSCRIPTION_MODEL = "whisper-large-v3-turbo"
|
| 22 |
CHAT_MODEL = "llama-3.3-70b-versatile"
|
| 23 |
+
MAX_VIDEO_DURATION = 120 # 2 minutes in seconds
|
| 24 |
|
| 25 |
+
# Job Matching Weights
|
|
|
|
| 26 |
SCORING_WEIGHTS = {
|
| 27 |
"technical_skills": 0.30,
|
| 28 |
"soft_skills": 0.20,
|
|
|
|
| 63 |
)
|
| 64 |
transcript_text = transcription.text
|
| 65 |
confidence = 0.95
|
| 66 |
+
return {"transcript": transcript_text, "confidence": confidence, "duration": duration}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
+
error_msg = "Transcription failed: " + str(e) if output_language=="English" else "ٹرانسکرپشن ناکام: " + str(e)
|
|
|
|
| 69 |
return {"error": error_msg}
|
| 70 |
finally:
|
| 71 |
if audio_path and os.path.exists(audio_path):
|
| 72 |
+
try:
|
| 73 |
+
os.unlink(audio_path)
|
| 74 |
+
except:
|
| 75 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# ============================================================================
|
| 78 |
+
# SIMPLE UI
|
| 79 |
# ============================================================================
|
| 80 |
|
| 81 |
+
def process_video_ui(video, language):
|
| 82 |
+
lang = "English" if "English" in language else "Urdu"
|
| 83 |
+
if not video:
|
| 84 |
+
return "", "Please upload a video file." if lang=="English" else "براہ کرم ایک ویڈیو فائل اپ لوڈ کریں۔"
|
| 85 |
+
|
| 86 |
+
result = transcribe_video(video, lang)
|
| 87 |
+
transcript = result.get("transcript", "")
|
| 88 |
+
error = result.get("error", "")
|
| 89 |
+
|
| 90 |
+
return transcript, error if error else "✅ Success / کامیاب"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
def create_ui():
|
| 93 |
with gr.Blocks(title="AI Graduate Job Matcher") as demo:
|
| 94 |
+
gr.HTML("<h1>🎓 AI Graduate Job-Matching Platform</h1><p>Developer: Najaf Ali Sharqi</p>")
|
| 95 |
+
video_input = gr.Video(label="Upload Video / ویڈیو اپ لوڈ کریں", source="upload")
|
| 96 |
language_dropdown = gr.Dropdown(choices=["English", "Urdu / اردو"], value="English", label="Output Language / آؤٹ پٹ زبان")
|
| 97 |
+
process_btn = gr.Button("🚀 Generate Transcript / ٹرانسکرپٹ بنائیں")
|
| 98 |
+
transcript_output = gr.Textbox(label="Transcript / ٹرانسکرپٹ", lines=15)
|
| 99 |
+
status_output = gr.Textbox(label="Status / حیثیت", lines=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
process_btn.click(
|
| 102 |
+
fn=process_video_ui,
|
| 103 |
inputs=[video_input, language_dropdown],
|
| 104 |
+
outputs=[transcript_output, status_output]
|
| 105 |
)
|
| 106 |
+
|
| 107 |
return demo
|
| 108 |
|
| 109 |
# ============================================================================
|
| 110 |
+
# MAIN
|
| 111 |
# ============================================================================
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
if not os.getenv("GROQ_API_KEY"):
|
| 115 |
+
print("⚠️ WARNING: GROQ_API_KEY not found!")
|
| 116 |
demo = create_ui()
|
| 117 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|