Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142import gradio as grimport torchfrom transformers import CodeT5ForConditionalGeneration, CodeT5Tokenizer# Load pre-trained CodeT5 model and tokenizermodel = CodeT5ForConditionalGeneration.from_pretrained("Salesforce/code-t5-small")tokenizer = CodeT5Tokenizer.from_pretrained("Salesforce/code-t5-small")def generate_code(prompt, code_file):# Read uploaded code fileif code_file:code_text = code_file.read().decode("utf-8")else:code_text = ""# Tokenize input promptinput_ids = tokenizer.encode(prompt, return_tensors="pt")# Generate code using CodeT5 modeloutput = model.generate(input_ids=input_ids, max_length=256)generated_code = tokenizer.decode(output[0], skip_special_tokens=True)# Return generated code and code previewreturn generated_code, f"```python\n{generated_code}\n```"# Create Gradio interfaceiface = gr.Interface(fn=generate_code,inputs=[________gr.Textbox(label="Input_Prompt",_placeholder="Enter_a_prompt"),________gr.Upload(label="Upload_Code_File",_file_types=["py"])],outputs=[________gr.Textbox(label="Generated_Code"),________gr.Code(label="Code_Preview",_language="python")____],title="Code Generation with CodeT5",description="Generate Python code based on input prompt and uploaded code file.")# Launch Gradio interfaceiface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import gradio as grdef fix_code(code_input, language, fix_mode):"""Placeholder function to 'fix' the code.In a real scenario, you'd integrate your LLM or custom logic here."""# For demonstration, just returning a mock "fixed" versionfixed_code = f"# Fixed [{language} - {fix_mode}]\n" + code_inputexplanation = ("Here's a mock explanation of what was fixed:\n""1. Analyzed the code.\n""2. Applied debugging/optimization.\n""3. Returned updated code.")return explanation, fixed_codedef run_code(code_input, language):"""Run the corrected code in a sandbox or a safe environment.Here, we just pretend to run it and return a mock output.IMPORTANT: Executing arbitrary code can be dangerous.In real-world apps, you need a secure sandbox environment."""# Mock execution outputreturn f"Running [{language}] code...\nOutput:\nHello from the mock run!\n\nCode:\n{code_input}"def chat_mode_interaction(user_input):"""Example 'Chat Mode' function."""# Return a mock chat responsereturn f"You said: {user_input}\n\nAI says: This is a placeholder chat response."# Build the UIwith gr.Blocks(title="AI Code Doctor - Fix, Optimize & Run") as demo:gr.Markdown("""# AI Code Doctor - Fix, Optimize & Run**The Ultimate Debugging Machine**""")# Tabs for Code Debug vs. Chat Modewith gr.Tab("Code Debug"):with gr.Row():with gr.Column():language = gr.Dropdown(label="Select Language",choices=["Python", "JavaScript", "C++", "Mistral", "Any"],value="Python")fix_mode = gr.Dropdown(label="Select Fix Mode",choices=["Fix Errors Only", "Fix + Optimize", "Fix + Explain"],value="Fix + Explain")code_input = gr.Textbox(label="Paste Your Code",lines=10,placeholder="Enter code here...")# Buttonsfix_button = gr.Button("⚙️ Fix Code")# AI Outputai_explanation = gr.Textbox(label="AI Explanation",lines=5,interactive=False)corrected_code = gr.Textbox(label="Corrected Code",lines=10,interactive=False)with gr.Column():execution_output = gr.Textbox(label="Execution Output",lines=15,interactive=False)run_button = gr.Button("▶️ Run My Code")# Define button actionsfix_button.click(fix_code,inputs=[code_input, language, fix_mode],outputs=[ai_explanation, corrected_code])run_button.click(run_code,inputs=[corrected_code, language],outputs=execution_output)with gr.Tab("Chat Mode"):chat_input = gr.Textbox(label="Enter your message", lines=2)chat_output = gr.Textbox(label="Chat Output", lines=5, interactive=False)chat_button = gr.Button("Send")chat_button.click(chat_mode_interaction,inputs=chat_input,outputs=chat_output)demo.launch()
app.py+110-41