Artificial-superintelligence
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -19,9 +19,7 @@ import numpy as np
|
|
19 |
import matplotlib.pyplot as plt
|
20 |
import seaborn as sns
|
21 |
from IPython.display import display
|
22 |
-
import
|
23 |
-
import html
|
24 |
-
import re
|
25 |
|
26 |
# Configure the Gemini API
|
27 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
@@ -35,7 +33,7 @@ generation_config = {
|
|
35 |
}
|
36 |
|
37 |
model = genai.GenerativeModel(
|
38 |
-
model_name="gemini-
|
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.
|
@@ -44,29 +42,21 @@ model = genai.GenerativeModel(
|
|
44 |
)
|
45 |
chat_session = model.start_chat(history=[])
|
46 |
|
|
|
47 |
def generate_response(user_input):
|
48 |
-
"""Generate a response from the AI model."""
|
49 |
try:
|
50 |
response = chat_session.send_message(user_input)
|
51 |
return response.text
|
52 |
except Exception as e:
|
53 |
return f"Error: {e}"
|
54 |
|
55 |
-
def optimize_code(code
|
56 |
"""Optimize the generated code using static analysis tools."""
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
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,19 +93,12 @@ def initialize_git_repo(repo_path):
|
|
103 |
repo = git.Repo(repo_path)
|
104 |
return repo
|
105 |
|
106 |
-
def integrate_with_git(repo_path, code
|
107 |
"""Integrate the generated code with a Git repository."""
|
108 |
repo = initialize_git_repo(repo_path)
|
109 |
-
|
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([
|
119 |
repo.index.commit("Added generated code")
|
120 |
|
121 |
def process_user_input(user_input):
|
@@ -145,24 +128,16 @@ def run_tests():
|
|
145 |
test_result = test_runner.run(test_suite)
|
146 |
return test_result
|
147 |
|
148 |
-
def execute_code_in_docker(code
|
149 |
"""Execute code in a Docker container for safety and isolation."""
|
150 |
client = docker.from_env()
|
151 |
try:
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
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,16 +175,6 @@ def display_dataframe(data):
|
|
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,7 +270,6 @@ st.title("π Ultra AI Code Assistant")
|
|
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,17 +282,18 @@ if st.button("Generate Code"):
|
|
318 |
if "Error" in completed_text:
|
319 |
handle_error(completed_text)
|
320 |
else:
|
321 |
-
optimized_code = optimize_code(completed_text
|
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(
|
|
|
|
|
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
|
332 |
|
333 |
# Run automated tests
|
334 |
test_result = run_tests()
|
@@ -338,7 +303,7 @@ if st.button("Generate Code"):
|
|
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
|
342 |
if status_code == 0:
|
343 |
st.success("Code executed successfully in Docker!")
|
344 |
st.text(execution_result)
|
|
|
19 |
import matplotlib.pyplot as plt
|
20 |
import seaborn as sns
|
21 |
from IPython.display import display
|
22 |
+
from tenacity import retry, stop_after_attempt, wait_fixed
|
|
|
|
|
23 |
|
24 |
# Configure the Gemini API
|
25 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
|
|
33 |
}
|
34 |
|
35 |
model = genai.GenerativeModel(
|
36 |
+
model_name="gemini-1.5-pro",
|
37 |
generation_config=generation_config,
|
38 |
system_instruction="""
|
39 |
You are Ath, a highly knowledgeable and advanced code assistant. Your responses are optimized for secure, high-quality, and cutting-edge code solutions.
|
|
|
42 |
)
|
43 |
chat_session = model.start_chat(history=[])
|
44 |
|
45 |
+
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
|
46 |
def generate_response(user_input):
|
47 |
+
"""Generate a response from the AI model with retry mechanism."""
|
48 |
try:
|
49 |
response = chat_session.send_message(user_input)
|
50 |
return response.text
|
51 |
except Exception as e:
|
52 |
return f"Error: {e}"
|
53 |
|
54 |
+
def optimize_code(code):
|
55 |
"""Optimize the generated code using static analysis tools."""
|
56 |
+
with open("temp_code.py", "w") as file:
|
57 |
+
file.write(code)
|
58 |
+
result = subprocess.run(["pylint", "temp_code.py"], capture_output=True, text=True)
|
59 |
+
os.remove("temp_code.py")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
return code
|
61 |
|
62 |
def fetch_from_github(query):
|
|
|
93 |
repo = git.Repo(repo_path)
|
94 |
return repo
|
95 |
|
96 |
+
def integrate_with_git(repo_path, code):
|
97 |
"""Integrate the generated code with a Git repository."""
|
98 |
repo = initialize_git_repo(repo_path)
|
99 |
+
with open(os.path.join(repo_path, "generated_code.py"), "w") as file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
file.write(code)
|
101 |
+
repo.index.add(["generated_code.py"])
|
102 |
repo.index.commit("Added generated code")
|
103 |
|
104 |
def process_user_input(user_input):
|
|
|
128 |
test_result = test_runner.run(test_suite)
|
129 |
return test_result
|
130 |
|
131 |
+
def execute_code_in_docker(code):
|
132 |
"""Execute code in a Docker container for safety and isolation."""
|
133 |
client = docker.from_env()
|
134 |
try:
|
135 |
+
container = client.containers.run(
|
136 |
+
image="python:3.9",
|
137 |
+
command=f"python -c '{code}'",
|
138 |
+
detach=True,
|
139 |
+
remove=True
|
140 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
result = container.wait()
|
142 |
logs = container.logs().decode('utf-8')
|
143 |
return logs, result['StatusCode']
|
|
|
175 |
df = pd.DataFrame(data)
|
176 |
display(df)
|
177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
# Streamlit UI setup
|
179 |
st.set_page_config(page_title="Ultra AI Code Assistant", page_icon="π", layout="wide")
|
180 |
|
|
|
270 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
271 |
|
272 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
|
|
273 |
|
274 |
if st.button("Generate Code"):
|
275 |
if prompt.strip() == "":
|
|
|
282 |
if "Error" in completed_text:
|
283 |
handle_error(completed_text)
|
284 |
else:
|
285 |
+
optimized_code = optimize_code(completed_text)
|
286 |
st.success("Code generated and optimized successfully!")
|
287 |
|
|
|
288 |
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
289 |
+
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
290 |
+
st.code(optimized_code)
|
291 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
292 |
st.markdown('</div>', unsafe_allow_html=True)
|
293 |
|
294 |
# Integrate with Git
|
295 |
repo_path = "./repo" # Replace with your repository path
|
296 |
+
integrate_with_git(repo_path, optimized_code)
|
297 |
|
298 |
# Run automated tests
|
299 |
test_result = run_tests()
|
|
|
303 |
st.error("Some tests failed. Please check the code.")
|
304 |
|
305 |
# Execute code in Docker
|
306 |
+
execution_result, status_code = execute_code_in_docker(optimized_code)
|
307 |
if status_code == 0:
|
308 |
st.success("Code executed successfully in Docker!")
|
309 |
st.text(execution_result)
|