SIddz commited on
Commit
44099e4
·
verified ·
1 Parent(s): 869ee13

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # ==== CONFIGURATION ====
5
+
6
+ SUPPORTED_LANGUAGES = [
7
+ ("python3", "Python 3"),
8
+ ("javascript", "JavaScript"),
9
+ ("cpp", "C++"),
10
+ ("java", "Java"),
11
+ ("go", "Go"),
12
+ ("rust", "Rust"),
13
+ ]
14
+
15
+ # -- Replace with your Nebius API details --
16
+ NEBIUS_API_URL = "https://api.studio.nebius.com/v1/" # Change to your actual endpoint
17
+ NEBIUS_API_TOKEN = "eyJhbGciOiJIUzI1NiIsImtpZCI6IlV6SXJWd1h0dnprLVRvdzlLZWstc0M1akptWXBvX1VaVkxUZlpnMDRlOFUiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiJnb29nbGUtb2F1dGgyfDEwOTczNTk0MjQyODU0MDg2Njk4NyIsInNjb3BlIjoib3BlbmlkIG9mZmxpbmVfYWNjZXNzIiwiaXNzIjoiYXBpX2tleV9pc3N1ZXIiLCJhdWQiOlsiaHR0cHM6Ly9uZWJpdXMtaW5mZXJlbmNlLmV1LmF1dGgwLmNvbS9hcGkvdjIvIl0sImV4cCI6MTkwNjc4NDM3NSwidXVpZCI6ImJhYmM2ODY0LTExNDQtNDNlYi1hMDg4LTI0YWNkMmEzNzI4ZiIsIm5hbWUiOiJhcyIsImV4cGlyZXNfYXQiOiIyMDMwLTA2LTA0VDA2OjE5OjM1KzAwMDAifQ.bcb06nZ8f9QuLsRShj3iUBcaguCdU5huiFFZVJwi_bE" # Put your actual token here
18
+
19
+ # ==== NEBIUS AI REVIEW AGENT ====
20
+
21
+ def ai_review(code, language):
22
+ """
23
+ Uses Nebius AI's API to review code.
24
+ Edit endpoint and token to match your deployment.
25
+ """
26
+ prompt = f"Review this {language} code for logic errors, style, and suggest improvements:\n\n{code}\n\n"
27
+ headers = {
28
+ "Authorization": f"Bearer {NEBIUS_API_TOKEN}",
29
+ "Content-Type": "application/json"
30
+ }
31
+ data = {
32
+ "prompt": prompt,
33
+ "max_tokens": 500
34
+ }
35
+ try:
36
+ response = requests.post(NEBIUS_API_URL, headers=headers, json=data, timeout=30)
37
+ if response.ok:
38
+ result = response.json()
39
+ # Adjust parsing based on Nebius AI's actual response format.
40
+ if 'generated_text' in result:
41
+ return result['generated_text']
42
+ if 'choices' in result and result['choices']:
43
+ return result['choices'][0].get('text', 'No output.')
44
+ return str(result)
45
+ return f"Error: {response.text}"
46
+ except Exception as e:
47
+ return f"Nebius AI review failed: {e}"
48
+
49
+ # ==== SECURE CODE EXECUTION AGENT (Piston API) ====
50
+
51
+ def run_code(code, language):
52
+ """Runs code securely using Piston API."""
53
+ url = "https://emkc.org/api/v2/piston/execute"
54
+ payload = {"language": language, "source": code}
55
+ try:
56
+ response = requests.post(url, json=payload, timeout=10)
57
+ if response.ok:
58
+ result = response.json()
59
+ return result.get("output", "No output.")
60
+ return f"Error: {response.text}"
61
+ except Exception as e:
62
+ return f"Error during code execution: {e}"
63
+
64
+ # ==== CONTROLLER LOGIC ====
65
+
66
+ def review_and_run(code, language_name):
67
+ code_lang = {name: code for code, name in SUPPORTED_LANGUAGES}
68
+ language_code = code_lang.get(language_name, "python3")
69
+ review = ai_review(code, language_name)
70
+ run_result = run_code(code, language_code)
71
+ return review, run_result
72
+
73
+ def _ai_review(code, language_name):
74
+ return ai_review(code, language_name)
75
+
76
+ def _run_code(code, language_name):
77
+ code_lang = {name: code for code, name in SUPPORTED_LANGUAGES}
78
+ language_code = code_lang.get(language_name, "python3")
79
+ return run_code(code, language_code)
80
+
81
+ # ==== BUILD GRADIO UI ====
82
+
83
+ with gr.Blocks(title="AI-Powered Code Review & Runner (Nebius AI)") as demo:
84
+ gr.Markdown("# 🤖 AI Code Review & Execution Assistant (Nebius AI 🟦)")
85
+ gr.Markdown(
86
+ "Paste your code, select a language, and get **AI-powered review & code execution**.<br>" +
87
+ "**AI review is powered by Nebius LLM. Code execution is sandboxed (Piston API).**"
88
+ )
89
+ with gr.Row():
90
+ code_input = gr.Code(label="Your Code", language="python", lines=15)
91
+ language_input = gr.Dropdown(
92
+ [name for code, name in SUPPORTED_LANGUAGES],
93
+ value="Python 3",
94
+ label="Language",
95
+ )
96
+ with gr.Row():
97
+ review_btn = gr.Button("AI Review Only")
98
+ run_btn = gr.Button("Run Code Only")
99
+ both_btn = gr.Button("AI Review & Run")
100
+
101
+ ai_review_output = gr.Textbox(label="AI Review Output (Nebius)", lines=6)
102
+ run_output = gr.Textbox(label="Run Output", lines=6)
103
+
104
+ review_btn.click(_ai_review, [code_input, language_input], ai_review_output)
105
+ run_btn.click(_run_code, [code_input, language_input], run_output)
106
+ both_btn.click(review_and_run, [code_input, language_input], [ai_review_output, run_output])
107
+
108
+ demo.launch()