Skip to content

Commit b2e19fd

Browse files
committed
better way to pass files
1 parent 25be973 commit b2e19fd

File tree

4 files changed

+90
-64
lines changed

4 files changed

+90
-64
lines changed

patchwork/common/client/llm/google.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
from __future__ import annotations
22

3-
import hashlib
43
import time
54
from functools import lru_cache
65
from pathlib import Path
76

7+
import magic
88
from google import genai
9+
from google.genai import types
910
from google.genai.types import (
1011
CountTokensConfig,
1112
File,
1213
GenerateContentConfig,
1314
GenerateContentResponse,
1415
Model,
16+
Part,
1517
)
1618
from openai.types import CompletionUsage
1719
from openai.types.chat import (
@@ -59,20 +61,28 @@ def get_models(self) -> set[str]:
5961
def is_model_supported(self, model: str) -> bool:
6062
return model in self.get_models()
6163

62-
def __upload(self, file: Path | NotGiven) -> File | None:
64+
def __upload(self, file: Path | NotGiven) -> Part | File | None:
6365
if file is NotGiven:
6466
return None
6567

66-
md5_name = hashlib.md5(file.read_bytes()).hexdigest()
68+
file_bytes = file.read_bytes()
69+
70+
try:
71+
mime_type = magic.Magic(mime=True, uncompress=True).from_buffer(file_bytes)
72+
return types.Part.from_bytes(data=file_bytes, mime_type=mime_type)
73+
except Exception as e:
74+
pass
75+
76+
cleaned_name = "".join([char for char in file.name.lower() if char.isalnum()])
6777
try:
68-
file_ref = self.client.files.get(name=md5_name)
78+
file_ref = self.client.files.get(name=cleaned_name)
6979
if file_ref.error is None:
7080
return file_ref
7181
except Exception as e:
7282
pass
7383

7484
try:
75-
file_ref = self.client.files.upload(file=file, config=dict(name=md5_name))
85+
file_ref = self.client.files.upload(file=file, config=dict(name=cleaned_name))
7686
if file_ref.error is None:
7787
return file_ref
7888
except Exception as e:

patchwork/steps/SendEmail/SendEmail.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import imaplib
4-
import poplib
53
import smtplib
64
from email.mime.text import MIMEText
75

0 commit comments

Comments
 (0)