Spaces:
Runtime error
Runtime error
Update app.py
Browse files13 unmodified lines1415161718192011 unmodified lines323334353637383940414243444546474849507 unmodified lines585960616263644 unmodified lines697071727374757633 unmodified lines11011111211311411511611711811912012112244 unmodified lines16716816917017117217317432 unmodified lines20720820921021121221321421521613 unmodified lineslogger = logging.getLogger(__name__)# Define the model namemodel_name = "microsoft/phi-2"try:logger.info("Starting model initialization...")11 unmodified lineslogger.info("Loading tokenizer...")tokenizer = AutoTokenizer.from_pretrained(model_name,trust_remote_code=True)logger.info("Tokenizer loaded successfully")# Load modellogger.info("Loading model...")model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float16 if device == "cuda" else torch.float32,trust_remote_code=True)if device == "cuda":model = model.to(device)logger.info("Model loaded successfully")# Create pipeline7 unmodified linestemperature=0.7,top_p=0.9,repetition_penalty=1.1,device=0 if device == "cuda" else -1)logger.info("Pipeline created successfully")4 unmodified lines# Configure system messagesystem_message = """You are AQuaBot, an AI assistant aware of environmental impact.You help users with any topic while raising awareness about water consumptionin AI. Did you know that training GPT-3 consumed 5.4 million liters of water,equivalent to the daily consumption of a city of 10,000 people?"""# Constants for water consumption calculationWATER_PER_TOKEN = {33 unmodified linesinput_water_consumption = calculate_water_consumption(user_input, True)total_water_consumption += input_water_consumption# Create promptconversation_history = ""if chat_history:for message in chat_history:conversation_history += f"User: {message[0]}\nAssistant: {message[1]}\n"prompt = f"{system_message}\n\n{conversation_history}User: {user_input}\nAssistant:"logger.info("Generating model response...")outputs = model_gen(44 unmodified lines<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;"><h1 style="color: #2d333a;">AQuaBot</h1><p style="color: #4a5568;">Welcome to AQuaBot - An AI assistant that helps raise awareness about waterconsumption in language models.</p></div>""")32 unmodified lines</div><div style="border-top: 1px solid #ddd; padding-top: 15px;"><p style="color: #666; font-size: 14px;"><strong>Important note:</strong> This application uses Microsoft's Phi-2 modelinstead of GPT-3 for availability and cost reasons. However,the water consumption calculations per token (input/output) are based on theconclusions from the cited paper.</p></div></div>13 unmodified lines1415161718192011 unmodified lines323334353637383940414243444546474849505152537 unmodified lines616263646566674 unmodified lines72737475767778798033 unmodified lines11411511611711811912012112212312412512644 unmodified lines17117217317417517617717832 unmodified lines21121221321421521621721821913 unmodified lineslogger = logging.getLogger(__name__)# Define the model namemodel_name = "meta-llama/Llama-2-7b-hf"try:logger.info("Starting model initialization...")11 unmodified lineslogger.info("Loading tokenizer...")tokenizer = AutoTokenizer.from_pretrained(model_name,trust_remote_code=True,token=True # You'll need to set your HF token here)tokenizer.pad_token = tokenizer.eos_tokenlogger.info("Tokenizer loaded successfully")# Load model with device maplogger.info("Loading model...")model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float16 if device == "cuda" else torch.float32,trust_remote_code=True,token=True, # You'll need to set your HF token heredevice_map="auto",load_in_8bit=True)logger.info("Model loaded successfully")# Create pipeline7 unmodified linestemperature=0.7,top_p=0.9,repetition_penalty=1.1,device_map="auto")logger.info("Pipeline created successfully")4 unmodified lines# Configure system messagesystem_message = """You are AQuaBot, an AI assistant aware of environmental impact.You help users with any topic while raising awareness about water consumptionin AI. Did you know that training large language models like Llama 2 can consumesubstantial amounts of water due to the cooling requirements of data centers?Let's work together while being mindful of our environmental impact."""# Constants for water consumption calculationWATER_PER_TOKEN = {33 unmodified linesinput_water_consumption = calculate_water_consumption(user_input, True)total_water_consumption += input_water_consumption# Create prompt with Llama 2 chat formatconversation_history = ""if chat_history:for message in chat_history:conversation_history += f"[INST] {message[0]} [/INST] {message[1]} "prompt = f"{system_message}\n\n{conversation_history}[INST] {user_input} [/INST]"logger.info("Generating model response...")outputs = model_gen(44 unmodified lines<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;"><h1 style="color: #2d333a;">AQuaBot</h1><p style="color: #4a5568;">Welcome to AQuaBot - An AI assistant powered by Llama 2 that helps raise awarenessabout water consumption in language models.</p></div>""")32 unmodified lines</div><div style="border-top: 1px solid #ddd; padding-top: 15px;"><p style="color: #666; font-size: 14px;"><strong>Important note:</strong> This application uses Meta's Llama 2 (7B parameters) model.The water consumption calculations per token (input/output) are based on thegeneral conclusions from the cited paper about large language models.</p></div></div>
app.py+21-18