|
import streamlit as st |
|
import google.generativeai as genai |
|
import requests |
|
import subprocess |
|
import os |
|
import pylint |
|
import pandas as pd |
|
import numpy as np |
|
from sklearn.model_selection import train_test_split, GridSearchCV |
|
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier |
|
from sklearn.metrics import (accuracy_score, precision_score, |
|
recall_score, f1_score, confusion_matrix) |
|
import git |
|
import spacy |
|
from spacy.lang.en import English |
|
import boto3 |
|
import unittest |
|
import docker |
|
import sympy as sp |
|
from scipy.optimize import minimize, differential_evolution |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
from IPython.display import display |
|
from tenacity import retry, stop_after_attempt, wait_fixed |
|
import torch |
|
import torch.nn as nn |
|
import torch.optim as optim |
|
from transformers import (AutoTokenizer, AutoModel, |
|
pipeline, set_seed) |
|
import networkx as nx |
|
from sklearn.cluster import KMeans |
|
from scipy.stats import ttest_ind |
|
from statsmodels.tsa.arima.model import ARIMA |
|
import nltk |
|
from nltk.sentiment import SentimentIntensityAnalyzer |
|
import cv2 |
|
from PIL import Image |
|
import tensorflow as tf |
|
from tensorflow.keras.applications import ResNet50 |
|
from tensorflow.keras.preprocessing import image |
|
from tensorflow.keras.applications.resnet50 import preprocess_input |
|
import logging |
|
from logging.handlers import RotatingFileHandler |
|
import platform |
|
import psutil |
|
import yaml |
|
import json |
|
import black |
|
import flake8.main.application |
|
|
|
|
|
nltk.download('punkt') |
|
nltk.download('vader_lexicon') |
|
|
|
|
|
log_handler = RotatingFileHandler('app.log', maxBytes=1e6, backupCount=5) |
|
logging.basicConfig( |
|
handlers=[log_handler], |
|
level=logging.INFO, |
|
format='%(asctime)s - %(levelname)s - %(message)s' |
|
) |
|
|
|
|
|
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"]) |
|
|
|
|
|
SYSTEM_INSTRUCTIONS = """ |
|
You are Ath, an ultra-advanced AI code assistant with expertise across multiple domains. Follow these guidelines: |
|
1. Generate secure, efficient, and maintainable code |
|
2. Implement industry best practices and design patterns |
|
3. Include proper error handling and logging |
|
4. Optimize for performance and scalability |
|
5. Add detailed documentation and type hints |
|
6. Suggest relevant libraries and frameworks |
|
7. Consider security implications and vulnerabilities |
|
8. Provide test cases and benchmarking |
|
9. Support multiple programming languages when applicable |
|
10. Follow PEP8 and other relevant style guides |
|
""" |
|
|
|
|
|
generation_config = { |
|
"temperature": 0.35, |
|
"top_p": 0.85, |
|
"top_k": 40, |
|
"max_output_tokens": 8192, |
|
} |
|
|
|
model = genai.GenerativeModel( |
|
model_name="gemini-1.5-pro", |
|
generation_config=generation_config, |
|
system_instruction=SYSTEM_INSTRUCTIONS |
|
) |
|
chat_session = model.start_chat(history=[]) |
|
|
|
@retry(stop=stop_after_attempt(5), wait=wait_fixed(2)) |
|
def generate_response(user_input): |
|
try: |
|
response = chat_session.send_message(user_input) |
|
return response.text |
|
except Exception as e: |
|
logging.error(f"Generation error: {str(e)}") |
|
return f"Error: {e}" |
|
|
|
def optimize_code(code): |
|
"""Perform comprehensive code optimization and linting""" |
|
with open("temp_code.py", "w") as file: |
|
file.write(code) |
|
|
|
|
|
tools = { |
|
'pylint': ["pylint", "temp_code.py"], |
|
'flake8': ["flake8", "temp_code.py"], |
|
'black': ["black", "--check", "temp_code.py"] |
|
} |
|
|
|
results = {} |
|
for tool, cmd in tools.items(): |
|
result = subprocess.run(cmd, capture_output=True, text=True) |
|
results[tool] = { |
|
'output': result.stdout + result.stderr, |
|
'status': result.returncode |
|
} |
|
|
|
|
|
try: |
|
formatted_code = black.format_file_contents( |
|
code, mode=black.FileMode() |
|
) |
|
code = formatted_code |
|
except Exception as e: |
|
logging.warning(f"Black formatting failed: {str(e)}") |
|
|
|
os.remove("temp_code.py") |
|
return code, results |
|
|
|
def train_advanced_ml_model(X, y): |
|
"""Enhanced ML training with hyperparameter tuning""" |
|
X_train, X_test, y_train, y_test = train_test_split( |
|
X, y, test_size=0.2, stratify=y |
|
) |
|
|
|
param_grid = { |
|
'RandomForest': { |
|
'n_estimators': [100, 200], |
|
'max_depth': [None, 10, 20], |
|
'min_samples_split': [2, 5] |
|
}, |
|
'GradientBoosting': { |
|
'n_estimators': [100, 200], |
|
'learning_rate': [0.1, 0.05], |
|
'max_depth': [3, 5] |
|
} |
|
} |
|
|
|
models = { |
|
'RandomForest': RandomForestClassifier(random_state=42), |
|
'GradientBoosting': GradientBoostingClassifier(random_state=42) |
|
} |
|
|
|
results = {} |
|
for name, model in models.items(): |
|
grid_search = GridSearchCV( |
|
model, |
|
param_grid[name], |
|
cv=5, |
|
n_jobs=-1, |
|
scoring='f1_weighted' |
|
) |
|
grid_search.fit(X_train, y_train) |
|
|
|
best_model = grid_search.best_estimator_ |
|
y_pred = best_model.predict(X_test) |
|
|
|
results[name] = { |
|
'best_params': grid_search.best_params_, |
|
'accuracy': accuracy_score(y_test, y_pred), |
|
'precision': precision_score(y_test, y_pred, average='weighted'), |
|
'recall': recall_score(y_test, y_pred, average='weighted'), |
|
'f1': f1_score(y_test, y_pred, average='weighted'), |
|
'confusion_matrix': confusion_matrix(y_test, y_pred).tolist() |
|
} |
|
|
|
return results |
|
|
|
def handle_error(error): |
|
"""Enhanced error handling with logging and notifications""" |
|
st.error(f"An error occurred: {error}") |
|
logging.error(f"User-facing error: {str(error)}") |
|
|
|
|
|
try: |
|
if st.secrets.get("AWS_CREDENTIALS"): |
|
client = boto3.client( |
|
'sns', |
|
aws_access_key_id=st.secrets["AWS_CREDENTIALS"]["access_key"], |
|
aws_secret_access_key=st.secrets["AWS_CREDENTIALS"]["secret_key"], |
|
region_name='us-east-1' |
|
) |
|
client.publish( |
|
TopicArn=st.secrets["AWS_CREDENTIALS"]["sns_topic"], |
|
Message=f"Code Assistant Error: {str(error)}" |
|
) |
|
except Exception as e: |
|
logging.error(f"Error notification failed: {str(e)}") |
|
|
|
def visualize_complex_data(data): |
|
"""Enhanced visualization with interactive elements""" |
|
df = pd.DataFrame(data) |
|
|
|
|
|
fig = px.scatter_matrix(df) |
|
fig.update_layout( |
|
title='Interactive Scatter Matrix', |
|
width=1200, |
|
height=800 |
|
) |
|
|
|
|
|
if df.shape[1] >= 3: |
|
fig_3d = px.scatter_3d( |
|
df, |
|
x=df.columns[0], |
|
y=df.columns[1], |
|
z=df.columns[2], |
|
title='3D Data Visualization' |
|
) |
|
return [fig, fig_3d] |
|
|
|
return [fig] |
|
|
|
def perform_nlp_analysis(text): |
|
"""Enhanced NLP analysis with transformer models""" |
|
|
|
nlp = spacy.load("en_core_web_trf") |
|
doc = nlp(text) |
|
|
|
|
|
sentiment_analyzer = pipeline( |
|
"sentiment-analysis", |
|
model="distilbert-base-uncased-finetuned-sst-2-english" |
|
) |
|
|
|
|
|
summarizer = pipeline("summarization", model="t5-small") |
|
|
|
return { |
|
'entities': [(ent.text, ent.label_) for ent in doc.ents], |
|
'syntax': [(token.text, token.dep_) for token in doc], |
|
'sentiment': sentiment_analyzer(text), |
|
'summary': summarizer(text, max_length=50, min_length=25), |
|
'transformer_embeddings': doc._.trf_data.tensors[-1].tolist() |
|
} |
|
|
|
|
|
st.set_page_config( |
|
page_title="Ultra AI Code Assistant Pro", |
|
page_icon="🚀", |
|
layout="wide", |
|
initial_sidebar_state="expanded" |
|
) |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.main-container { |
|
background-color: #f8f9fa; |
|
padding: 2rem; |
|
border-radius: 10px; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
} |
|
.code-block { |
|
background-color: #1e1e1e; |
|
color: #d4d4d4; |
|
padding: 1rem; |
|
border-radius: 5px; |
|
margin: 1rem 0; |
|
font-family: 'Fira Code', monospace; |
|
} |
|
.stButton>button { |
|
background: linear-gradient(45deg, #4CAF50, #45a049); |
|
color: white; |
|
border: none; |
|
padding: 0.8rem 1.5rem; |
|
border-radius: 25px; |
|
font-weight: bold; |
|
transition: transform 0.2s; |
|
} |
|
.stButton>button:hover { |
|
transform: scale(1.05); |
|
} |
|
.feature-card { |
|
background: white; |
|
padding: 1.5rem; |
|
border-radius: 10px; |
|
margin: 1rem 0; |
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.title("🚀 Ultra AI Code Assistant Pro") |
|
st.markdown(""" |
|
<div class="main-container"> |
|
<p class="subtitle">Next-Generation AI-Powered Development Environment</p> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
main_col, sidebar_col = st.columns([3, 1]) |
|
|
|
with main_col: |
|
task_type = st.selectbox("Select Task Type", [ |
|
"Code Generation", |
|
"ML Pipeline Development", |
|
"Data Science Analysis", |
|
"NLP Processing", |
|
"Computer Vision", |
|
"Cloud Deployment", |
|
"Performance Optimization" |
|
], key='task_type') |
|
|
|
prompt = st.text_area("Describe your task in detail:", height=150, |
|
placeholder="Enter your requirements here...") |
|
|
|
if st.button("Generate Solution", key="main_generate"): |
|
if not prompt.strip(): |
|
st.error("Please provide detailed requirements") |
|
else: |
|
with st.spinner("Analyzing requirements and generating solution..."): |
|
try: |
|
|
|
processed_input = process_user_input(prompt) |
|
response = generate_response(f""" |
|
Generate comprehensive solution for: {processed_input.text} |
|
Include: |
|
- Architecture design |
|
- Implementation code |
|
- Testing strategy |
|
- Deployment plan |
|
- Monitoring setup |
|
""") |
|
|
|
if "Error" in response: |
|
handle_error(response) |
|
else: |
|
optimized_code, lint_results = optimize_code(response) |
|
|
|
|
|
tab1, tab2, tab3 = st.tabs(["Solution", "Analysis", "Deployment"]) |
|
|
|
with tab1: |
|
st.subheader("Optimized Solution") |
|
st.code(optimized_code, language='python') |
|
|
|
col1, col2 = st.columns(2) |
|
with col1: |
|
st.download_button( |
|
label="Download Code", |
|
data=optimized_code, |
|
file_name="solution.py", |
|
mime="text/python" |
|
) |
|
with col2: |
|
if st.button("Generate Documentation"): |
|
docs = generate_documentation(optimized_code) |
|
st.markdown(docs) |
|
|
|
with tab2: |
|
st.subheader("Code Quality Report") |
|
for tool, result in lint_results.items(): |
|
with st.expander(f"{tool.upper()} Results"): |
|
st.code(result['output']) |
|
|
|
st.subheader("Performance Metrics") |
|
|
|
|
|
with tab3: |
|
st.subheader("Cloud Deployment Options") |
|
|
|
|
|
except Exception as e: |
|
handle_error(e) |
|
|
|
with sidebar_col: |
|
st.markdown("## Quick Tools") |
|
|
|
if st.button("Code Review"): |
|
|
|
pass |
|
|
|
if st.button("Security Scan"): |
|
|
|
pass |
|
|
|
st.markdown("## Project Stats") |
|
|
|
st.write(f"CPU Usage: {psutil.cpu_percent()}%") |
|
st.write(f"Memory Usage: {psutil.virtual_memory().percent}%") |
|
|
|
st.markdown("## Recent Activity") |
|
|
|
st.write("No recent activity") |
|
|
|
|
|
st.markdown("## Advanced Features") |
|
features = st.columns(3) |
|
|
|
with features[0]: |
|
with st.expander("Live Collaboration"): |
|
st.write("Real-time collaborative coding features") |
|
|
|
|
|
with features[1]: |
|
with st.expander("API Generator"): |
|
st.write("Generate REST API endpoints from code") |
|
|
|
|
|
with features[2]: |
|
with st.expander("ML Ops"): |
|
st.write("Machine Learning Operations Dashboard") |
|
|
|
|
|
|
|
st.markdown("## System Health Monitor") |
|
sys_cols = st.columns(4) |
|
sys_cols[0].metric("CPU Load", f"{psutil.cpu_percent()}%") |
|
sys_cols[1].metric("Memory", f"{psutil.virtual_memory().percent}%") |
|
sys_cols[2].metric("Disk", f"{psutil.disk_usage('/').percent}%") |
|
sys_cols[3].metric("Network", f"{psutil.net_io_counters().bytes_sent/1e6:.2f}MB") |
|
|
|
|
|
st.markdown(""" |
|
<hr> |
|
<div style="text-align: center; padding: 1rem"> |
|
<p>Ultra AI Code Assistant Pro v2.0</p> |
|
<small>Powered by Gemini 1.5 Pro | Secure and Compliant</small> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|