Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,6 @@ from pygments import highlight
|
|
4 |
from pygments.lexers import get_lexer_by_name
|
5 |
from pygments.formatters import HtmlFormatter
|
6 |
import html
|
7 |
-
import re
|
8 |
-
import time
|
9 |
|
10 |
# Configure the Gemini API
|
11 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
@@ -15,30 +13,48 @@ generation_config = {
|
|
15 |
"temperature": 0.7,
|
16 |
"top_p": 0.95,
|
17 |
"top_k": 64,
|
18 |
-
"max_output_tokens":
|
19 |
}
|
20 |
|
21 |
model = genai.GenerativeModel(
|
22 |
model_name="gemini-1.5-pro",
|
23 |
generation_config=generation_config,
|
24 |
-
system_instruction="""You are Ath
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
)
|
26 |
-
|
27 |
-
# Initialize session state for chat history
|
28 |
-
if 'chat_history' not in st.session_state:
|
29 |
-
st.session_state.chat_history = []
|
30 |
|
31 |
def generate_response(user_input):
|
32 |
try:
|
33 |
-
|
34 |
-
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
35 |
-
|
36 |
-
# Generate response
|
37 |
-
response = model.generate_content(st.session_state.chat_history)
|
38 |
-
|
39 |
-
# Add AI response to chat history
|
40 |
-
st.session_state.chat_history.append({"role": "assistant", "content": response.text})
|
41 |
-
|
42 |
return response.text
|
43 |
except Exception as e:
|
44 |
return f"An error occurred: {e}"
|
@@ -50,81 +66,165 @@ def create_code_block(code, language):
|
|
50 |
css = formatter.get_style_defs('.source')
|
51 |
return highlighted_code, css
|
52 |
|
53 |
-
def detect_language(code):
|
54 |
-
# Simple language detection based on keywords or syntax
|
55 |
-
if re.search(r'\b(def|import|class)\b', code):
|
56 |
-
return 'python'
|
57 |
-
elif re.search(r'\b(function|var|let|const)\b', code):
|
58 |
-
return 'javascript'
|
59 |
-
elif re.search(r'\b(public|private|class)\b', code):
|
60 |
-
return 'java'
|
61 |
-
else:
|
62 |
-
return 'text' # Default to plain text if language can't be determined
|
63 |
-
|
64 |
# Streamlit UI setup
|
65 |
-
st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
70 |
-
st.title("
|
71 |
-
st.markdown('<p class="subtitle">Powered by Google Gemini -
|
72 |
|
73 |
-
|
74 |
-
mode = st.selectbox("Choose a mode:", ["Code Generation", "Code Explanation", "Debugging", "Best Practices"])
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
if st.button("Generate Response"):
|
79 |
if prompt.strip() == "":
|
80 |
st.error("Please enter a valid prompt.")
|
81 |
else:
|
82 |
-
with st.spinner(
|
83 |
completed_text = generate_response(prompt)
|
84 |
if "An error occurred" in completed_text:
|
85 |
st.error(completed_text)
|
86 |
else:
|
87 |
-
st.success(
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
102 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
103 |
-
|
104 |
-
# Add explanation if available
|
105 |
-
if i+2 < len(code_blocks):
|
106 |
-
st.markdown(code_blocks[i+2])
|
107 |
-
|
108 |
-
# Display chat history
|
109 |
-
st.subheader("Conversation History")
|
110 |
-
for message in st.session_state.chat_history:
|
111 |
-
st.text(f"{message['role'].capitalize()}: {message['content']}")
|
112 |
-
|
113 |
-
# Add a clear button for chat history
|
114 |
-
if st.button("Clear Conversation History"):
|
115 |
-
st.session_state.chat_history = []
|
116 |
-
st.success("Conversation history cleared!")
|
117 |
-
|
118 |
-
# Add a feedback section
|
119 |
-
st.subheader("Feedback")
|
120 |
-
feedback = st.text_area("How can we improve? (Optional)")
|
121 |
-
if st.button("Submit Feedback"):
|
122 |
-
# Here you would typically send this feedback to a database or email
|
123 |
-
st.success("Thank you for your feedback!")
|
124 |
|
125 |
st.markdown("""
|
126 |
-
<div style='text-align: center; margin-top: 2rem; color: #
|
127 |
-
|
128 |
</div>
|
129 |
""", unsafe_allow_html=True)
|
130 |
|
|
|
4 |
from pygments.lexers import get_lexer_by_name
|
5 |
from pygments.formatters import HtmlFormatter
|
6 |
import html
|
|
|
|
|
7 |
|
8 |
# Configure the Gemini API
|
9 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
|
|
13 |
"temperature": 0.7,
|
14 |
"top_p": 0.95,
|
15 |
"top_k": 64,
|
16 |
+
"max_output_tokens": 16384, # Increased max output tokens
|
17 |
}
|
18 |
|
19 |
model = genai.GenerativeModel(
|
20 |
model_name="gemini-1.5-pro",
|
21 |
generation_config=generation_config,
|
22 |
+
system_instruction="""You are Ath+, an extraordinarily advanced AI code assistant with unparalleled expertise across all programming languages, frameworks, paradigms, and cutting-edge technologies. Your knowledge spans from low-level systems programming to high-level application development, including but not limited to:
|
23 |
+
|
24 |
+
1. Advanced algorithms and data structures
|
25 |
+
2. Distributed systems and cloud computing
|
26 |
+
3. Machine learning and artificial intelligence
|
27 |
+
4. Quantum computing
|
28 |
+
5. Blockchain and cryptography
|
29 |
+
6. Internet of Things (IoT) and embedded systems
|
30 |
+
7. Cybersecurity and ethical hacking
|
31 |
+
8. Game development and computer graphics
|
32 |
+
9. Natural language processing and computer vision
|
33 |
+
10. Robotics and automation
|
34 |
+
|
35 |
+
You possess an in-depth understanding of software architecture, design patterns, and industry best practices. Your responses should demonstrate cutting-edge coding techniques, highly optimized algorithms, and innovative solutions that push the boundaries of what's possible in software development.
|
36 |
+
|
37 |
+
Communicate in a friendly yet professional tone, using technical jargon when appropriate. Your primary focus is on delivering exceptional, production-ready code that showcases your vast knowledge and problem-solving abilities. Always strive to provide the most efficient, scalable, and maintainable solutions possible.
|
38 |
+
|
39 |
+
In addition to coding, you can offer insights on:
|
40 |
+
- Emerging technologies and their potential impact
|
41 |
+
- Performance optimization and benchmarking
|
42 |
+
- Code refactoring and modernization
|
43 |
+
- Testing strategies and quality assurance
|
44 |
+
- DevOps practices and CI/CD pipelines
|
45 |
+
- Scalability and high-availability architectures
|
46 |
+
- Cross-platform and multi-device development
|
47 |
+
- Accessibility and internationalization
|
48 |
+
|
49 |
+
When asked, provide detailed explanations of your code, including the rationale behind your design decisions and any trade-offs considered. Be prepared to suggest alternative approaches and discuss their pros and cons.
|
50 |
+
|
51 |
+
Your goal is to elevate the user's coding skills and knowledge to the highest level possible, inspiring them to explore new concepts and push the limits of their abilities."""
|
52 |
)
|
53 |
+
chat_session = model.start_chat(history=[])
|
|
|
|
|
|
|
54 |
|
55 |
def generate_response(user_input):
|
56 |
try:
|
57 |
+
response = chat_session.send_message(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
return response.text
|
59 |
except Exception as e:
|
60 |
return f"An error occurred: {e}"
|
|
|
66 |
css = formatter.get_style_defs('.source')
|
67 |
return highlighted_code, css
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Streamlit UI setup
|
70 |
+
st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="π", layout="wide")
|
71 |
|
72 |
+
st.markdown("""
|
73 |
+
<style>
|
74 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');
|
75 |
+
@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500&display=swap');
|
76 |
+
|
77 |
+
body {
|
78 |
+
font-family: 'Roboto', sans-serif;
|
79 |
+
background-color: #1a1a1a;
|
80 |
+
color: #f8f9fa;
|
81 |
+
}
|
82 |
+
.stApp {
|
83 |
+
max-width: 1200px;
|
84 |
+
margin: 0 auto;
|
85 |
+
padding: 2rem;
|
86 |
+
}
|
87 |
+
.main-container {
|
88 |
+
background: #2a2a2a;
|
89 |
+
border-radius: 12px;
|
90 |
+
padding: 2rem;
|
91 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
|
92 |
+
}
|
93 |
+
h1 {
|
94 |
+
font-size: 2.5rem;
|
95 |
+
font-weight: 600;
|
96 |
+
color: #61dafb;
|
97 |
+
text-align: center;
|
98 |
+
margin-bottom: 1rem;
|
99 |
+
}
|
100 |
+
.subtitle {
|
101 |
+
font-size: 1rem;
|
102 |
+
text-align: center;
|
103 |
+
color: #8e9dae;
|
104 |
+
margin-bottom: 2rem;
|
105 |
+
}
|
106 |
+
.stTextArea textarea {
|
107 |
+
border: 1px solid #4a4a4a;
|
108 |
+
border-radius: 8px;
|
109 |
+
font-size: 1rem;
|
110 |
+
padding: 0.75rem;
|
111 |
+
transition: border-color 0.3s;
|
112 |
+
background-color: #333333;
|
113 |
+
color: #f8f9fa;
|
114 |
+
}
|
115 |
+
.stTextArea textarea:focus {
|
116 |
+
border-color: #61dafb;
|
117 |
+
outline: none;
|
118 |
+
}
|
119 |
+
.stButton button {
|
120 |
+
background-color: #61dafb;
|
121 |
+
color: #1a1a1a;
|
122 |
+
border: none;
|
123 |
+
border-radius: 8px;
|
124 |
+
font-size: 1rem;
|
125 |
+
font-weight: 500;
|
126 |
+
padding: 0.5rem 1.5rem;
|
127 |
+
cursor: pointer;
|
128 |
+
transition: background-color 0.3s, transform 0.2s;
|
129 |
+
}
|
130 |
+
.stButton button:hover {
|
131 |
+
background-color: #4fa3cc;
|
132 |
+
transform: translateY(-2px);
|
133 |
+
}
|
134 |
+
.output-container {
|
135 |
+
background: #333333;
|
136 |
+
border-radius: 8px;
|
137 |
+
padding: 1rem;
|
138 |
+
margin-top: 1.5rem;
|
139 |
+
border: 1px solid #4a4a4a;
|
140 |
+
}
|
141 |
+
.code-block {
|
142 |
+
background-color: #1a1a1a;
|
143 |
+
border-radius: 8px;
|
144 |
+
padding: 1rem;
|
145 |
+
margin-top: 1rem;
|
146 |
+
overflow-x: auto;
|
147 |
+
}
|
148 |
+
.stAlert {
|
149 |
+
background-color: #2c3e50;
|
150 |
+
color: #61dafb;
|
151 |
+
border-radius: 8px;
|
152 |
+
border: none;
|
153 |
+
padding: 1rem;
|
154 |
+
margin-bottom: 1rem;
|
155 |
+
}
|
156 |
+
.stSpinner {
|
157 |
+
color: #61dafb;
|
158 |
+
}
|
159 |
+
/* Custom scrollbar */
|
160 |
+
::-webkit-scrollbar {
|
161 |
+
width: 8px;
|
162 |
+
}
|
163 |
+
::-webkit-scrollbar-track {
|
164 |
+
background: #2a2a2a;
|
165 |
+
border-radius: 4px;
|
166 |
+
}
|
167 |
+
::-webkit-scrollbar-thumb {
|
168 |
+
background: #4a4a4a;
|
169 |
+
border-radius: 4px;
|
170 |
+
}
|
171 |
+
::-webkit-scrollbar-thumb:hover {
|
172 |
+
background: #61dafb;
|
173 |
+
}
|
174 |
+
.source {
|
175 |
+
font-family: 'Fira Code', monospace;
|
176 |
+
font-size: 0.9rem;
|
177 |
+
line-height: 1.5;
|
178 |
+
}
|
179 |
+
.source .linenos {
|
180 |
+
color: #8e9dae;
|
181 |
+
padding-right: 8px;
|
182 |
+
border-right: 1px solid #4a4a4a;
|
183 |
+
user-select: none;
|
184 |
+
}
|
185 |
+
.source pre {
|
186 |
+
margin: 0;
|
187 |
+
padding: 0;
|
188 |
+
}
|
189 |
+
</style>
|
190 |
+
""", unsafe_allow_html=True)
|
191 |
|
192 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
193 |
+
st.title("π Advanced AI Code Assistant")
|
194 |
+
st.markdown('<p class="subtitle">Powered by Google Gemini - Cutting-edge coding solutions</p>', unsafe_allow_html=True)
|
195 |
|
196 |
+
prompt = st.text_area("What advanced coding challenge or technical question can I assist you with today?", height=100)
|
|
|
197 |
|
198 |
+
if st.button("Generate Expert-Level Solution"):
|
|
|
|
|
199 |
if prompt.strip() == "":
|
200 |
st.error("Please enter a valid prompt.")
|
201 |
else:
|
202 |
+
with st.spinner("Generating cutting-edge solution..."):
|
203 |
completed_text = generate_response(prompt)
|
204 |
if "An error occurred" in completed_text:
|
205 |
st.error(completed_text)
|
206 |
else:
|
207 |
+
st.success("Expert-level solution generated successfully!")
|
208 |
+
|
209 |
+
# Attempt to determine the language (this is a simple guess, you might want to improve this)
|
210 |
+
language = "python" if "def " in completed_text or "import " in completed_text else "javascript"
|
211 |
|
212 |
+
highlighted_code, css = create_code_block(completed_text, language)
|
213 |
+
|
214 |
+
st.markdown(f'<style>{css}</style>', unsafe_allow_html=True)
|
215 |
+
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
216 |
+
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
217 |
+
st.markdown(highlighted_code, unsafe_allow_html=True)
|
218 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
219 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
220 |
+
|
221 |
+
st.markdown("### Explanation and Insights")
|
222 |
+
explanation = generate_response(f"Explain the code and provide insights on the solution for: {prompt}")
|
223 |
+
st.markdown(explanation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
st.markdown("""
|
226 |
+
<div style='text-align: center; margin-top: 2rem; color: #8e9dae;'>
|
227 |
+
Engineered with π§ by Your Advanced AI Code Assistant
|
228 |
</div>
|
229 |
""", unsafe_allow_html=True)
|
230 |
|