Update app.py
Browse files
app.py
CHANGED
@@ -12,29 +12,30 @@ import spacy
|
|
12 |
from spacy.lang.en import English
|
13 |
import boto3
|
14 |
import unittest
|
15 |
-
|
|
|
16 |
from scipy.optimize import minimize
|
17 |
import numpy as np
|
18 |
import matplotlib.pyplot as plt
|
19 |
import seaborn as sns
|
|
|
20 |
|
21 |
# Configure the Gemini API
|
22 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
23 |
|
24 |
# Create the model with optimized parameters and enhanced system instructions
|
25 |
generation_config = {
|
26 |
-
"temperature": 0.
|
27 |
-
"top_p": 0.
|
28 |
-
"top_k":
|
29 |
-
"max_output_tokens":
|
30 |
}
|
31 |
|
32 |
model = genai.GenerativeModel(
|
33 |
-
model_name="gemini-
|
34 |
generation_config=generation_config,
|
35 |
system_instruction="""
|
36 |
-
You are Ath, a highly knowledgeable code assistant.
|
37 |
-
Your responses should contain optimized, secure, and high-quality code only, without explanations. You are designed to provide accurate, efficient, and cutting-edge code solutions.
|
38 |
"""
|
39 |
)
|
40 |
chat_session = model.start_chat(history=[])
|
@@ -111,7 +112,6 @@ def interact_with_cloud_services(service_name, action, params):
|
|
111 |
|
112 |
def run_tests():
|
113 |
"""Run automated tests using unittest."""
|
114 |
-
# Ensure the tests directory is importable
|
115 |
tests_dir = os.path.join(os.getcwd(), 'tests')
|
116 |
if not os.path.exists(tests_dir):
|
117 |
os.makedirs(tests_dir)
|
@@ -125,11 +125,27 @@ def run_tests():
|
|
125 |
test_result = test_runner.run(test_suite)
|
126 |
return test_result
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
def solve_equation(equation):
|
129 |
"""Solve mathematical equations using SymPy."""
|
130 |
-
x, y = symbols('x y')
|
131 |
-
eq = Eq(eval(equation))
|
132 |
-
solution = solve(eq, x)
|
133 |
return solution
|
134 |
|
135 |
def optimize_function(function, initial_guess):
|
@@ -145,8 +161,19 @@ def visualize_data(data):
|
|
145 |
plt.title('Correlation Heatmap')
|
146 |
plt.show()
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
# Streamlit UI setup
|
149 |
-
st.set_page_config(page_title="
|
150 |
|
151 |
st.markdown("""
|
152 |
<style>
|
@@ -158,7 +185,7 @@ st.markdown("""
|
|
158 |
color: #1a202c;
|
159 |
}
|
160 |
.stApp {
|
161 |
-
max-width:
|
162 |
margin: 0 auto;
|
163 |
padding: 2rem;
|
164 |
}
|
@@ -236,7 +263,7 @@ st.markdown("""
|
|
236 |
""", unsafe_allow_html=True)
|
237 |
|
238 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
239 |
-
st.title("
|
240 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
241 |
|
242 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
@@ -271,12 +298,20 @@ if st.button("Generate Code"):
|
|
271 |
st.success("All tests passed successfully!")
|
272 |
else:
|
273 |
st.error("Some tests failed. Please check the code.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
except Exception as e:
|
275 |
handle_error(e)
|
276 |
|
277 |
st.markdown("""
|
278 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
279 |
-
Created with β€οΈ by Your
|
280 |
</div>
|
281 |
""", unsafe_allow_html=True)
|
282 |
|
|
|
12 |
from spacy.lang.en import English
|
13 |
import boto3
|
14 |
import unittest
|
15 |
+
import docker
|
16 |
+
import sympy as sp
|
17 |
from scipy.optimize import minimize
|
18 |
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"])
|
25 |
|
26 |
# Create the model with optimized parameters and enhanced system instructions
|
27 |
generation_config = {
|
28 |
+
"temperature": 0.5, # Lower temperature for more deterministic responses
|
29 |
+
"top_p": 0.7, # Adjusted for better diversity
|
30 |
+
"top_k": 40, # Increased for more diverse tokens
|
31 |
+
"max_output_tokens": 2048, # Increased for longer responses
|
32 |
}
|
33 |
|
34 |
model = genai.GenerativeModel(
|
35 |
+
model_name="gemini-2.0-pro",
|
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=[])
|
|
|
112 |
|
113 |
def run_tests():
|
114 |
"""Run automated tests using unittest."""
|
|
|
115 |
tests_dir = os.path.join(os.getcwd(), 'tests')
|
116 |
if not os.path.exists(tests_dir):
|
117 |
os.makedirs(tests_dir)
|
|
|
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 |
+
container = client.containers.run(
|
133 |
+
image="python:3.9",
|
134 |
+
command=f"python -c '{code}'",
|
135 |
+
detach=True,
|
136 |
+
remove=True
|
137 |
+
)
|
138 |
+
result = container.wait()
|
139 |
+
logs = container.logs().decode('utf-8')
|
140 |
+
return logs, result['StatusCode']
|
141 |
+
except Exception as e:
|
142 |
+
return f"Error: {e}", 1
|
143 |
+
|
144 |
def solve_equation(equation):
|
145 |
"""Solve mathematical equations using SymPy."""
|
146 |
+
x, y = sp.symbols('x y')
|
147 |
+
eq = sp.Eq(eval(equation))
|
148 |
+
solution = sp.solve(eq, x)
|
149 |
return solution
|
150 |
|
151 |
def optimize_function(function, initial_guess):
|
|
|
161 |
plt.title('Correlation Heatmap')
|
162 |
plt.show()
|
163 |
|
164 |
+
def analyze_data(data):
|
165 |
+
"""Perform advanced data analysis using Pandas and NumPy."""
|
166 |
+
df = pd.DataFrame(data)
|
167 |
+
summary = df.describe()
|
168 |
+
return summary
|
169 |
+
|
170 |
+
def display_dataframe(data):
|
171 |
+
"""Display a DataFrame in a user-friendly format."""
|
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 |
|
178 |
st.markdown("""
|
179 |
<style>
|
|
|
185 |
color: #1a202c;
|
186 |
}
|
187 |
.stApp {
|
188 |
+
max-width: 1200px;
|
189 |
margin: 0 auto;
|
190 |
padding: 2rem;
|
191 |
}
|
|
|
263 |
""", unsafe_allow_html=True)
|
264 |
|
265 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
266 |
+
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)
|
|
|
298 |
st.success("All tests passed successfully!")
|
299 |
else:
|
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)
|
307 |
+
else:
|
308 |
+
st.error(f"Code execution failed: {execution_result}")
|
309 |
except Exception as e:
|
310 |
handle_error(e)
|
311 |
|
312 |
st.markdown("""
|
313 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
314 |
+
Created with β€οΈ by Your Ultra AI Code Assistant
|
315 |
</div>
|
316 |
""", unsafe_allow_html=True)
|
317 |
|