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|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from together import Together
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import moviepy.editor as mp
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
import tempfile
|
| 8 |
+
import base64
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
# Set up Together.ai API key
|
| 13 |
+
together_api_key = os.environ.get("TOGETHER_API_KEY",)
|
| 14 |
+
client = Together(api_key=together_api_key)
|
| 15 |
+
|
| 16 |
+
def generate_interview_transcript(role, experience, additional_details, interview_type):
|
| 17 |
+
prompt = f"""As a professional interview transcript writer, create a {interview_type} mock interview script for a {experience} {role} candidate.
|
| 18 |
+
Include interviewer and candidate responses using gender-neutral names.
|
| 19 |
+
The interview should be realistic, professional, and concise.
|
| 20 |
+
Incorporate the following additional details about the candidate: {additional_details}
|
| 21 |
+
|
| 22 |
+
Format the transcript as follows:
|
| 23 |
+
Interviewer (Alex): [Question]
|
| 24 |
+
Candidate (Sam): [Response]
|
| 25 |
+
|
| 26 |
+
Create a short but relevant interview with 3-5 question-answer pairs."""
|
| 27 |
+
|
| 28 |
+
response = client.chat.completions.create(
|
| 29 |
+
model="meta-llama/Llama-3-8b-chat-hf",
|
| 30 |
+
messages=[
|
| 31 |
+
{"role": "system", "content": "You are a professional interview transcript writer."},
|
| 32 |
+
{"role": "user", "content": prompt}
|
| 33 |
+
],
|
| 34 |
+
)
|
| 35 |
+
return response.choices[0].message.content
|
| 36 |
+
|
| 37 |
+
def generate_audio(script, lang='en'):
|
| 38 |
+
lines = script.split('\n')
|
| 39 |
+
full_text = " ".join([line.split(':', 1)[1].strip() for line in lines if ':' in line])
|
| 40 |
+
|
| 41 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp:
|
| 42 |
+
audio_file = temp.name
|
| 43 |
+
tts = gTTS(text=full_text, lang=lang, slow=False)
|
| 44 |
+
tts.save(audio_file)
|
| 45 |
+
return audio_file
|
| 46 |
+
|
| 47 |
+
def generate_image(prompt):
|
| 48 |
+
response = client.images.generate(
|
| 49 |
+
prompt=prompt,
|
| 50 |
+
model="stabilityai/stable-diffusion-2-1",
|
| 51 |
+
steps=30,
|
| 52 |
+
width=512,
|
| 53 |
+
height=512,
|
| 54 |
+
n=1
|
| 55 |
+
)
|
| 56 |
+
image_data = base64.b64decode(response.data[0].b64_json)
|
| 57 |
+
return Image.open(BytesIO(image_data))
|
| 58 |
+
|
| 59 |
+
def create_video_with_images_and_audio(script, audio_file, interviewer_img, interviewee_img, output_video):
|
| 60 |
+
audio_clip = mp.AudioFileClip(audio_file)
|
| 61 |
+
total_audio_duration = audio_clip.duration
|
| 62 |
+
lines = [line for line in script.split('\n') if ':' in line]
|
| 63 |
+
|
| 64 |
+
clips = []
|
| 65 |
+
start_time = 0
|
| 66 |
+
|
| 67 |
+
for line in lines:
|
| 68 |
+
speaker, text = line.split(':', 1)
|
| 69 |
+
text = text.strip()
|
| 70 |
+
duration = len(text.split()) * 0.5 # Estimate duration based on word count
|
| 71 |
+
|
| 72 |
+
if 'Interviewer' in speaker:
|
| 73 |
+
img = np.array(interviewer_img)
|
| 74 |
+
else:
|
| 75 |
+
img = np.array(interviewee_img)
|
| 76 |
+
|
| 77 |
+
clip = mp.ImageClip(img).set_duration(duration)
|
| 78 |
+
clip = clip.set_start(start_time)
|
| 79 |
+
clips.append(clip)
|
| 80 |
+
|
| 81 |
+
start_time += duration
|
| 82 |
+
|
| 83 |
+
video = mp.CompositeVideoClip(clips, size=(512, 512))
|
| 84 |
+
final_clip = video.set_audio(audio_clip)
|
| 85 |
+
final_clip.write_videofile(output_video, codec='libx264', fps=24)
|
| 86 |
+
|
| 87 |
+
def create_mock_interview(role, experience, additional_details, interview_type):
|
| 88 |
+
with st.spinner("Generating interview transcript..."):
|
| 89 |
+
transcript = generate_interview_transcript(role, experience, additional_details, interview_type)
|
| 90 |
+
st.success("Interview transcript generated!")
|
| 91 |
+
st.text_area("Generated Transcript", transcript, height=200)
|
| 92 |
+
|
| 93 |
+
with st.spinner("Generating audio..."):
|
| 94 |
+
audio_file = generate_audio(transcript)
|
| 95 |
+
st.success("Audio generated!")
|
| 96 |
+
|
| 97 |
+
with st.spinner("Generating images for interviewer and interviewee..."):
|
| 98 |
+
interviewer_img = generate_image("A professional interviewer sitting in a modern office setting, facing the camera, upper body shot")
|
| 99 |
+
interviewee_img = generate_image(f"A confident{experience} job candidate sitting for an interview, facing the camera, upper body shot")
|
| 100 |
+
st.success("Images generated!")
|
| 101 |
+
|
| 102 |
+
with st.spinner("Creating final video..."):
|
| 103 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp:
|
| 104 |
+
output_video = temp.name
|
| 105 |
+
create_video_with_images_and_audio(transcript, audio_file, interviewer_img, interviewee_img, output_video)
|
| 106 |
+
st.success("Video created successfully!")
|
| 107 |
+
return output_video
|
| 108 |
+
|
| 109 |
+
# Streamlit UI
|
| 110 |
+
st.set_page_config(page_title="AIInterviewCoach", page_icon="π₯", layout="wide")
|
| 111 |
+
|
| 112 |
+
st.title("π¬ AIInterviewCoach")
|
| 113 |
+
st.subheader("Generate Custom Mock Interview Videos with AI")
|
| 114 |
+
|
| 115 |
+
col1, col2 = st.columns(2)
|
| 116 |
+
with col1:
|
| 117 |
+
role = st.text_input("π¨ Job Role", "Data Analyst")
|
| 118 |
+
experience = st.selectbox("π Experience Level", ["Entry-level", "Mid-level", "Senior", "Executive"])
|
| 119 |
+
with col2:
|
| 120 |
+
interview_type = st.selectbox("π Interview Scenario", ["Standard", "Behavioral", "Technical", "Case Study"])
|
| 121 |
+
additional_details = st.text_area("β¨ Additional Details", "Proficient in SQL and Python")
|
| 122 |
+
|
| 123 |
+
if st.button("π₯ Generate Mock Interview"):
|
| 124 |
+
st.write("Generating interview based on the following parameters:")
|
| 125 |
+
st.write(f"- Role: {role}")
|
| 126 |
+
st.write(f"- Experience Level: {experience}")
|
| 127 |
+
st.write(f"- Interview Type: {interview_type}")
|
| 128 |
+
st.write(f"- Additional Details: {additional_details}")
|
| 129 |
+
|
| 130 |
+
output_video = create_mock_interview(role, experience, additional_details, interview_type)
|
| 131 |
+
st.success("π Your mock interview is ready!")
|
| 132 |
+
st.video(output_video)
|
| 133 |
+
with open(output_video, 'rb') as f:
|
| 134 |
+
st.download_button(
|
| 135 |
+
label="π₯ Download Video",
|
| 136 |
+
data=f,
|
| 137 |
+
file_name="my_mock_interview.mp4",
|
| 138 |
+
mime="video/mp4"
|
| 139 |
+
)
|
| 140 |
+
os.unlink(output_video) # Delete the temporary file
|
| 141 |
+
|
| 142 |
+
st.sidebar.title("π About AIInterviewCoach")
|
| 143 |
+
st.sidebar.info("""
|
| 144 |
+
AIInterviewCoach creates realistic mock interview videos.
|
| 145 |
+
|
| 146 |
+
π§ Features:
|
| 147 |
+
- Custom scenarios
|
| 148 |
+
- Role-specific questions
|
| 149 |
+
- AI-generated visuals
|
| 150 |
+
- Professional voice-overs
|
| 151 |
+
|
| 152 |
+
π Created by: Dhruv Tibarewal
|
| 153 |
+
""")
|
| 154 |
+
|
| 155 |
+
st.sidebar.title("π‘ Tips")
|
| 156 |
+
st.sidebar.success("""
|
| 157 |
+
- Be specific about your role
|
| 158 |
+
- Share unique skills
|
| 159 |
+
- Try different scenarios
|
| 160 |
+
- Practice, iterate, succeed! π
|
| 161 |
+
""")
|