Spaces:
Paused
Paused
Synced repo using 'sync_with_huggingface' Github Action
Browse files
app.py
CHANGED
|
@@ -1,7 +1,20 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from flake8.api import legacy as flake8
|
| 3 |
import autopep8
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def lint_code(code: str) -> tuple[str, str]:
|
| 6 |
"""
|
| 7 |
Lint the provided Python code using Flake8 and auto-fix it with autopep8.
|
|
@@ -48,20 +61,41 @@ def handle_input(code: str, file) -> tuple[str, str]:
|
|
| 48 |
return process_file(file)
|
| 49 |
return lint_code(code)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Launch the interface
|
| 67 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from flake8.api import legacy as flake8
|
| 4 |
import autopep8
|
| 5 |
|
| 6 |
+
# Initialize the pipeline for text generation
|
| 7 |
+
pipe = pipeline("text-generation", model="replit/replit-code-v1_5-3b", trust_remote_code=True)
|
| 8 |
+
|
| 9 |
+
def generate_code(prompt, max_length=100, num_return_sequences=1):
|
| 10 |
+
try:
|
| 11 |
+
# Use the pipeline to generate code
|
| 12 |
+
results = pipe(prompt, max_length=max_length, num_return_sequences=num_return_sequences)
|
| 13 |
+
# Format the generated results
|
| 14 |
+
return "\n\n".join([res["generated_text"] for res in results])
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return f"An error occurred: {str(e)}"
|
| 17 |
+
|
| 18 |
def lint_code(code: str) -> tuple[str, str]:
|
| 19 |
"""
|
| 20 |
Lint the provided Python code using Flake8 and auto-fix it with autopep8.
|
|
|
|
| 61 |
return process_file(file)
|
| 62 |
return lint_code(code)
|
| 63 |
|
| 64 |
+
# Gradio interface
|
| 65 |
+
with gr.Blocks() as interface:
|
| 66 |
+
gr.Markdown("### PyLint Pro: AI Code Generation Tool")
|
| 67 |
+
|
| 68 |
+
# Input fields
|
| 69 |
+
prompt_input = gr.Textbox(label="Code Prompt", placeholder="Enter your code snippet or logic...")
|
| 70 |
+
max_length_input = gr.Slider(label="Max Length", minimum=10, maximum=500, value=100)
|
| 71 |
+
num_return_sequences_input = gr.Slider(label="Number of Outputs", minimum=1, maximum=5, value=1)
|
| 72 |
+
|
| 73 |
+
# Output
|
| 74 |
+
output_box = gr.Textbox(label="Generated Code", placeholder="The generated code will appear here...", lines=10)
|
| 75 |
+
|
| 76 |
+
# Button to trigger code generation
|
| 77 |
+
generate_button = gr.Button("Generate Code")
|
| 78 |
+
|
| 79 |
+
# Define interaction
|
| 80 |
+
generate_button.click(
|
| 81 |
+
generate_code,
|
| 82 |
+
inputs=[prompt_input, max_length_input, num_return_sequences_input],
|
| 83 |
+
outputs=output_box,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Existing linting interface
|
| 87 |
+
gr.Markdown("### Python Code Linter")
|
| 88 |
+
code_input = gr.Textbox(lines=20, placeholder="Paste your Python code here...", label="Python Code")
|
| 89 |
+
file_input = gr.File(label="Upload a Python (.py) file", file_types=[".py"])
|
| 90 |
+
linted_code_output = gr.Textbox(label="Linted Code", lines=20)
|
| 91 |
+
lint_report_output = gr.Textbox(label="Linting Report")
|
| 92 |
+
lint_button = gr.Button("Lint Code")
|
| 93 |
+
lint_button.click(
|
| 94 |
+
handle_input,
|
| 95 |
+
inputs=[code_input, file_input],
|
| 96 |
+
outputs=[linted_code_output, lint_report_output],
|
| 97 |
+
)
|
| 98 |
|
| 99 |
# Launch the interface
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
interface.launch()
|