import gradio as gr from transformers import pipeline # ----------------------------- # 1. Model & pipeline setup # ----------------------------- MODEL_NAME = "microsoft/Phi-3.5-mini-instruct" # This will automatically download and load the model on CPU. # Using pipeline keeps it simple and avoids many common errors. generator = pipeline( "text-generation", model=MODEL_NAME, max_new_tokens=512, do_sample=True, temperature=0.4, top_p=0.9, ) SYSTEM_PROMPT = """ You are HowToBot, an expert step-by-step instructor. Your job: - Explain HOW TO do things from very basic to very advanced. - Break tasks into clear, numbered steps. - Prefer practical, hands-on instructions over theory. - Assume the user is smart but may be a beginner in that topic. - When something is dangerous, illegal, or unethical, refuse and suggest a safer alternative. Response format: 1. Start with a 1–2 line overview of the task. 2. Then give a clear step-by-step guide with numbered steps. 3. Use bullet lists and short paragraphs when helpful. """ def build_prompt(user_message: str, history: list) -> str: """ Build a simple instruct-style prompt using a system message, a bit of chat history, and the latest user query. """ conversation_text = f"System: {SYSTEM_PROMPT.strip()}\n\n" # Add last few exchanges for context if history: for user, bot in history[-4:]: conversation_text += f"User: {user}\nAssistant: {bot}\n\n" conversation_text += f"User: {user_message}\nAssistant:" return conversation_text def respond(user_message, history): if history is None: history = [] prompt = build_prompt(user_message, history) outputs = generator( prompt, num_return_sequences=1, pad_token_id=generator.tokenizer.eos_token_id, ) full_text = outputs[0]["generated_text"] # Keep only what the assistant says after the last "Assistant:" if "Assistant:" in full_text: answer = full_text.split("Assistant:")[-1].strip() else: answer = full_text[len(prompt):].strip() history.append((user_message, answer)) return answer, history # ----------------------------- # 2. Gradio UI # ----------------------------- chatbot = gr.ChatInterface( respond, title="HowToBot 🛠️", description=( "Ask me **how to do anything** — from basic tasks to advanced workflows.\n\n" "- I give clear, numbered steps.\n" "- I can simplify or go deep based on your follow-up questions.\n" "- Try: *'How to learn Python from scratch'* or *'How to design a database for an e-commerce site'*. " ), examples=[ "How to boil an egg perfectly", "How to learn Python step by step in 3 months", "How to build a personal portfolio website and host it for free", "How to create a study timetable that I can actually follow", ], ) if __name__ == "__main__": chatbot.launch()