File size: 9,837 Bytes
043739c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import json
from dotenv import load_dotenv
import uuid
from datetime import datetime
from groq import Groq

from constants import SYSTEM_PROMPT, INTERVIEW_INSTRUCTIONS, MOCK_INTERVIEW_PROMPT,EXPECTED_OUTPUT
from constants import JOB_MATCHING_INSTRUCTIONS,JOB_MATCHING_PROMPT,JOB_ROLES

from phi.agent import Agent
from phi.model.google import Gemini
from phi.tools.duckduckgo import DuckDuckGo

from elevenlabs.client import ElevenLabs
from elevenlabs import VoiceSettings

from gradio import (
    Blocks, Chatbot, Row, Column, Radio, Dropdown,
    Button, Audio, Textbox, State, HTML
)

load_dotenv()

# Initialize clients
eleven_client = ElevenLabs(api_key=os.getenv("ELEVEN_API_KEY"))
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))

def get_current_datetime() -> str:
    return json.dumps({
        "current_datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    })

def create_agent(mode):
    base_config = {
        "model": Gemini(id="gemini-2.0-flash-exp", api_key=os.getenv("GOOGLE_API_KEY")),
        "show_tool_calls": True,
        "tools": [DuckDuckGo(fixed_max_results=10), get_current_datetime],
        "add_history_to_messages": True,
    }
    
    if mode == "Interview Guide":
        return Agent(
            **base_config,
            system_prompt=SYSTEM_PROMPT,
            instructions=INTERVIEW_INSTRUCTIONS,
            num_history_responses=6,
            expected_output=EXPECTED_OUTPUT
        )
    elif mode == "Job Matching":
        return Agent(
            **base_config,
            system_prompt=JOB_MATCHING_PROMPT,
            instructions=JOB_MATCHING_INSTRUCTIONS,
        )
    else:
        return Agent(
            **base_config,
            system_prompt=MOCK_INTERVIEW_PROMPT,
            instructions=["Conduct a technical mock interview.", "Ask one question at a time.", "Evaluate the candidate's responses in simple concise words"],
            num_history_responses=3,
        )

def text_to_speech_file(text: str, client: ElevenLabs) -> str:
    """
    Converts text to speech using ElevenLabs API and saves as MP3.
    
    Args:
        text (str): Text to convert to speech
        client (ElevenLabs): Initialized ElevenLabs client
    
    Returns:
        str: Path to saved audio file
    """
    try:
        response = client.text_to_speech.convert(
            voice_id="pNInz6obpgDQGcFmaJgB",  # Adam voice
            optimize_streaming_latency="0",
            output_format="mp3_22050_32",
            text=text,
            model_id="eleven_turbo_v2",
            voice_settings=VoiceSettings(
                stability=0.0,
                similarity_boost=1.0,
                style=0.0,
                use_speaker_boost=True,
            ),
        )
        
        save_file_path = f"{uuid.uuid4()}.mp3"
        with open(save_file_path, "wb") as f:
            for chunk in response:
                if chunk:
                    f.write(chunk)
        
        return save_file_path
    except Exception as e:
        raise Exception(f"Text-to-speech conversion failed: {str(e)}")

def handle_mock_interview(audio_path, history, agent_state, voice_choice):
    """Handle audio for Mock Interview mode"""
    if not audio_path:
        return history, agent_state, None, ""
    
    try:
        # Transcribe audio
        with open(audio_path, "rb") as audio_file:
            transcription = groq_client.audio.transcriptions.create(
                file=("recording.wav", audio_file.read(), "audio/wav"),
                model="whisper-large-v3-turbo",
                response_format="text"
            )
        
        # Initialize agent if needed
        if "agent" not in agent_state or agent_state.get("mode") != "Mock Interview":
            agent_state["agent"] = create_agent("Mock Interview")
            agent_state["mode"] = "Mock Interview"
        
        # Get agent response
        agent = agent_state["agent"]
        response = agent.run(transcription).content
        
        try:
            # Generate audio using the improved text-to-speech function
            audio_output = text_to_speech_file(response, eleven_client)
            
            # Update history
            history.append({"role": "user", "content": f"[Audio]: {transcription}"})
            history.append({"role": "assistant", "content": response})
            
            return history, agent_state, audio_output, ""
            
        except Exception as audio_error:
            return history, agent_state, None, f"Audio generation error: {str(audio_error)}"
            
    except Exception as e:
        return history, agent_state, None, f"Error: {str(e)}"

def handle_text_input(message, history, mode, agent_state):
    """Handle text input for Interview Guide mode"""
    if not message.strip():
        return history, agent_state, "", ""
    
    if "agent" not in agent_state or agent_state.get("mode") != mode:
        agent_state["agent"] = create_agent(mode)
        agent_state["mode"] = mode
    
    agent = agent_state["agent"]
    history = history + [{"role": "user", "content": message}]
    
    try:
        # Using direct response instead of streaming
        response = agent.run(message).content
        history.append({"role": "assistant", "content": response})
        return history, agent_state, "", ""
    except Exception as e:
        return history, agent_state, "", f"Error: {str(e)}"

def handle_job_matching(role, experience, location, history, agent_state):
    """Handle job matching mode inputs"""
    # Initialize history if None
    if history is None:
        history = []
    
    # Initialize agent_state if None
    if agent_state is None:
        agent_state = {}
        
    if not all([role, experience, location]):
        return history, agent_state, "Please fill in all fields"
    
    # Initialize agent if needed
    if "agent" not in agent_state or agent_state.get("mode") != "Job Matching":
        agent_state["agent"] = create_agent("Job Matching")
        agent_state["mode"] = "Job Matching"
    
    query = f"""Find relevant jobs for:
    Role: {role}
    Experience: {experience}
    Location: {location}
    
    Please search for current job listings and provide details including:
    1. Company name
    2. Job title
    3. Location
    4. Key requirements
    5. Application link or process
    """
    
    try:
        agent = agent_state["agent"]
        history = history + [{"role": "user", "content": query}]
        response = agent.run(query).content
        history.append({"role": "assistant", "content": response})
        return history, agent_state, ""
    except Exception as e:
        return history, agent_state, f"Error: {str(e)}"
    
def clear_chat():
    return [], {}, None, ""

with Blocks(title="AI Interview Assistant") as demo:
    # State
    chat_history = State([])
    agent_state = State({})
    
    mode = Radio(
        choices=["Interview Guide", "Mock Interview", "Job Matching"],
        label="Mode",
        value="Interview Guide"
    )

    chatbot = Chatbot(label="Conversation", height=500, type="messages")
    error_msg = HTML()
    
    with Row():
        with Column(visible=True) as text_col:
            text_input = Textbox(
                label="Type your message",
                placeholder="Ask about interview preparation...",
                lines=3
            )
            submit_btn = Button("Send Message")
        
        # Audio input for Mock Interview
        with Column(visible=False) as audio_col:
            voice_select = Dropdown(
                choices=["Brian", "Rachel", "Sam"],
                value="Brian",
                label="Assistant Voice"
            )
            audio_input = Audio(
                sources=["microphone"],
                type="filepath",
                label="Record your answer"
            )
            audio_output = Audio(
                label="Assistant's Response",
                visible=True
            )
        
        with Column(visible=False) as job_col:
            role_select = Dropdown(
                choices=JOB_ROLES,
                label="Select Job Role",
                value="Software Engineer"
            )
            experience = Dropdown(
                choices=["0-2 years", "2-5 years", "5-8 years", "8+ years"],
                label="Experience Level",
                value="0-2 years"
            )
            location = Textbox(
                label="Preferred Location",
                placeholder="Enter city, state, or 'Remote'",
                lines=1
            )
            search_btn = Button("Search Jobs")
    
    # Clear button
    clear_btn = Button("Clear Chat")
    
    # Event handlers
    def update_mode(mode_value):
        return (
            Column(visible=mode_value == "Interview Guide"),
            Column(visible=mode_value == "Mock Interview"),
            Column(visible=mode_value == "Job Matching")
        )
    
    mode.change(
        update_mode,
        inputs=[mode],
        outputs=[text_col,audio_col,job_col]
    )
    
    submit_btn.click(
        handle_text_input,
        inputs=[text_input, chat_history, mode, agent_state],
        outputs=[chatbot, agent_state, text_input, error_msg]
    )
    
    audio_input.change(
        handle_mock_interview,
        inputs=[audio_input, chat_history, agent_state, voice_select],
        outputs=[chatbot, agent_state, audio_output, error_msg]
    )

    search_btn.click(
        handle_job_matching,
        inputs=[role_select, experience, location, chat_history, agent_state],
        outputs=[chatbot, agent_state, error_msg]
    )
    
    clear_btn.click(
        clear_chat,
        outputs=[chat_history, agent_state, audio_output, error_msg]
    )

if __name__ == "__main__":
    demo.launch()