import gradio as gr from transformers import ( AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, ) import torch import time # ============================== # 1. Free Models Registry # ============================== FREE_MODELS = { "Flan-T5 Large": { "id": "google/flan-t5-large", "type": "seq2seq", "prompt_style": "plain" }, "Mistral-7B Instruct": { "id": "mistralai/Mistral-7B-Instruct-v0.2", "type": "causal", "prompt_style": "chatml" }, "TinyLlama-1.1B Chat": { "id": "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "type": "causal", "prompt_style": "tinymix" }, "Phi-2": { "id": "microsoft/phi-2", "type": "causal", "prompt_style": "plain" }, "Zephyr-7B Alpha": { "id": "HuggingFaceH4/zephyr-7b-alpha", "type": "causal", "prompt_style": "zephyr" } } model_cache = {} def format_prompt(style, job_role, question, model_name): if style == "chatml": return f"<|user|>\n[{job_role}] {question}\n<|assistant|>" elif style == "zephyr": return f"<|system|>You are a helpful assistant for {job_role} interviews.\n<|user|>{question}\n<|assistant|>" elif style == "gemma": return f"user\n[{job_role}] {question}\nmodel\n" elif style == "tinymix": return f"Interview Question for {job_role}:\n{question}\nAnswer:" else: if model_name == "Flan-T5 Large": return f"Answer the following {job_role} interview question:\n{question}" return f"[{job_role}] {question}" def load_model(model_name): if model_name in model_cache: return model_cache[model_name] model_info = FREE_MODELS[model_name] model_id = model_info["id"] model_type = model_info["type"] tokenizer = AutoTokenizer.from_pretrained(model_id) if model_type == "seq2seq": model = AutoModelForSeq2SeqLM.from_pretrained(model_id) else: model = AutoModelForCausalLM.from_pretrained(model_id) model.eval() model_cache[model_name] = (tokenizer, model, model_type) return tokenizer, model, model_type def run_model(model_name, prompt): try: tokenizer, model, model_type = load_model(model_name) inputs = tokenizer(prompt, return_tensors="pt") with torch.no_grad(): if model_type == "seq2seq": outputs = model.generate(**inputs, max_new_tokens=256) else: outputs = model.generate( **inputs, max_new_tokens=256, pad_token_id=tokenizer.eos_token_id ) return tokenizer.decode(outputs[0], skip_special_tokens=True) except Exception as e: return f"āš ļø Error: {str(e)}" def generate_response(job_role, model_choice, question, wait_time): if model_choice == "All Models": responses = {} start_time = time.time() for name in FREE_MODELS: info = FREE_MODELS[name] prompt = format_prompt(info["prompt_style"], job_role, question, name) response = run_model(name, prompt) elapsed = round(time.time() - start_time, 2) responses[name] = { "response": response, "time_taken": elapsed } sorted_responses = sorted(responses.items(), key=lambda x: x[1]['time_taken']) leaderboard = "\n\n".join([ f"šŸ† **{name}**\nā±ļø Time: {resp['time_taken']}s\nšŸ“ {resp['response']}" for name, resp in sorted_responses ]) return leaderboard else: info = FREE_MODELS[model_choice] prompt = format_prompt(info["prompt_style"], job_role, question, model_choice) return run_model(model_choice, prompt) job_roles = ["Data Science", "Machine Learning", "AI Engineering", "Software Engineering", "Testing"] with gr.Blocks(title="🧠 AI Interview Coach") as demo: with gr.Row(): with gr.Column(scale=1): job_role = gr.Dropdown(choices=job_roles, label="Select Job Role", value="Data Science") model_choice = gr.Dropdown(choices=list(FREE_MODELS.keys()) + ["All Models"], label="Select Model", value="Flan-T5 Large") question = gr.Textbox(label="Your Question", placeholder="e.g. What is data?") wait_time = gr.Textbox(label="Wait Seconds Before Grading", value="5") submit_btn = gr.Button("Submit") clear_btn = gr.Button("Clear") with gr.Column(scale=2): status = gr.Markdown("šŸ•’ Waiting for submission...") result = gr.Textbox(label="AI Coach Response / Leaderboard", lines=15, show_copy_button=True) def full_handler(job_role, model_choice, question, wait_time): status_text = "ā³ Processing, please wait..." result_text = generate_response(job_role, model_choice, question, int(wait_time)) return "āœ… Done!", result_text submit_btn.click(fn=full_handler, inputs=[job_role, model_choice, question, wait_time], outputs=[status, result]) clear_btn.click(fn=lambda: ("šŸ•’ Waiting for submission...", ""), inputs=[], outputs=[status, result]) if __name__ == "__main__": demo.launch()