A tool that uses the Gemini API to convert files into structured JSON.
Project description
Gemini Structurizer
Transform unstructured files into structured JSON using Google's Gemini API.
Gemini Structurizer is a Python library and command-line tool that leverages the power of Google's Gemini models to parse various file types (like .txt, .pdf, etc.) and extract information into a user-defined JSON schema. It's designed to be flexible and configurable, allowing users to define the processing logic through prompts and schemas.
Features
- AI-Powered Structure Extraction: Utilizes Gemini's multimodal capabilities to understand file content.
- Configurable Processing: Define extraction logic using a JSON configuration file:
- Specify the Gemini model to use.
- Provide system-level instructions.
- Craft user prompts tailored to your file .
- Define the desired output JSON schema.
- File Upload & Management: Handles uploading files to the Gemini API and cleaning them up afterwards.
- Automatic Configuration File Generation: If a configuration file is not found, a template is automatically generated to guide the user.
- Flexible Input: Process files specified via a configuration file, command-line arguments, or directly within your Python scripts (when used as a library).
- Automatic Output Naming: Output JSON files are conveniently named based on the input file.
- Skip Existing Output: Avoids reprocessing if the target JSON output file already exists (can be overridden).
- Library & CLI: Can be used шоколадный as a Python library in your projects or as a standalone command-line tool.
Installation
Prerequisites
- Python 3.9.21+
- A Google Cloud Project with the Gemini API enabled.
- An API key for the Gemini API. Set this key to the
GOOGLE_API_KEYenvironment variable.
export GOOGLE_API_KEY="YOUR_API_KEY_HERE"
From PyPI
pip install gemini-structurizer
(Replace gemini-structurizer with the actual name you publish on PyPI if different)
Usage
As a Command-Line Tool
The primary way to use the tool is by providing a configuration file.
-
Configuration File (
gemini_structurizer_config.json)If the configuration file (default:
gemini_structurizer_config.jsonin the current directory) doesn't exist, the tool will generate a template for you on its first run. You need to edit this file:{ "model_name": "gemini-2.5-flash-preview-04-17", // Or your preferred Gemini model "system_instruction": "You are an expert in extracting structured data from documents.", "user_prompt_for_file_processing": "Please analyze the content of the uploaded file '{filename}' and extract chapter information according to the provided schema. Each line in the input text file is prefixed with 'line_number:'.", "output_json_schema": { "type": "object", "properties": { "chapter_list": { "type": "array", "description": "List of chapters.", "items": { "type": "object", "properties": { "chapter_name": { "type": "string" }, "start_line": { "type": "integer" }, "end_line": { "type": "integer" }, "short_name": { "type": "string" } }, "required": ["chapter_name", "start_line", "end_line", "short_name"] } } }, "required": ["chapter_list"] }, "input_file_to_process": "path/to/your/input_file.txt" // Path to the file you want to process }
Key fields to configure:
model_name: The Gemini model to use (e.g., "gemini-1.5-pro", "gemini-1.5-flash-preview-04-17").system_instruction: High-level instructions for the AI model.user_prompt_for_file_processing: Specific instructions on how to process the uploaded file. You can use{filename}as a placeholder for the uploaded file's name.output_json_schema: The JSON schema defining the structure of the desired output.input_file_to_process: The path to the input file (e.g.,.txt,.pdf). This can be overridden by the command-line-iargument.
-
Running the Tool:
-
Using the default config file (
gemini_structurizer_config.json) in the current directory:gemini-structurize(This assumes you've set up the
entry_pointsinsetup.pyand installed the package. If running the script directly:python path/to/gemini_structurizer/processor.py) -
Specifying an input file (overrides config):
gemini-structurize -i path/to/another_file.txt
-
Specifying a custom config file:
gemini-structurize -c path/to/custom_config.json -i path/to/input_file.txt
-
Overwriting existing output:
gemini-structurize -i path/to/input_file.txt --overwrite
The output JSON file will be saved in the same directory as the input file, with the same base name but a
.jsonextension. -
As a Python Library
from gemini_structurizer import structure_file_with_gemini # Assuming __init__.py exposes this
import os
# Ensure GOOGLE_API_KEY is set in your environment
# os.environ['GOOGLE_API_KEY'] = "YOUR_API_KEY_HERE" # For testing, not recommended for production
input_document_path = "path/to/your/document.txt"
# Optional: path to a custom configuration file.
# If None, it looks for 'gemini_structurizer_config.json' in the directory of the calling script.
custom_config = "path/to/my_specific_task_config.json"
# Ensure the config file is set up correctly (model, prompts, schema)
# The 'input_file_to_process' in the config will be ignored if 'input_document_path' is provided here.
json_output_path = structure_file_with_gemini(
input_filepath=input_document_path,
custom_config_path=custom_config, # Can be None to use default config name in calling script's dir
overwrite_existing_output=False # Set to True to re-process even if output exists
)
if json_output_path:
print(f"Successfully processed file. Output at: {json_output_path}")
with open(json_output_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Now you can work with the structured 'data'
print(data)
else:
print("File processing failed.")
How it Works
- The tool loads a JSON configuration file that defines the Gemini model, system instructions, user prompt (how to process the file), and the desired output JSON schema.
- The specified input file is uploaded to the Gemini API.
- The Gemini model processes the file content based on your prompts and attempts to generate a JSON output conforming to your schema.
- The resulting JSON is saved to a file.
Configuration Details
The gemini_structurizer_config.json (or your custom config file) is crucial.
model_name: (String) The identifier for the Gemini model (e.g., "gemini-1.5-flash-preview-04-17").system_instruction: (String) A general instruction for the AI model about its role or task.user_prompt_for_file_processing: (String) This is where you tell the AI how to interpret the file and what to extract. You can use the placeholder{filename}which will be replaced by the name of the uploaded file. This prompt should guide the AI to produce data that fits youroutput_json_schema.output_json_schema: (Object) A valid JSON Schema object that defines the structure of the JSON output you expect from the AI. The Gemini API will use this schema to format its response.input_file_to_process: (String) Path to the default input file. This is overridden if an input file is provided via the-icommand-line argument or as a direct argument when using the library function.
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have suggestions or find bugs.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature). - Commit your changes (
git commit -m 'Add some AmazingFeature'). - Push to the branch (
git push origin feature/AmazingFeature). - Open a Pull Request.
License
Distributed under the MIT License. See LICENSE file for more information.
Contact
zionpi - zhanngpenng@gmail.com
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gemini_structurizer-0.1.1.tar.gz.
File metadata
- Download URL: gemini_structurizer-0.1.1.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49a4c63167663e03c014426387c7d5650d7dae74c5707decdf5798e1b2d197a9
|
|
| MD5 |
81706cb06ffb3feec4a81a1924f31f3b
|
|
| BLAKE2b-256 |
5d87abda67d2a1b9d8a6abb97d9352ead4d8038a7bdc7b173f0339e3d07bc57d
|
File details
Details for the file gemini_structurizer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: gemini_structurizer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
609fd4ff0a898f7b873fea6d0e8dd8af5867b1d7aff778c423089332a0fbac31
|
|
| MD5 |
e9a70d58b2c55fec3f25a914f98cf7ae
|
|
| BLAKE2b-256 |
661aebf7d3edac2d457c14d1b9b9d024d16d1a8651203af375be78058e5e98ae
|