Upload google_gemma-3n-E4B-it_3.py with huggingface_hub
Browse files- google_gemma-3n-E4B-it_3.py +78 -0
google_gemma-3n-E4B-it_3.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.12"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "transformers",
|
| 5 |
+
# "torch",
|
| 6 |
+
# ]
|
| 7 |
+
# ///
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
GEMMA_PATH = "google/gemma-3n-E2B-it" #@param ["google/gemma-3n-E2B-it", "google/gemma-3n-E4B-it"]
|
| 13 |
+
RESOURCE_URL_PREFIX = "https://raw.githubusercontent.com/google-gemini/gemma-cookbook/refs/heads/main/Demos/sample-data/"
|
| 14 |
+
|
| 15 |
+
from IPython.display import Audio, Image, Markdown, display
|
| 16 |
+
|
| 17 |
+
class ChatState():
|
| 18 |
+
def __init__(self, model, processor):
|
| 19 |
+
self.model = model
|
| 20 |
+
self.processor = processor
|
| 21 |
+
self.history = []
|
| 22 |
+
|
| 23 |
+
def send_message(self, message, max_tokens=256):
|
| 24 |
+
self.history.append(message)
|
| 25 |
+
|
| 26 |
+
input_ids = self.processor.apply_chat_template(
|
| 27 |
+
self.history,
|
| 28 |
+
add_generation_prompt=True,
|
| 29 |
+
tokenize=True,
|
| 30 |
+
return_dict=True,
|
| 31 |
+
return_tensors="pt",
|
| 32 |
+
)
|
| 33 |
+
input_len = input_ids["input_ids"].shape[-1]
|
| 34 |
+
|
| 35 |
+
input_ids = input_ids.to(self.model.device, dtype=model.dtype)
|
| 36 |
+
outputs = self.model.generate(
|
| 37 |
+
**input_ids,
|
| 38 |
+
max_new_tokens=max_tokens,
|
| 39 |
+
disable_compile=True
|
| 40 |
+
)
|
| 41 |
+
text = self.processor.batch_decode(
|
| 42 |
+
outputs[:, input_len:],
|
| 43 |
+
skip_special_tokens=True,
|
| 44 |
+
clean_up_tokenization_spaces=True
|
| 45 |
+
)
|
| 46 |
+
self.history.append({
|
| 47 |
+
"role": "assistant",
|
| 48 |
+
"content": [
|
| 49 |
+
{"type": "text", "text": text[0]},
|
| 50 |
+
]
|
| 51 |
+
})
|
| 52 |
+
|
| 53 |
+
# display chat
|
| 54 |
+
for item in message['content']:
|
| 55 |
+
if item['type'] == 'text':
|
| 56 |
+
formatted_prompt = "<font size='+1' color='brown'>🙋♂️<blockquote>\n" + item['text'] + "\n</blockquote></font>"
|
| 57 |
+
display(Markdown(formatted_prompt))
|
| 58 |
+
elif item['type'] == 'audio':
|
| 59 |
+
display(Audio(item['audio']))
|
| 60 |
+
elif item['type'] == 'image':
|
| 61 |
+
display(Image(item['image']))
|
| 62 |
+
|
| 63 |
+
formatted_text = "<font size='+1' color='teal'>🤖<blockquote>\n" + text[0] + "\n</blockquote></font>"
|
| 64 |
+
display(Markdown(formatted_text))
|
| 65 |
+
with open('google_gemma-3n-E4B-it_3.txt', 'w') as f:
|
| 66 |
+
f.write('Everything was good in google_gemma-3n-E4B-it_3.txt')
|
| 67 |
+
except Exception as e:
|
| 68 |
+
with open('google_gemma-3n-E4B-it_3.txt', 'w') as f:
|
| 69 |
+
import traceback
|
| 70 |
+
traceback.print_exc(file=f)
|
| 71 |
+
finally:
|
| 72 |
+
from huggingface_hub import upload_file
|
| 73 |
+
upload_file(
|
| 74 |
+
path_or_fileobj='google_gemma-3n-E4B-it_3.txt',
|
| 75 |
+
repo_id='model-metadata/custom_code_execution_files',
|
| 76 |
+
path_in_repo='google_gemma-3n-E4B-it_3.txt',
|
| 77 |
+
repo_type='dataset',
|
| 78 |
+
)
|