import gradio as gr import requests # ==== CONFIGURATION ==== SUPPORTED_LANGUAGES = [ ("python3", "Python 3"), ("javascript", "JavaScript"), ("cpp", "C++"), ("java", "Java"), ("go", "Go"), ("rust", "Rust"), ] # -- Replace with your Nebius API details -- NEBIUS_API_URL = "https://api.studio.nebius.com/v1/" # Change to your actual endpoint NEBIUS_API_TOKEN = "eyJhbGciOiJIUzI1NiIsImtpZCI6IlV6SXJWd1h0dnprLVRvdzlLZWstc0M1akptWXBvX1VaVkxUZlpnMDRlOFUiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiJnb29nbGUtb2F1dGgyfDEwOTczNTk0MjQyODU0MDg2Njk4NyIsInNjb3BlIjoib3BlbmlkIG9mZmxpbmVfYWNjZXNzIiwiaXNzIjoiYXBpX2tleV9pc3N1ZXIiLCJhdWQiOlsiaHR0cHM6Ly9uZWJpdXMtaW5mZXJlbmNlLmV1LmF1dGgwLmNvbS9hcGkvdjIvIl0sImV4cCI6MTkwNjc4NDM3NSwidXVpZCI6ImJhYmM2ODY0LTExNDQtNDNlYi1hMDg4LTI0YWNkMmEzNzI4ZiIsIm5hbWUiOiJhcyIsImV4cGlyZXNfYXQiOiIyMDMwLTA2LTA0VDA2OjE5OjM1KzAwMDAifQ.bcb06nZ8f9QuLsRShj3iUBcaguCdU5huiFFZVJwi_bE" # Put your actual token here # ==== NEBIUS AI REVIEW AGENT ==== def ai_review(code, language): """ Uses Nebius AI's API to review code. Edit endpoint and token to match your deployment. """ prompt = f"Review this {language} code for logic errors, style, and suggest improvements:\n\n{code}\n\n" headers = { "Authorization": f"Bearer {NEBIUS_API_TOKEN}", "Content-Type": "application/json" } data = { "prompt": prompt, "max_tokens": 500 } try: response = requests.post(NEBIUS_API_URL, headers=headers, json=data, timeout=30) if response.ok: result = response.json() # Adjust parsing based on Nebius AI's actual response format. if 'generated_text' in result: return result['generated_text'] if 'choices' in result and result['choices']: return result['choices'][0].get('text', 'No output.') return str(result) return f"Error: {response.text}" except Exception as e: return f"Nebius AI review failed: {e}" # ==== SECURE CODE EXECUTION AGENT (Piston API) ==== def run_code(code, language): """Runs code securely using Piston API.""" url = "https://emkc.org/api/v2/piston/execute" payload = {"language": language, "source": code} try: response = requests.post(url, json=payload, timeout=10) if response.ok: result = response.json() return result.get("output", "No output.") return f"Error: {response.text}" except Exception as e: return f"Error during code execution: {e}" # ==== CONTROLLER LOGIC ==== def review_and_run(code, language_name): code_lang = {name: code for code, name in SUPPORTED_LANGUAGES} language_code = code_lang.get(language_name, "python3") review = ai_review(code, language_name) run_result = run_code(code, language_code) return review, run_result def _ai_review(code, language_name): return ai_review(code, language_name) def _run_code(code, language_name): code_lang = {name: code for code, name in SUPPORTED_LANGUAGES} language_code = code_lang.get(language_name, "python3") return run_code(code, language_code) # ==== BUILD GRADIO UI ==== with gr.Blocks(title="AI-Powered Code Review & Runner (Nebius AI)") as demo: gr.Markdown("# 🤖 AI Code Review & Execution Assistant (Nebius AI 🟦)") gr.Markdown( "Paste your code, select a language, and get **AI-powered review & code execution**.
" + "**AI review is powered by Nebius LLM. Code execution is sandboxed (Piston API).**" ) with gr.Row(): code_input = gr.Code(label="Your Code", language="python", lines=15) language_input = gr.Dropdown( [name for code, name in SUPPORTED_LANGUAGES], value="Python 3", label="Language", ) with gr.Row(): review_btn = gr.Button("AI Review Only") run_btn = gr.Button("Run Code Only") both_btn = gr.Button("AI Review & Run") ai_review_output = gr.Textbox(label="AI Review Output (Nebius)", lines=6) run_output = gr.Textbox(label="Run Output", lines=6) review_btn.click(_ai_review, [code_input, language_input], ai_review_output) run_btn.click(_run_code, [code_input, language_input], run_output) both_btn.click(review_and_run, [code_input, language_input], [ai_review_output, run_output]) demo.launch()