whackthejacker commited on
Commit
8a7793a
·
verified ·
1 Parent(s): b81292f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -41
app.py CHANGED
@@ -1,42 +1,111 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import CodeT5ForConditionalGeneration, CodeT5Tokenizer
4
-
5
- # Load pre-trained CodeT5 model and tokenizer
6
- model = CodeT5ForConditionalGeneration.from_pretrained("Salesforce/code-t5-small")
7
- tokenizer = CodeT5Tokenizer.from_pretrained("Salesforce/code-t5-small")
8
-
9
- def generate_code(prompt, code_file):
10
- # Read uploaded code file
11
- if code_file:
12
- code_text = code_file.read().decode("utf-8")
13
- else:
14
- code_text = ""
15
-
16
- # Tokenize input prompt
17
- input_ids = tokenizer.encode(prompt, return_tensors="pt")
18
-
19
- # Generate code using CodeT5 model
20
- output = model.generate(input_ids=input_ids, max_length=256)
21
- generated_code = tokenizer.decode(output[0], skip_special_tokens=True)
22
-
23
- # Return generated code and code preview
24
- return generated_code, f"```python\n{generated_code}\n```"
25
-
26
- # Create Gradio interface
27
- iface = gr.Interface(
28
- fn=generate_code,
29
- inputs=[
30
- ________gr.Textbox(label="Input_Prompt",_placeholder="Enter_a_prompt"),
31
- ________gr.Upload(label="Upload_Code_File",_file_types=["py"])
32
- ],
33
- outputs=[
34
- ________gr.Textbox(label="Generated_Code"),
35
- ________gr.Code(label="Code_Preview",_language="python")
36
- ____],
37
- title="Code Generation with CodeT5",
38
- description="Generate Python code based on input prompt and uploaded code file."
39
- )
40
-
41
- # Launch Gradio interface
42
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+
3
+ def fix_code(code_input, language, fix_mode):
4
+ """
5
+ Placeholder function to 'fix' the code.
6
+ In a real scenario, you'd integrate your LLM or custom logic here.
7
+ """
8
+ # For demonstration, just returning a mock "fixed" version
9
+ fixed_code = f"# Fixed [{language} - {fix_mode}]\n" + code_input
10
+ explanation = (
11
+ "Here's a mock explanation of what was fixed:\n"
12
+ "1. Analyzed the code.\n"
13
+ "2. Applied debugging/optimization.\n"
14
+ "3. Returned updated code."
15
+ )
16
+ return explanation, fixed_code
17
+
18
+ def run_code(code_input, language):
19
+ """
20
+ Run the corrected code in a sandbox or a safe environment.
21
+ Here, we just pretend to run it and return a mock output.
22
+
23
+ IMPORTANT: Executing arbitrary code can be dangerous.
24
+ In real-world apps, you need a secure sandbox environment.
25
+ """
26
+ # Mock execution output
27
+ return f"Running [{language}] code...\nOutput:\nHello from the mock run!\n\nCode:\n{code_input}"
28
+
29
+ def chat_mode_interaction(user_input):
30
+ """
31
+ Example 'Chat Mode' function.
32
+ """
33
+ # Return a mock chat response
34
+ return f"You said: {user_input}\n\nAI says: This is a placeholder chat response."
35
+
36
+ # Build the UI
37
+ with gr.Blocks(title="AI Code Doctor - Fix, Optimize & Run") as demo:
38
+ gr.Markdown(
39
+ """
40
+ # AI Code Doctor - Fix, Optimize & Run
41
+ **The Ultimate Debugging Machine**
42
+ """
43
+ )
44
+
45
+ # Tabs for Code Debug vs. Chat Mode
46
+ with gr.Tab("Code Debug"):
47
+ with gr.Row():
48
+ with gr.Column():
49
+ language = gr.Dropdown(
50
+ label="Select Language",
51
+ choices=["Python", "JavaScript", "C++", "Mistral", "Any"],
52
+ value="Python"
53
+ )
54
+ fix_mode = gr.Dropdown(
55
+ label="Select Fix Mode",
56
+ choices=["Fix Errors Only", "Fix + Optimize", "Fix + Explain"],
57
+ value="Fix + Explain"
58
+ )
59
+ code_input = gr.Textbox(
60
+ label="Paste Your Code",
61
+ lines=10,
62
+ placeholder="Enter code here..."
63
+ )
64
+
65
+ # Buttons
66
+ fix_button = gr.Button("⚙️ Fix Code")
67
+
68
+ # AI Output
69
+ ai_explanation = gr.Textbox(
70
+ label="AI Explanation",
71
+ lines=5,
72
+ interactive=False
73
+ )
74
+ corrected_code = gr.Textbox(
75
+ label="Corrected Code",
76
+ lines=10,
77
+ interactive=False
78
+ )
79
+
80
+ with gr.Column():
81
+ execution_output = gr.Textbox(
82
+ label="Execution Output",
83
+ lines=15,
84
+ interactive=False
85
+ )
86
+ run_button = gr.Button("▶️ Run My Code")
87
+
88
+ # Define button actions
89
+ fix_button.click(
90
+ fix_code,
91
+ inputs=[code_input, language, fix_mode],
92
+ outputs=[ai_explanation, corrected_code]
93
+ )
94
+ run_button.click(
95
+ run_code,
96
+ inputs=[corrected_code, language],
97
+ outputs=execution_output
98
+ )
99
+
100
+ with gr.Tab("Chat Mode"):
101
+ chat_input = gr.Textbox(label="Enter your message", lines=2)
102
+ chat_output = gr.Textbox(label="Chat Output", lines=5, interactive=False)
103
+ chat_button = gr.Button("Send")
104
+
105
+ chat_button.click(
106
+ chat_mode_interaction,
107
+ inputs=chat_input,
108
+ outputs=chat_output
109
+ )
110
+
111
+ demo.launch()