CEC-Learning / Gemma.py
Jeff Myers II
Switched back to google/gemma-3-4b-it
8b78b70
raw
history blame
3.64 kB
from transformers import pipeline
from huggingface_hub import login
import spaces
import json
import os
__export__ = ["GemmaLLM"]
class GemmaLLM:
def __init__(self):
login(token=os.environ.get("GEMMA_TOKEN"))
# quant_config = quantization_config.BitsAndBytesConfig(
# load_in_8bit=True,
# llm_int8_threshold=6.0,
# llm_int8_has_fp16_weight=False,
# )
model_id = "google/gemma-3-4b-it"
# model_id = "google/gemma-3n-E4B-it-litert-preview"
# model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quant_config)
# tokenizer = AutoTokenizer.from_pretrained(model_id)
self.model = pipeline("text-generation", model=model_id)
@spaces.GPU
def generate(self, message) -> str:
outputs = self.model(message, max_new_tokens=1024)[0]["generated_text"]
return outputs
def _get_summary_message(self, article, num_paragraphs) -> dict:
summarize = "You are a helpful assistant. Your main task is to summarize articles. You will be given an article that you will generate a summary for. The summary should include all the key points of the article. ONLY RESPOND WITH THE SUMMARY!!!"
summary = f"Summarize the data in the following JSON into {num_paragraphs} paragraph(s) so that it is easy to read and understand:\n"
message = [{"role": "system", "content": [{"type": "text", "text": summarize}]},
{"role": "user", "content": [{"type": "text", "text": summary + json.dumps(article, indent=4)}]}]
return message
def get_summary(self, article, num_paragraphs) -> str:
message = self._get_summary_message(article, num_paragraphs)
summary = self.generate(message)
return summary[2]["content"]
def _get_questions_message(self, summary, num_questions, difficulty) -> dict:
question = f"""
You are a helpful assistant. Your main task is to generate {num_questions} multiple choice questions from an article. Respond in the following JSON structure and schema:\n\njson\n```{json.dumps(list((
dict(question=str.__name__, correct_answer=str.__name__, false_answers=[str.__name__, str.__name__, str.__name__]),
dict(question=str.__name__, correct_answer=str.__name__, false_answers=[str.__name__, str.__name__, str.__name__]),
dict(question=str.__name__, correct_answer=str.__name__, false_answers=[str.__name__, str.__name__, str.__name__]))), indent=4)}```\n\nThere should only be {num_questions} questions generated. Each question should only have 3 false answers and 1 correct answer. The correct answer should be the most relevant answer based on the context derived from the article. False answers should not contain the correct answer. False answers should contain false information but also be reasonably plausible for answering the question. ONLY RESPOND WITH RAW JSON!!!
"""
questions = f"Generate {difficulty.lower()} questions and answers from the following article:\n"
message = [{"role": "system", "content": [{"type": "text", "text": question}]},
{"role": "user", "content": [{"type": "text", "text": questions + summary}]}]
return message
def get_questions(self, summary, num_questions, difficulty) -> dict:
message = self._get_questions_message(summary, num_questions, difficulty)
questions = self.generate(message)
return json.loads(questions[2]["content"].strip("```").replace("json\n", ""))