From 14b5aa4ed7ee6271d94cba29c3a850e2f6ee76d4 Mon Sep 17 00:00:00 2001 From: ROHAN DWIVEDI Date: Tue, 26 Aug 2025 20:11:10 +0530 Subject: [PATCH 1/2] added example of github actions --- .../markdown-converter/convert_to_md.py | 67 +++++++++++++++++++ .../markdown-converter/generate-markdown.yml | 54 +++++++++++++++ .../markdown-converter/readme.md | 62 +++++++++++++++++ examples/github-actions/readme.md | 17 +++++ 4 files changed, 200 insertions(+) create mode 100644 examples/github-actions/markdown-converter/convert_to_md.py create mode 100644 examples/github-actions/markdown-converter/generate-markdown.yml create mode 100644 examples/github-actions/markdown-converter/readme.md create mode 100644 examples/github-actions/readme.md diff --git a/examples/github-actions/markdown-converter/convert_to_md.py b/examples/github-actions/markdown-converter/convert_to_md.py new file mode 100644 index 000000000..c28318117 --- /dev/null +++ b/examples/github-actions/markdown-converter/convert_to_md.py @@ -0,0 +1,67 @@ +import os +from google import genai + + +def convert_to_markdown(text_content,api_key): + + + client = genai.Client(api_key = api_key) + + + + MODEL_ID = "gemini-2.5-flash" # @param ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"] {"allow-input":true, isTemplate: true} + + + prompt = f""" + Convert the following text into well-structured Markdown format suitable for GitHub documentation. + Use appropriate headings (#, ##, ###), lists, code blocks, and bold/italic formatting. + Ensure the output is clean and directly usable in a .md code don't include (```Markdown ````). + + Text to convert: + {text_content} + """ + + try: + + response = client.models.generate_content( + model = MODEL_ID, + contents = prompt + ) + + + return response.text + except Exception as e: + print(f"An error occurred during content generation: {e}") + return "" + +def main(): + + + api_key = os.getenv("GEMINI_API_KEY") + if not api_key: + print("Error: GEMINI_API_KEY environment variable is not set.") + exit(1) + + input_file = "source.txt" # You can change this to your input file + output_file = "README.md" # You can change this to your desired output file + + + if not os.path.exists(input_file): + print(f"Error: Input file '{input_file}' not found.") + exit(1) + + with open(input_file, "r") as f: + text_content = f.read() + + markdown_content = convert_to_markdown(text_content, api_key) + + if markdown_content: + + with open(output_file, "w") as f: + f.write(markdown_content) + print(f"Successfully converted '{input_file}' to '{output_file}'.") + else: + print("Markdown conversion failed.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/github-actions/markdown-converter/generate-markdown.yml b/examples/github-actions/markdown-converter/generate-markdown.yml new file mode 100644 index 000000000..9b2d312bd --- /dev/null +++ b/examples/github-actions/markdown-converter/generate-markdown.yml @@ -0,0 +1,54 @@ + + +name: Generate Markdown from Text + + +on: + push: + paths: + - 'source.txt' + pull_request: + paths: + - 'source.txt' + + + + +permissions: + contents: write + + +jobs: + convert_to_md: + runs-on: ubuntu-latest + + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install google-genai + + + - name: Run Markdown Converter Script + run: python convert_to_md.py + env: + + GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} + + + - name: Commit and Push Changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: 'Auto-generate README.md from source.txt' + branch: main \ No newline at end of file diff --git a/examples/github-actions/markdown-converter/readme.md b/examples/github-actions/markdown-converter/readme.md new file mode 100644 index 000000000..c4b96c6e0 --- /dev/null +++ b/examples/github-actions/markdown-converter/readme.md @@ -0,0 +1,62 @@ +# AI Text to Markdown Converter GitHub Action + +This is a GitHub Action that automatically converts plain text into a well-structured Markdown document using the Gemini API. It's designed to streamline the process of creating and maintaining documentation, like `README.md` files, directly within your repository. + +## How It Works + +The workflow is simple and automated. You write your content in a plain text file, push your changes, and the GitHub Action handles the rest. + +1. **Write Content:** You write or paste your text into a designated source file (e.g., `source.txt`). +2. **Commit & Push:** You commit and push the changes to your repository. +3. **Workflow Trigger:** The GitHub Action automatically detects the change to the `source.txt` file and starts the conversion process. +4. **AI Conversion:** A Python script uses the Gemini API to intelligently convert your text into Markdown, adding headings, lists, bold text, and other formatting as needed. +5. **Auto-Commit:** The action then automatically commits the new or updated Markdown content to a destination file (e.g., `README.md`). + +## Getting Started + +To use this tool in your own repository, you need to set up two files and a secret. + +### Step 1: Add the Tool's Files + +Place the following two files in your repository: + +* **`convert_to_md.py`**: This script contains the logic for the AI conversion. +* **`.github/workflows/generate-markdown.yml`**: This YAML file defines the GitHub Action workflow. + +* **`structure of the repository`**: + +/your-repository +├── .github +│ └── workflows +│ └── generate-markdown.yml # The GitHub Action workflow file +├── convert_to_md.py # The Python script for conversion +├── source.txt # The input text file +└── README.md # The output Markdown file (created automatically) + + + +### Step 2: Add Your API Key + +To securely use the Gemini API, you must store your API key as a GitHub Secret. + +1. Go to your repository's **Settings > Secrets and variables > Actions**. +2. Click **New repository secret**. +3. Set the **Name** to `GEMINI_API_KEY`. +4. Paste your personal Gemini API key into the **Secret** field. + +### Step 3: Ensure Read and Write Permissions + +For the GitHub Action to be able to create and push a new file to your repository, you must grant it permission. + +1. Go to your repository's **Settings > Actions > General**. +2. Scroll down to the **Workflow permissions** section. +3. Select **Read and write permissions**. +4. Click **Save**. + +### Step 4: Use the Tool + +1. Create a file named `source.txt` in the root of your repository. +2. Add your plain text content to `source.txt`. +3. Commit and push your changes. + +The workflow will run automatically, and you will see a new `README.md` file created with the formatted Markdown content. \ No newline at end of file diff --git a/examples/github-actions/readme.md b/examples/github-actions/readme.md new file mode 100644 index 000000000..fe7cac803 --- /dev/null +++ b/examples/github-actions/readme.md @@ -0,0 +1,17 @@ +# GitHub Actions and the Gemini API + +This directory contains examples of using GitHub Actions to create automated workflows powered by the Gemini API. + +## What is a GitHub Action? +A GitHub Action is a custom, automated task that you can run in your repository. You can use actions to automate a wide range of tasks, from running tests and building applications to creating documentation and performing content-related tasks. They are a core part of a developer's CI/CD (Continuous Integration/Continuous Delivery) workflow. + +## How They Integrate with the Gemini API +GitHub Actions are perfect for automating AI-powered tasks. By creating a workflow that calls the Gemini API, you can: +- **Generate content:** Automatically create blog posts, documentation, or code snippets. +- **Analyze text:** Review new code, issues, or pull requests for summaries or key information. +- **Format data:** Convert unstructured data into a clean, structured format, such as the text-to-Markdown example in this directory. + +Because the workflows run on GitHub's servers, they have direct access to your repository's files and can use secrets to securely handle your API keys. This creates a powerful, hands-off automation system. + +## Examples +* [AI Text to Markdown Converter](./ai-markdown-converter/README.md) \ No newline at end of file From 64eb4b6caed9770a4982a2b1b2a0298095349715 Mon Sep 17 00:00:00 2001 From: ROHAN DWIVEDI Date: Tue, 26 Aug 2025 20:18:03 +0530 Subject: [PATCH 2/2] added liscence header --- .../markdown-converter/convert_to_md.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/examples/github-actions/markdown-converter/convert_to_md.py b/examples/github-actions/markdown-converter/convert_to_md.py index c28318117..e4702815f 100644 --- a/examples/github-actions/markdown-converter/convert_to_md.py +++ b/examples/github-actions/markdown-converter/convert_to_md.py @@ -1,3 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import os from google import genai