taprosoft
commited on
Commit
·
e0879fe
1
Parent(s):
dbd0cbc
fix: update gemini blank key fallback
Browse files- backends/gemini.py +22 -13
backends/gemini.py
CHANGED
@@ -5,7 +5,12 @@ from google import genai
|
|
5 |
from google.genai import types
|
6 |
|
7 |
# Create a client
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
MODEL_NAME = "gemini-2.0-flash"
|
10 |
PROMPT = """
|
11 |
Convert the following document to markdown, preserving header, table and figure structure as much as possible.
|
@@ -28,16 +33,20 @@ def convert_gemini(path: str, file_name: str):
|
|
28 |
generation_config = types.GenerationConfig(
|
29 |
max_output_tokens=8192,
|
30 |
).to_json_dict()
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
# Convert the response to the pydantic model and return it
|
43 |
-
return
|
|
|
5 |
from google.genai import types
|
6 |
|
7 |
# Create a client
|
8 |
+
try:
|
9 |
+
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY", ""))
|
10 |
+
except Exception as e:
|
11 |
+
print(e)
|
12 |
+
client = None
|
13 |
+
|
14 |
MODEL_NAME = "gemini-2.0-flash"
|
15 |
PROMPT = """
|
16 |
Convert the following document to markdown, preserving header, table and figure structure as much as possible.
|
|
|
33 |
generation_config = types.GenerationConfig(
|
34 |
max_output_tokens=8192,
|
35 |
).to_json_dict()
|
36 |
+
if client:
|
37 |
+
response = client.models.generate_content(
|
38 |
+
model=MODEL_NAME,
|
39 |
+
contents=[
|
40 |
+
PROMPT,
|
41 |
+
types.Part.from_bytes(
|
42 |
+
data=Path(path).read_bytes(),
|
43 |
+
mime_type="application/pdf",
|
44 |
+
),
|
45 |
+
],
|
46 |
+
config=generation_config,
|
47 |
+
)
|
48 |
+
output = response.text
|
49 |
+
else:
|
50 |
+
output = "Error: Gemini API not available."
|
51 |
# Convert the response to the pydantic model and return it
|
52 |
+
return output, []
|