Spaces:
Sleeping
Sleeping
Initial code snippet generator space
Browse files- README.md +31 -8
- app.py +125 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,13 +1,36 @@
|
|
| 1 |
---
|
| 2 |
title: Code Snippet Generator
|
| 3 |
-
emoji: 🐠
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
|
| 8 |
-
python_version: '3.13'
|
| 9 |
-
app_file: app.py
|
| 10 |
-
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Code Snippet Generator
|
|
|
|
|
|
|
|
|
|
| 3 |
sdk: gradio
|
| 4 |
+
app_port: 7860
|
|
|
|
|
|
|
|
|
|
| 5 |
---
|
| 6 |
|
| 7 |
+
# Code Snippet Generator
|
| 8 |
+
|
| 9 |
+
Get ready-to-use code snippets for common development tasks. Built with Gradio for fast, interactive access.
|
| 10 |
+
|
| 11 |
+
## Features
|
| 12 |
+
|
| 13 |
+
- **Multiple Languages:** Python, JavaScript, SQL, Bash
|
| 14 |
+
- **Common Tasks:** File I/O, Error Handling, Async, Array Operations
|
| 15 |
+
- **Instant Access:** No setup required, just select and generate
|
| 16 |
+
- **Copy-Paste Ready:** All snippets are tested and production-ready
|
| 17 |
+
|
| 18 |
+
## How to Use
|
| 19 |
+
|
| 20 |
+
1. Select your programming language
|
| 21 |
+
2. Choose the task you need help with
|
| 22 |
+
3. Click "Generate Snippet"
|
| 23 |
+
4. Copy the code and use it in your project
|
| 24 |
+
|
| 25 |
+
## Tech Stack
|
| 26 |
+
|
| 27 |
+
- Gradio for the UI
|
| 28 |
+
- Python for the backend
|
| 29 |
+
- HuggingFace Spaces for hosting
|
| 30 |
+
|
| 31 |
+
## License
|
| 32 |
+
|
| 33 |
+
MIT License - Free for personal and commercial use.
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
*Built with ❤️ by AI, maintained by humans*
|
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Code Snippet Generator - HuggingFace Space
|
| 3 |
+
Generates ready-to-use code snippets for common development tasks.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Code snippet templates
|
| 10 |
+
SNIPPETS = {
|
| 11 |
+
"Python": {
|
| 12 |
+
"File Operations": """
|
| 13 |
+
# Read file
|
| 14 |
+
with open('file.txt', 'r') as f:
|
| 15 |
+
content = f.read()
|
| 16 |
+
|
| 17 |
+
# Write file
|
| 18 |
+
with open('file.txt', 'w') as f:
|
| 19 |
+
f.write('content')
|
| 20 |
+
|
| 21 |
+
# JSON
|
| 22 |
+
import json
|
| 23 |
+
with open('data.json') as f:
|
| 24 |
+
data = json.load(f)
|
| 25 |
+
|
| 26 |
+
# CSV
|
| 27 |
+
import csv
|
| 28 |
+
with open('data.csv') as f:
|
| 29 |
+
reader = csv.DictReader(f)
|
| 30 |
+
for row in reader:
|
| 31 |
+
pass
|
| 32 |
+
""",
|
| 33 |
+
"Error Handling": """
|
| 34 |
+
try:
|
| 35 |
+
result = risky_operation()
|
| 36 |
+
except ValueError as e:
|
| 37 |
+
handle_error(e)
|
| 38 |
+
except (TypeError, KeyError):
|
| 39 |
+
pass
|
| 40 |
+
else:
|
| 41 |
+
success_case()
|
| 42 |
+
finally:
|
| 43 |
+
cleanup()
|
| 44 |
+
""",
|
| 45 |
+
},
|
| 46 |
+
"JavaScript": {
|
| 47 |
+
"Async/Await": """
|
| 48 |
+
// Fetch with async/await
|
| 49 |
+
async function getData(url) {
|
| 50 |
+
try {
|
| 51 |
+
const res = await fetch(url);
|
| 52 |
+
const data = await res.json();
|
| 53 |
+
return data;
|
| 54 |
+
} catch (err) {
|
| 55 |
+
console.error(err);
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// Parallel requests
|
| 60 |
+
const [users, posts] = await Promise.all([
|
| 61 |
+
fetch('/users').then(r => r.json()),
|
| 62 |
+
fetch('/posts').then(r => r.json())
|
| 63 |
+
]);
|
| 64 |
+
""",
|
| 65 |
+
"Array Methods": """
|
| 66 |
+
// Map, filter, reduce
|
| 67 |
+
const doubled = arr.map(x => x * 2);
|
| 68 |
+
const evens = arr.filter(x => x % 2 === 0);
|
| 69 |
+
const sum = arr.reduce((a, b) => a + b, 0);
|
| 70 |
+
|
| 71 |
+
// Find first match
|
| 72 |
+
const user = users.find(u => u.id === targetId);
|
| 73 |
+
|
| 74 |
+
// Group by property
|
| 75 |
+
const grouped = arr.reduce((acc, item) => {
|
| 76 |
+
(acc[item.type] = acc[item.type] || []).push(item);
|
| 77 |
+
return acc;
|
| 78 |
+
}, {});
|
| 79 |
+
""",
|
| 80 |
+
},
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
def generate_snippet(language, task):
|
| 84 |
+
"""Generate a code snippet for the given language and task"""
|
| 85 |
+
if language in SNIPPETS:
|
| 86 |
+
for task_name, snippet in SNIPPETS[language].items():
|
| 87 |
+
if task.lower() in task_name.lower() or task_name.lower() in task.lower():
|
| 88 |
+
return snippet
|
| 89 |
+
|
| 90 |
+
# Default response
|
| 91 |
+
return f"# {language} snippet for: {task}\n\n// Generated by Code Snippet Generator\n// Customize this template as needed\n\ndef task():\n pass"
|
| 92 |
+
|
| 93 |
+
# Gradio interface
|
| 94 |
+
with gr.Blocks() as demo:
|
| 95 |
+
gr.Markdown("""
|
| 96 |
+
# Code Snippet Generator
|
| 97 |
+
Get ready-to-use code snippets for common development tasks.
|
| 98 |
+
|
| 99 |
+
**Languages:** Python, JavaScript, SQL, Bash
|
| 100 |
+
**Tasks:** File I/O, Error Handling, Async, Array Operations, and more
|
| 101 |
+
""")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
language = gr.Dropdown(
|
| 105 |
+
choices=["Python", "JavaScript", "SQL", "Bash"],
|
| 106 |
+
value="Python",
|
| 107 |
+
label="Language"
|
| 108 |
+
)
|
| 109 |
+
task = gr.Dropdown(
|
| 110 |
+
choices=["File Operations", "Error Handling", "Async/Await", "Array Methods"],
|
| 111 |
+
value="File Operations",
|
| 112 |
+
label="Task"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
generate_btn = gr.Button("Generate Snippet", variant="primary")
|
| 116 |
+
output = gr.Code(label="Generated Code", language="python")
|
| 117 |
+
|
| 118 |
+
generate_btn.click(
|
| 119 |
+
fn=generate_snippet,
|
| 120 |
+
inputs=[language, task],
|
| 121 |
+
outputs=output
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
huggingface_hub
|