Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,9 @@ import numpy as np
|
|
19 |
import matplotlib.pyplot as plt
|
20 |
import seaborn as sns
|
21 |
from IPython.display import display
|
|
|
|
|
|
|
22 |
|
23 |
# Configure the Gemini API
|
24 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
@@ -32,10 +35,11 @@ generation_config = {
|
|
32 |
}
|
33 |
|
34 |
model = genai.GenerativeModel(
|
35 |
-
model_name="gemini-
|
36 |
generation_config=generation_config,
|
37 |
system_instruction="""
|
38 |
You are Ath, a highly knowledgeable and advanced code assistant. Your responses are optimized for secure, high-quality, and cutting-edge code solutions.
|
|
|
39 |
"""
|
40 |
)
|
41 |
chat_session = model.start_chat(history=[])
|
@@ -48,12 +52,21 @@ def generate_response(user_input):
|
|
48 |
except Exception as e:
|
49 |
return f"Error: {e}"
|
50 |
|
51 |
-
def optimize_code(code):
|
52 |
"""Optimize the generated code using static analysis tools."""
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
return code
|
58 |
|
59 |
def fetch_from_github(query):
|
@@ -90,12 +103,19 @@ def initialize_git_repo(repo_path):
|
|
90 |
repo = git.Repo(repo_path)
|
91 |
return repo
|
92 |
|
93 |
-
def integrate_with_git(repo_path, code):
|
94 |
"""Integrate the generated code with a Git repository."""
|
95 |
repo = initialize_git_repo(repo_path)
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
file.write(code)
|
98 |
-
repo.index.add([
|
99 |
repo.index.commit("Added generated code")
|
100 |
|
101 |
def process_user_input(user_input):
|
@@ -125,16 +145,24 @@ def run_tests():
|
|
125 |
test_result = test_runner.run(test_suite)
|
126 |
return test_result
|
127 |
|
128 |
-
def execute_code_in_docker(code):
|
129 |
"""Execute code in a Docker container for safety and isolation."""
|
130 |
client = docker.from_env()
|
131 |
try:
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
result = container.wait()
|
139 |
logs = container.logs().decode('utf-8')
|
140 |
return logs, result['StatusCode']
|
@@ -172,6 +200,16 @@ def display_dataframe(data):
|
|
172 |
df = pd.DataFrame(data)
|
173 |
display(df)
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
# Streamlit UI setup
|
176 |
st.set_page_config(page_title="Ultra AI Code Assistant", page_icon="π", layout="wide")
|
177 |
|
@@ -267,6 +305,7 @@ st.title("π Ultra AI Code Assistant")
|
|
267 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
268 |
|
269 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
|
|
270 |
|
271 |
if st.button("Generate Code"):
|
272 |
if prompt.strip() == "":
|
@@ -279,18 +318,17 @@ if st.button("Generate Code"):
|
|
279 |
if "Error" in completed_text:
|
280 |
handle_error(completed_text)
|
281 |
else:
|
282 |
-
optimized_code = optimize_code(completed_text)
|
283 |
st.success("Code generated and optimized successfully!")
|
284 |
|
|
|
285 |
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
286 |
-
st.markdown(
|
287 |
-
st.code(optimized_code)
|
288 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
289 |
st.markdown('</div>', unsafe_allow_html=True)
|
290 |
|
291 |
# Integrate with Git
|
292 |
repo_path = "./repo" # Replace with your repository path
|
293 |
-
integrate_with_git(repo_path, optimized_code)
|
294 |
|
295 |
# Run automated tests
|
296 |
test_result = run_tests()
|
@@ -300,7 +338,7 @@ if st.button("Generate Code"):
|
|
300 |
st.error("Some tests failed. Please check the code.")
|
301 |
|
302 |
# Execute code in Docker
|
303 |
-
execution_result, status_code = execute_code_in_docker(optimized_code)
|
304 |
if status_code == 0:
|
305 |
st.success("Code executed successfully in Docker!")
|
306 |
st.text(execution_result)
|
|
|
19 |
import matplotlib.pyplot as plt
|
20 |
import seaborn as sns
|
21 |
from IPython.display import display
|
22 |
+
import json
|
23 |
+
import html
|
24 |
+
import re
|
25 |
|
26 |
# Configure the Gemini API
|
27 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
|
|
35 |
}
|
36 |
|
37 |
model = genai.GenerativeModel(
|
38 |
+
model_name="gemini-2.0-pro",
|
39 |
generation_config=generation_config,
|
40 |
system_instruction="""
|
41 |
You are Ath, a highly knowledgeable and advanced code assistant. Your responses are optimized for secure, high-quality, and cutting-edge code solutions.
|
42 |
+
Focus on generating code that is efficient, readable, and adheres to best practices. Ensure that the code is well-documented and includes error handling where necessary.
|
43 |
"""
|
44 |
)
|
45 |
chat_session = model.start_chat(history=[])
|
|
|
52 |
except Exception as e:
|
53 |
return f"Error: {e}"
|
54 |
|
55 |
+
def optimize_code(code, language):
|
56 |
"""Optimize the generated code using static analysis tools."""
|
57 |
+
if language == 'python':
|
58 |
+
with open("temp_code.py", "w") as file:
|
59 |
+
file.write(code)
|
60 |
+
result = subprocess.run(["pylint", "temp_code.py"], capture_output=True, text=True)
|
61 |
+
os.remove("temp_code.py")
|
62 |
+
elif language == 'javascript':
|
63 |
+
with open("temp_code.js", "w") as file:
|
64 |
+
file.write(code)
|
65 |
+
result = subprocess.run(["eslint", "temp_code.js"], capture_output=True, text=True)
|
66 |
+
os.remove("temp_code.js")
|
67 |
+
elif language == 'html':
|
68 |
+
# HTML optimization can be more complex due to its structure
|
69 |
+
code = html.escape(code)
|
70 |
return code
|
71 |
|
72 |
def fetch_from_github(query):
|
|
|
103 |
repo = git.Repo(repo_path)
|
104 |
return repo
|
105 |
|
106 |
+
def integrate_with_git(repo_path, code, language):
|
107 |
"""Integrate the generated code with a Git repository."""
|
108 |
repo = initialize_git_repo(repo_path)
|
109 |
+
filename = "generated_code"
|
110 |
+
if language == 'python':
|
111 |
+
filename += ".py"
|
112 |
+
elif language == 'javascript':
|
113 |
+
filename += ".js"
|
114 |
+
elif language == 'html':
|
115 |
+
filename += ".html"
|
116 |
+
with open(os.path.join(repo_path, filename), "w") as file:
|
117 |
file.write(code)
|
118 |
+
repo.index.add([filename])
|
119 |
repo.index.commit("Added generated code")
|
120 |
|
121 |
def process_user_input(user_input):
|
|
|
145 |
test_result = test_runner.run(test_suite)
|
146 |
return test_result
|
147 |
|
148 |
+
def execute_code_in_docker(code, language):
|
149 |
"""Execute code in a Docker container for safety and isolation."""
|
150 |
client = docker.from_env()
|
151 |
try:
|
152 |
+
if language == 'python':
|
153 |
+
container = client.containers.run(
|
154 |
+
image="python:3.9",
|
155 |
+
command=f"python -c '{code}'",
|
156 |
+
detach=True,
|
157 |
+
remove=True
|
158 |
+
)
|
159 |
+
elif language == 'javascript':
|
160 |
+
container = client.containers.run(
|
161 |
+
image="node:14",
|
162 |
+
command=f"node -e '{code}'",
|
163 |
+
detach=True,
|
164 |
+
remove=True
|
165 |
+
)
|
166 |
result = container.wait()
|
167 |
logs = container.logs().decode('utf-8')
|
168 |
return logs, result['StatusCode']
|
|
|
200 |
df = pd.DataFrame(data)
|
201 |
display(df)
|
202 |
|
203 |
+
def format_code(code, language):
|
204 |
+
"""Format code using appropriate syntax highlighting."""
|
205 |
+
if language == 'python':
|
206 |
+
return f"```python\n{code}\n```"
|
207 |
+
elif language == 'javascript':
|
208 |
+
return f"```javascript\n{code}\n```"
|
209 |
+
elif language == 'html':
|
210 |
+
return f"```html\n{code}\n```"
|
211 |
+
return code
|
212 |
+
|
213 |
# Streamlit UI setup
|
214 |
st.set_page_config(page_title="Ultra AI Code Assistant", page_icon="π", layout="wide")
|
215 |
|
|
|
305 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
306 |
|
307 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
308 |
+
language = st.selectbox("Select language", ["python", "javascript", "html"])
|
309 |
|
310 |
if st.button("Generate Code"):
|
311 |
if prompt.strip() == "":
|
|
|
318 |
if "Error" in completed_text:
|
319 |
handle_error(completed_text)
|
320 |
else:
|
321 |
+
optimized_code = optimize_code(completed_text, language)
|
322 |
st.success("Code generated and optimized successfully!")
|
323 |
|
324 |
+
formatted_code = format_code(optimized_code, language)
|
325 |
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
326 |
+
st.markdown(formatted_code, unsafe_allow_html=True)
|
|
|
|
|
327 |
st.markdown('</div>', unsafe_allow_html=True)
|
328 |
|
329 |
# Integrate with Git
|
330 |
repo_path = "./repo" # Replace with your repository path
|
331 |
+
integrate_with_git(repo_path, optimized_code, language)
|
332 |
|
333 |
# Run automated tests
|
334 |
test_result = run_tests()
|
|
|
338 |
st.error("Some tests failed. Please check the code.")
|
339 |
|
340 |
# Execute code in Docker
|
341 |
+
execution_result, status_code = execute_code_in_docker(optimized_code, language)
|
342 |
if status_code == 0:
|
343 |
st.success("Code executed successfully in Docker!")
|
344 |
st.text(execution_result)
|