Spaces:
Paused
Paused
Synced repo using 'sync_with_huggingface' Github Action
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ 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.
|
|
@@ -9,7 +10,12 @@ def lint_code(code: str) -> tuple[str, str]:
|
|
| 9 |
"""
|
| 10 |
# Run Flake8 for linting, ignoring line-length violations (E501)
|
| 11 |
style_guide = flake8.get_style_guide(ignore=["E501"])
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Auto-fix the code with autopep8
|
| 15 |
fixed_code = autopep8.fix_code(code)
|
|
@@ -19,6 +25,7 @@ def lint_code(code: str) -> tuple[str, str]:
|
|
| 19 |
|
| 20 |
return fixed_code, lint_report
|
| 21 |
|
|
|
|
| 22 |
def process_file(file) -> tuple[str, str]:
|
| 23 |
"""
|
| 24 |
Process the uploaded file, decode its content, and lint it.
|
|
@@ -30,6 +37,7 @@ def process_file(file) -> tuple[str, str]:
|
|
| 30 |
# Lint the code and get the report
|
| 31 |
return lint_code(code)
|
| 32 |
|
|
|
|
| 33 |
def handle_input(code: str, file) -> tuple[str, str]:
|
| 34 |
"""
|
| 35 |
Handle both code inputs: either text or file.
|
|
@@ -55,4 +63,4 @@ interface = gr.Interface(
|
|
| 55 |
)
|
| 56 |
|
| 57 |
# Launch the interface
|
| 58 |
-
interface.launch()
|
|
|
|
| 2 |
from flake8.api import legacy as flake8
|
| 3 |
import autopep8
|
| 4 |
|
| 5 |
+
# Lint code function that uses Flake8 and autopep8
|
| 6 |
def lint_code(code: str) -> tuple[str, str]:
|
| 7 |
"""
|
| 8 |
Lint the provided Python code using Flake8 and auto-fix it with autopep8.
|
|
|
|
| 10 |
"""
|
| 11 |
# Run Flake8 for linting, ignoring line-length violations (E501)
|
| 12 |
style_guide = flake8.get_style_guide(ignore=["E501"])
|
| 13 |
+
|
| 14 |
+
# Using a string IO to simulate a file for flake8 (since flake8 expects file paths)
|
| 15 |
+
from io import StringIO
|
| 16 |
+
code_file = StringIO(code)
|
| 17 |
+
|
| 18 |
+
report = style_guide.check_files([code_file])
|
| 19 |
|
| 20 |
# Auto-fix the code with autopep8
|
| 21 |
fixed_code = autopep8.fix_code(code)
|
|
|
|
| 25 |
|
| 26 |
return fixed_code, lint_report
|
| 27 |
|
| 28 |
+
# Process the uploaded file (read, decode, and lint it)
|
| 29 |
def process_file(file) -> tuple[str, str]:
|
| 30 |
"""
|
| 31 |
Process the uploaded file, decode its content, and lint it.
|
|
|
|
| 37 |
# Lint the code and get the report
|
| 38 |
return lint_code(code)
|
| 39 |
|
| 40 |
+
# Handle input, whether code is pasted or a file is uploaded
|
| 41 |
def handle_input(code: str, file) -> tuple[str, str]:
|
| 42 |
"""
|
| 43 |
Handle both code inputs: either text or file.
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch the interface
|
| 66 |
+
interface.launch()
|