Update app.py
Browse files2 unmodified lines34567891 unmodified line1112131415161718192021222324252627282930313233343536375 unmodified lines434445464748495051522 unmodified linesimport torch# Load model and tokenizerMODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,1 unmodified linedevice_map="auto")# Safe and minimal response generatordef generate_response(prompt, temperature, max_tokens):try:inputs = tokenizer(prompt, return_tensors="pt").to(model.device)outputs = model.generate(**inputs,max_new_tokens=min(max_tokens, 150), # hard captemperature=temperature,do_sample=True,pad_token_id=tokenizer.eos_token_id)raw_output = tokenizer.decode(outputs[0], skip_special_tokens=True)response = raw_output[len(prompt):].strip()# Truncate to 2 lines to avoid long responsesreturn "\n".join(response.split("\n")[:2])except Exception as e:return f"❗Generation error: {str(e)}"# Gradio interfacedef launch_interface():5 unmodified linesgr.Slider(50, 512, step=10, value=200, label="Max New Tokens")],outputs=gr.Textbox(label="Response"),title="🤖 TinyLlama Chatbot (1.1B)",theme=gr.themes.Soft(),css=".footer {visibility: hidden;}").launch()if __name__ == "__main__":launch_interface()2 unmodified lines34567891 unmodified line1112131415161718192021222324252627285 unmodified lines34353637383940414243442 unmodified linesimport torch# Load model and tokenizerMODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,1 unmodified linedevice_map="auto")# Function to generate responsedef generate_response(prompt, temperature, max_tokens):inputs = tokenizer(prompt, return_tensors="pt").to(model.device)outputs = model.generate(**inputs,max_new_tokens=max_tokens,temperature=temperature,do_sample=True,pad_token_id=tokenizer.eos_token_id)response = tokenizer.decode(outputs[0], skip_special_tokens=True)return response[len(prompt):].strip()# Gradio interfacedef launch_interface():5 unmodified linesgr.Slider(50, 512, step=10, value=200, label="Max New Tokens")],outputs=gr.Textbox(label="Response"),title="🤖 Mistral Chatbot (7B)",theme=gr.themes.Soft(),css=".footer {visibility: hidden;}").launch()if __name__ == "__main__":launch_interface()
app.py+14-22