Team Ai
Apppublic

fakeboyinthehouse100/leetcode-solution-post-genai-backend

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿš€ LeetCode Solution Post Generator

AI-Powered Markdown Blog Post Generator for LeetCode Solutions

![FastAPI](https://fastapi.tiangolo.com/) ![HuggingFace](https://huggingface.co/) ![StarCoder2](https://huggingface.co/bigcode/starcoder2-7b)

๐Ÿ“– Overview

LeetCode Solution Post GenAI is an intelligent backend service that transforms your raw LeetCode solutions into beautifully formatted, comprehensive markdown blog posts. Powered by StarCoder2-7B, this API automatically generates detailed explanations, complexity analysis, and step-by-step walkthroughs for your coding solutions.

What it does:

  • โ€”๐Ÿ”„ Transforms raw code into professional blog posts
  • โ€”๐Ÿ“ Generates detailed explanations and intuition
  • โ€”๐Ÿงฎ Analyzes time and space complexity
  • โ€”๐Ÿ“š Creates step-by-step algorithm walkthroughs
  • โ€”๐ŸŽจ Formats everything in clean, readable markdown

๐ŸŽฏ Perfect For

  • โ€”๐Ÿ“ฑ Frontend developers building LeetCode blog platforms
  • โ€”๐Ÿ‘จโ€๐Ÿ’ป Competitive programmers sharing solutions
  • โ€”๐Ÿ“– Technical bloggers creating educational content
  • โ€”๐ŸŽ“ Students documenting their learning journey
  • โ€”๐Ÿข Companies building coding interview platforms

๐Ÿš€ API Endpoints

Health Check

GET /health

Check if the service is running and the AI model is loaded.

bash
curl https://your-space-name.hf.space/health

Response:

json
{
  "status": "healthy",
  "message": "Server is running",
  "model_loaded": true,
  "prompt_loaded": true
}

Generate Content

POST /generate

Transform your LeetCode solution into a professional blog post.

bash
curl -X POST https://your-space-name.hf.space/generate \
  -H "Content-Type: application/json" \
  -d '{
    "problem_id": "1",
    "solution": "function twoSum(nums, target) { return []; }"
  }'

Request Body:

FieldTypeRequiredDescription
problem_idstringโœ…LeetCode problem number
solutionstringโœ…Your solution code

Response:

json
{
  "markdown": "Generated markdown content..."
}

๐Ÿ› ๏ธ Tech Stack

  • โ€”Python 3.9 - Core runtime
  • โ€”FastAPI - Modern web framework
  • โ€”Transformers - HuggingFace model integration
  • โ€”PyTorch - Deep learning backend
  • โ€”StarCoder2-7B - Code generation AI model
  • โ€”Docker - Containerization
  • โ€”HuggingFace Spaces - Cloud hosting

๐ŸŽจ Generated Content Features

The AI generates comprehensive blog posts with:

  • โ€”๐ŸŽฏ Catchy titles with emojis and technique tags
  • โ€”๐Ÿ’ก Intuition section explaining the problem approach
  • โ€”๐Ÿ“– Detailed approach with step-by-step breakdowns
  • โ€”๐Ÿ” Code walkthroughs with concrete examples
  • โ€”๐Ÿ“Š Complexity analysis (time & space)
  • โ€”โœ… Clean formatting ready for publishing

Example Output Preview

### Easy Hash Map | Two Sum Magic ๐ŸŽฏ | TS Solution | LeetCode - 1

# Intuition
We need to find two numbers in an array that sum to a target value.

# Approach
Goal: Find indices of two numbers that sum to target
Strategy: One-pass hash map lookup

# Complexity
- Time complexity: O(n)
- Space complexity: O(n)

๐Ÿ“ฑ Frontend Integration

JavaScript Example

javascript
const generateBlogPost = async (problemId, solution) => {
  const response = await fetch('https://your-space-name.hf.space/generate', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      problem_id: problemId,
      solution: solution,
    }),
  });

  const data = await response.json();
  return data.markdown;
};

React Example

jsx
const [markdown, setMarkdown] = useState('');

const handleGenerate = async () => {
  try {
    const response = await fetch('/api/generate', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ problem_id, solution }),
    });
    const data = await response.json();
    setMarkdown(data.markdown);
  } catch (error) {
    console.error('Generation failed:', error);
  }
};

๐Ÿงช Testing with Postman

1. Health Check

  • โ€”Method: GET
  • โ€”URL: https://your-space-name.hf.space/health

2. Generate Content

  • โ€”Method: POST
  • โ€”URL: https://your-space-name.hf.space/generate
  • โ€”Headers: Content-Type: application/json
  • โ€”Body:
json
{
  "problem_id": "1",
  "solution": "function twoSum(nums, target) { return []; }"
}

โšก Performance & Limits

Response Times

  • โ€”First request: 10-30 seconds (model loading)
  • โ€”Subsequent requests: 3-8 seconds
  • โ€”Concurrent requests: Supported with queuing

Resource Usage

  • โ€”Model size: ~7B parameters
  • โ€”Memory usage: ~14GB VRAM (recommended)
  • โ€”CPU fallback: Available but slower

Input Limits

  • โ€”Solution code: Up to 2000 characters
  • โ€”Problem ID: Any valid string
  • โ€”Output: ~1000-2000 characters markdown

๐Ÿ”ง Local Development

Prerequisites

bash
pip install fastapi uvicorn transformers torch accelerate

Run Locally

bash
git clone https://huggingface.co/spaces/YOUR_USERNAME/leetcode-solution-post-genai
cd leetcode-solution-post-genai
python app.py

๐Ÿค Contributing

We welcome contributions! You can help by:

  • โ€”๐Ÿ› Reporting bugs
  • โ€”๐Ÿ’ก Suggesting features
  • โ€”๐Ÿ“– Improving documentation
  • โ€”๐Ÿงช Testing with different solutions
  • โ€”โšก Optimizing performance

๐Ÿ“„ License

This project is licensed under the MIT License.

๐Ÿ™ Acknowledgments

  • โ€”๐Ÿค— HuggingFace for Transformers library and hosting
  • โ€”๐ŸŒŸ BigCode Team for StarCoder2 model
  • โ€”โšก FastAPI for the web framework
  • โ€”๐Ÿ’ป LeetCode for inspiration

Made with โค๏ธ for the coding community

โญ Star this project if you find it useful!