Spaces:
Runtime error
Runtime error
Update app.py
Browse files11 unmodified lines1213141516171875 unmodified lines9495969798991001011021 unmodified line10410510610710810911011111211311411511611711811918 unmodified lines138139140141142143144130 unmodified lines27527627727827928028128228328411 unmodified lineslevel=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)# Get HuggingFace token from environment variablehf_token = os.environ.get('HUGGINGFACE_TOKEN')75 unmodified linesconversation_history = ""if chat_history:for message in chat_history:conversation_history += f"{message[0]} {message[1]} "prompt = f"[INST] {system_message}\n\n{conversation_history}{user_input}"logger.info("Generating model response...")outputs = model_gen(1 unmodified linemax_new_tokens=256,return_full_text=False,pad_token_id=tokenizer.eos_token_id,)logger.info("Model response generated successfully")assistant_response = outputs[0]['generated_text'].strip()# Calculate water consumption for outputoutput_water_consumption = calculate_water_consumption(assistant_response, False)total_water_consumption += output_water_consumption# Update chat history with the new formatted messageschat_history.append([user_input, assistant_response])# Prepare water consumption message18 unmodified lineserror_message = f"An error occurred: {str(e)}"chat_history.append([user_input, error_message])return chat_history, show_water# Constants for water consumption calculationWATER_PER_TOKEN = {130 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 Llama-2-7b 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>11 unmodified lines1213141516171875 unmodified lines9495969798991001011021031041051 unmodified line10710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314418 unmodified lines163164165166167168130 unmodified lines29930030130230330430530630711 unmodified lineslevel=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s')logger = logging.getLogger(_name_)# Get HuggingFace token from environment variablehf_token = os.environ.get('HUGGINGFACE_TOKEN')75 unmodified linesconversation_history = ""if chat_history:for message in chat_history:# Remove any [INST] tags from the historyuser_msg = message[0].replace("[INST]", "").replace("[/INST]", "").strip()assistant_msg = message[1].replace("[INST]", "").replace("[/INST]", "").strip()conversation_history += f"[INST] {user_msg} [/INST] {assistant_msg} "prompt = f"<s>[INST] {system_message}\n\n{conversation_history}[INST] {user_input} [/INST]"logger.info("Generating model response...")outputs = model_gen(1 unmodified linemax_new_tokens=256,return_full_text=False,pad_token_id=tokenizer.eos_token_id,do_sample=True,temperature=0.7,top_p=0.9,repetition_penalty=1.1)logger.info("Model response generated successfully")# Clean up the response by removing any [INST] tags and trimmingassistant_response = outputs[0]['generated_text'].strip()assistant_response = assistant_response.replace("[INST]", "").replace("[/INST]", "").strip()# If the response is too short, try to generate a more detailed oneif len(assistant_response.split()) < 10:prompt += "\nPlease provide a more detailed answer with context and explanation."outputs = model_gen(prompt,max_new_tokens=256,return_full_text=False,pad_token_id=tokenizer.eos_token_id,do_sample=True,temperature=0.7,top_p=0.9,repetition_penalty=1.1)assistant_response = outputs[0]['generated_text'].strip()assistant_response = assistant_response.replace("[INST]", "").replace("[/INST]", "").strip()# Calculate water consumption for outputoutput_water_consumption = calculate_water_consumption(assistant_response, False)total_water_consumption += output_water_consumption# Update chat history with the cleaned messageschat_history.append([user_input, assistant_response])# Prepare water consumption message18 unmodified lineserror_message = f"An error occurred: {str(e)}"chat_history.append([user_input, error_message])return chat_history, show_water# Constants for water consumption calculationWATER_PER_TOKEN = {130 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+32-9