YAMITEK's picture
Update app.py
cf8045f verified
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("---")