Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +10 -13
- app.py +25 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,14 +1,11 @@
|
|
| 1 |
-
|
| 2 |
-
title: Code Generator
|
| 3 |
-
emoji: 🏃
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: gray
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.38.2
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: apache-2.0
|
| 11 |
-
short_description: code-generator
|
| 12 |
-
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AI Code Generator Gradio App
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
This app lets you generate code snippets using a Hugging Face LLM (e.g., StarCoder).
|
| 4 |
+
Deploy it on [Hugging Face Spaces](https://huggingface.co/spaces) for free.
|
| 5 |
+
|
| 6 |
+
## 🧠 How to Use
|
| 7 |
+
1. Type your prompt (e.g., "read a JSON file in Python")
|
| 8 |
+
2. Select a language
|
| 9 |
+
3. Click "Submit" to generate the code
|
| 10 |
+
|
| 11 |
+
Built with Gradio + Transformers.
|
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the text-generation pipeline from Hugging Face
|
| 5 |
+
code_gen = pipeline("text-generation", model="bigcode/starcoder", device_map="auto")
|
| 6 |
+
|
| 7 |
+
def generate_code(prompt, language):
|
| 8 |
+
header = f"# Language: {language}\n"
|
| 9 |
+
full_prompt = header + prompt
|
| 10 |
+
|
| 11 |
+
output = code_gen(full_prompt, max_new_tokens=200, temperature=0.3, do_sample=True)[0]['generated_text']
|
| 12 |
+
|
| 13 |
+
# Trim only the newly generated code part
|
| 14 |
+
return output.replace(full_prompt, "").strip()
|
| 15 |
+
|
| 16 |
+
gr.Interface(
|
| 17 |
+
fn=generate_code,
|
| 18 |
+
inputs=[
|
| 19 |
+
gr.Textbox(lines=4, label="Enter your code task or prompt"),
|
| 20 |
+
gr.Dropdown(["Python", "JavaScript", "C++", "Java", "Bash", "HTML"], label="Target Language", value="Python"),
|
| 21 |
+
],
|
| 22 |
+
outputs=gr.Code(label="Generated Code"),
|
| 23 |
+
title="AI Code Generator",
|
| 24 |
+
description="Generate code snippets using a Hugging Face LLM like StarCoder. Try writing prompts like 'sort a list of integers' or 'read a CSV and print the average column value'."
|
| 25 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|