|
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer |
|
import gradio as gr |
|
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
|
def interview_chatbot(user_input, task): |
|
""" |
|
Handles interview-specific chatbot tasks. |
|
|
|
Parameters: |
|
- user_input: str, the input text from the user. |
|
- task: str, the type of task (e.g., "Behavioral Question", "Technical Question", "General Advice"). |
|
|
|
Returns: |
|
- str: The generated response. |
|
""" |
|
if task == "Behavioral Question": |
|
prompt = f"You are an interview coach. Provide a strong response to the following behavioral question:\n{user_input}\nSuggested Response:" |
|
elif task == "Technical Question": |
|
prompt = f"You are a technical interview expert. Answer the following technical question clearly and concisely:\nQuestion: {user_input}\nAnswer:" |
|
elif task == "General Advice": |
|
prompt = f"You are an interview expert. Provide advice for the following situation:\n{user_input}\nAdvice:" |
|
else: |
|
return "Invalid task selected." |
|
|
|
response = text_generator( |
|
prompt, |
|
max_length=200, |
|
num_return_sequences=1, |
|
pad_token_id=tokenizer.eos_token_id, |
|
temperature=0.7, |
|
top_p=0.9 |
|
)[0]["generated_text"] |
|
|
|
return response[len(prompt):].strip() |
|
|
|
def gradio_interface(user_input, task): |
|
""" |
|
Interface function for Gradio integration. |
|
""" |
|
if not user_input.strip(): |
|
return "Please enter some input." |
|
return interview_chatbot(user_input, task) |
|
with gr.Blocks(theme=gr.themes.Monochrome()) as interview_chat_ui: |
|
gr.Markdown( |
|
""" |
|
# π Interview Preparation Chatbot |
|
Welcome to your personal interview preparation assistant! This chatbot can help you tackle: |
|
- **Behavioral Questions**: Practice with confidence. |
|
- **Technical Questions**: Get clear and concise explanations. |
|
- **General Advice**: Learn how to ace your interviews. |
|
""", |
|
elem_id="main_header", |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
gr.Markdown( |
|
"""### π― Enter your query and select the task type:""", |
|
elem_id="sub_header", |
|
) |
|
user_input = gr.Textbox( |
|
lines=5, |
|
placeholder="Enter your question or situation here...", |
|
label="Your Input", |
|
elem_id="input_box", |
|
) |
|
task = gr.Radio( |
|
["Behavioral Question", "Technical Question", "General Advice"], |
|
label="Select Task", |
|
elem_id="task_selector", |
|
) |
|
submit_button = gr.Button("β¨ Get Response", elem_id="submit_button") |
|
|
|
with gr.Column(): |
|
gr.Markdown( |
|
"""### π‘ Chatbot Response:""", |
|
elem_id="response_header", |
|
) |
|
output = gr.Textbox( |
|
lines=10, |
|
label="Response", |
|
interactive=False, |
|
elem_id="output_box", |
|
) |
|
|
|
submit_button.click(gradio_interface, inputs=[user_input, task], outputs=output) |
|
clear_button = gr.Button("π§Ή Clear All", elem_id="clear_button") |
|
clear_button.click(lambda: ("", ""), None, [user_input, output]) |
|
|
|
gr.Markdown( |
|
""" |
|
--- |
|
**Tip**: Practice regularly to build confidence and improve your interview skills! π |
|
""", |
|
elem_id="footer_text", |
|
) |
|
|
|
interview_chat_ui.launch() |