Upload google_gemma-3n-E4B-it_3.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778# /// script# requires-python = ">=3.12"# dependencies = [# "transformers",# "torch",# ]# ///
try: import torch GEMMA_PATH = "google/gemma-3n-E2B-it" #@param ["google/gemma-3n-E2B-it", "google/gemma-3n-E4B-it"] RESOURCE_URL_PREFIX = "https://raw.githubusercontent.com/google-gemini/gemma-cookbook/refs/heads/main/Demos/sample-data/" from IPython.display import Audio, Image, Markdown, display class ChatState(): def __init__(self, model, processor): self.model = model self.processor = processor self.history = [] def send_message(self, message, max_tokens=256): self.history.append(message) input_ids = self.processor.apply_chat_template( self.history, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ) input_len = input_ids["input_ids"].shape[-1] input_ids = input_ids.to(self.model.device, dtype=model.dtype) outputs = self.model.generate( **input_ids, max_new_tokens=max_tokens, disable_compile=True ) text = self.processor.batch_decode( outputs[:, input_len:], skip_special_tokens=True, clean_up_tokenization_spaces=True ) self.history.append({ "role": "assistant", "content": [ {"type": "text", "text": text[0]}, ] }) # display chat for item in message['content']: if item['type'] == 'text': formatted_prompt = "<font size='+1' color='brown'>🙋♂️<blockquote>\n" + item['text'] + "\n</blockquote></font>" display(Markdown(formatted_prompt)) elif item['type'] == 'audio': display(Audio(item['audio'])) elif item['type'] == 'image': display(Image(item['image'])) formatted_text = "<font size='+1' color='teal'>🤖<blockquote>\n" + text[0] + "\n</blockquote></font>" display(Markdown(formatted_text)) with open('google_gemma-3n-E4B-it_3.txt', 'w') as f: f.write('Everything was good in google_gemma-3n-E4B-it_3.txt')except Exception as e: with open('google_gemma-3n-E4B-it_3.txt', 'w') as f: import traceback traceback.print_exc(file=f)finally: from huggingface_hub import upload_file upload_file( path_or_fileobj='google_gemma-3n-E4B-it_3.txt', repo_id='model-metadata/custom_code_execution_files', path_in_repo='google_gemma-3n-E4B-it_3.txt', repo_type='dataset', )google_gemma-3n-E4B-it_3.pyAdded+78