File size: 3,698 Bytes
6759283 |
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 |
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() |