Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Could not create Blob error

Hi, I am actually trying to prompt Gemini to give me some gradings on students essay, however there is an error that states 
Could not create `Blob`, expected `Blob`, `dict` or an `Image` type(`PIL.Image.Image` or `IPython.display.Image`). Got a: <class 'list'>

Hereby attached my code, 

model = genai.GenerativeModel('gemini-1.5-pro')

def get_json_Gemini(file, prompt😞
    start_time = time.time()
    messages=[
            {"role": "system", "parts": ["You are a teaching assistant."]},
            {"role": "user", "parts": [prompt]},
        ],
    response = model.generate_content(
        contents=messages,
        generation_config=genai.types.GenerationConfig(
            temperature=0.9,
            max_output_tokens=1600,
        )
    )
    end_time = time.time()
    print(f"API call for {file} took {end_time - start_time:.2f} seconds")
    # Save the response to a JSON file
    write_text_to_file(f"tmp/{file}.json", json.dumps(response))
    tokens = response['usage']['total_tokens']
    return json.loads(response['choices'][0]['message']['content']), tokens

def grade_answer(file, student_answer, marking_scheme😞
    prompt = marking_scheme.replace("<ANSWER></ANSWER>", student_answer)
    retry = 0
    while True:
        try:
            content, tokens = get_json_Gemini(file, prompt)
            break
        except Exception as e:
            if retry < 2:
                retry += 1
                print(e)
                print("Retrying:", retry)
                continue
            return 0, "Error", 0, 0, True, 0, True
    marks = content['marks']
    comments = content['comments']
    copyFromInternet = content['copyFromInternet']
    generativeAI = content['generativeAI']
    manualReview = content['manualReview']
    return marks, comments, copyFromInternet, generativeAI, manualReview, tokens, False

def grade_answers(df_answers, marking_scheme😞
    start_time = time.time()
    for index, row in df_answers.iterrows():
        file = row["FileName"]
        print(f"Grading answer {index + 1}/{len(df_answers)}: {file}")
        answer = row["Answers"]
        marks, comments, copyFromInternet, generativeAI, manualReview, tokens, error = grade_answer(file, answer, marking_scheme)
        df_answers.loc[index, "Marks"] = marks
        df_answers.loc[index, "Comments"] = comments
        df_answers.loc[index, "CopyFromInternet"] = copyFromInternet
        df_answers.loc[index, "GenerativeAI"] = generativeAI
        df_answers.loc[index, "GeminiTokens"] = tokens
        df_answers.loc[index, "ManualReview"] = manualReview
        df_answers.loc[index, "Error"] = error
    end_time = time.time()
    print(f"Grading all answers took {end_time - start_time:.2f} seconds")
    return df_answers

marking_scheme = read_text_file("marking_scheme.txt")
marking_scheme
 
# Assuming df_answers is read from an Excel file
df_marked = grade_answers(df_answers, marking_scheme)
df_marked.to_excel("data/marks.xlsx", index=False)

print("Grading completed and results saved to data/marks.xlsx")
0 2 621
2 REPLIES 2

Hello,

Thank you for contacting Google Cloud Community!

I would suggest you do the following:

  1. Modify your get_json_Gemini function to accept a dictionary containing the essay content instead of a filename.
  2. Inside the function, extract the essay text from the dictionary before calling the model.generate_content method.

Regards,
Jai Ade

Hello,

Thank you for your engagement regarding this issue. We haven’t heard back from you regarding this issue for sometime now. Hence, I'm going to close this issue which will no longer be monitored. However, if you have any new issues, Please don’t hesitate to create a new issue. We will be happy to assist you on the same.

Regards,
Jai Ade