File size: 6,711 Bytes
cf8045f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st 
from crewai import Agent, Task, Crew, LLM
from crewai_tools import SerperDevTool
from gtts import gTTS
import speech_recognition as sr
import os 

gemini_key=os.getenv("gemini_api")


serper_key=os.getenv("serper_api")

search_tool = SerperDevTool()

recognizer = sr.Recognizer()

def recognize_speech():
    with sr.Microphone() as source:
        st.info("Listening... Speak now!")
        recognizer.adjust_for_ambient_noise(source)
        try:
            audio = recognizer.listen(source, timeout=5,stream=False)
            text = recognizer.recognize_google(audio)
            return text
        except sr.UnknownValueError:
            return "Sorry, could not understand the audio."
        except sr.RequestError:
            return "Could not request results, check your internet."
def text_input():
    text= st.text_input("Enter the answer")
    return (text)
    

# Initialize LLM
llm = LLM(model="gemini/gemini-1.5-flash",
          verbose=True,
          temperature=0.5,
          api_key=gemini_key)


# Define Agents
question_agent = Agent(
    role="interviewer",
    goal="Frame {number} questions based on the {job_description}",
    verbose=False,
    memory=True,
    backstory="You need to frame interview questions based on the job description.",
    llm=llm,
    tools=[search_tool], 
    allow_delegation=True
)

generator_agent = Agent(
    role="answer generator",
    goal="frame answer to {question} based on the {job_description}.",
    verbose=False,
    memory=True,
    backstory="you are expert in answering the question based on {job_description}.",
    llm=llm,
    tools=[search_tool],
    allow_delegation=True
)

evaluation_agent = Agent(
    role="evulation",
    goal="frame answer to {question} based on the {job_description}.",
    verbose=False,
    memory=True,
    backstory="you are expert in answering the question based on {job_description}.",
    llm=llm,
    tools=[search_tool],
    allow_delegation=True
)


# Define Tasks
question_task = Task(
    description="Generate {number}  interview questions based on the  {job_description} .",
    expected_output="A list of {number} questions.",
    tools=[search_tool],
    agent=question_agent
)

generator_task = Task(
    description="frame the answer to the {question} based on the {job_description}.",
    expected_output="Correct if the answer is right; otherwise, return the correct answer.",
    tools=[search_tool],
    agent=generator_agent
)


evaluation_task = Task(
    description="frame the answer to the {question} based on the {job_description}.",
    expected_output="Correct if the answer is right; otherwise, return the correct answer.",
    tools=[search_tool],
    agent=generator_agent
)

# Define Crews
crew1 = Crew(agents=[question_agent], tasks=[question_task])
crew2 = Crew(agents=[generator_agent], tasks=[generator_task])
crew3 = Crew(agents=[evaluation_agent],tasks=[evaluation_task])

# Initialize Streamlit App
st.title("Interview Preparation Bot")

# *Reset function to clear all session variables*
def reset_session():
    st.session_state.job_description = ""
    st.session_state.number=1
    st.session_state.questions = []
    st.session_state.current_question_index = 0
    st.session_state.answers = []
    st.session_state.evaluations = []
    st.session_state.completed = False

# Initialize session state variables
if "questions" not in st.session_state:
    reset_session()

if "number" not in st.session_state:
    st.session_state.number = 1

# User Input
st.session_state.job_description = st.text_input("Enter the Topic you need Practice", value=st.session_state.job_description)
st.session_state.number = st.number_input("Enter the Number of Question you need",min_value=1,value=st.session_state.number)

# Button to Generate Questions
if st.button("Start") and st.session_state.job_description and st.session_state.number:
    result = crew1.kickoff(inputs={"job_description": st.session_state.job_description,'number':st.session_state.number})
    st.session_state.questions = [q.strip(' ```') for q in result.raw.split("\n") if q.strip()]
    
    st.session_state.current_question_index = 0
    st.session_state.answers = []
    st.session_state.evaluations = []
    st.session_state.completed = False
    
# # Display Questions One by One
if st.session_state.questions and st.session_state.current_question_index < len(st.session_state.questions):
    question = st.session_state.questions[st.session_state.current_question_index]
    st.write(f"*Question {st.session_state.current_question_index + 1}:* {question}")
    tts = gTTS(question, lang="en")
    tts.save("response.mp3")
    audio_file = open("response.mp3", "rb")
    audio_bytes = audio_file.read()
    st.audio(audio_bytes, format="audio/mp3")
    
    # User enters answer
    type=st.radio("select the type",["Answer with voice ","Answer by type"])    

    if type=="Answer with voice ":
        output=""
        
        if st.button("Start Recording"):
            output = recognize_speech()
            st.write("Transcription: ", output)
            

    elif type=="Answer by type":
        
        output=st.text_input(f"Enter your answer for Question ")
        
        
    # st.session_state.answers .append(st.session_state.current_question_index + 1)

    answer=answer = f"{st.session_state.current_question_index + 1}{output}"

        
    # answer = st.text_input(f"Enter your answer for Question {st.session_state.current_question_index + 1}")

    if st.button("Submit Answer") and answer:
        # Evaluate Answer
        result2 = crew2.kickoff(inputs={"job_description": st.session_state.job_description, "question": question, "answer": answer})
        evaluation_result = result2.raw

        # Store answer and evaluation
        st.session_state.answers.append(answer)
        st.session_state.evaluations.append(evaluation_result)

        # Move to the next question
        st.session_state.current_question_index += 1

        # If all questions are answered, mark as completed
        if st.session_state.current_question_index == len(st.session_state.questions)-1:
            st.session_state.completed = True
col1,col2=st.columns(2)
# Show Evaluations After All Answers
if st.session_state.completed:
    with col2:
        if st.button("Review The Answers"):
            st.write("### Final Evaluation:")
            for i in range(len(st.session_state.questions)-1):
                st.write(f"*Q{i+1}:* {st.session_state.questions[i]}")
                st.write(f"*Your Answer:* {st.session_state.answers[i]}")
                st.write(f"*Evaluation:* {st.session_state.evaluations[i]}")
                st.write("---")