Canstralian commited on
Commit
d248a03
·
verified ·
1 Parent(s): d53fd3d

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (1) hide show
  1. app.py +10 -101
app.py CHANGED
@@ -1,101 +1,10 @@
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.
21
- Returns the fixed code and the linting report.
22
- """
23
- try:
24
- # Run Flake8 for linting, ignoring line-length violations (E501)
25
- style_guide = flake8.get_style_guide(ignore=["E501"])
26
-
27
- # Directly check code content
28
- report = style_guide.check_code(code)
29
-
30
- # Auto-fix the code with autopep8
31
- fixed_code = autopep8.fix_code(code)
32
-
33
- # Generate the linting report
34
- lint_report = "\n".join(msg for msg in report.get_statistics())
35
-
36
- return fixed_code, lint_report
37
- except Exception as e:
38
- return code, f"An error occurred during linting: {str(e)}"
39
-
40
- def process_file(file) -> tuple[str, str]:
41
- """
42
- Process the uploaded file, decode its content, and lint it.
43
- Returns the fixed code and linting report.
44
- """
45
- try:
46
- # Read the file content and decode it to a string
47
- with file as f:
48
- code = f.read().decode("utf-8")
49
-
50
- # Lint the code and get the report
51
- return lint_code(code)
52
- except Exception as e:
53
- return "", f"An error occurred while processing the file: {str(e)}"
54
-
55
- def handle_input(code: str, file) -> tuple[str, str]:
56
- """
57
- Handle both code inputs: either text or file.
58
- If a file is provided, process the file. Otherwise, process the pasted code.
59
- """
60
- if file:
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()
 
1
+ fastapi==0.95.2
2
+ uvicorn==0.22.0
3
+ transformers==4.38.0
4
+ torch==2.2.0
5
+ pydantic==1.10.13
6
+ gradio==3.1.4
7
+ datasets==2.13.1
8
+ huggingface_hub==0.16.4
9
+ flake8==6.0.0
10
+ autopep8==2.0.2