Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Configure the Gemini API
|
5 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
@@ -26,180 +30,170 @@ def generate_response(user_input):
|
|
26 |
except Exception as e:
|
27 |
return f"An error occurred: {e}"
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Streamlit UI setup
|
30 |
-
st.set_page_config(page_title="
|
31 |
|
32 |
st.markdown("""
|
33 |
<style>
|
34 |
-
@import url('https://fonts.googleapis.com/css2?family=
|
35 |
|
36 |
body {
|
37 |
-
font-family: '
|
38 |
-
background-color: #
|
39 |
-
color: #
|
40 |
}
|
41 |
.stApp {
|
42 |
-
max-width:
|
43 |
margin: 0 auto;
|
44 |
padding: 2rem;
|
45 |
}
|
46 |
.main-container {
|
47 |
background: #ffffff;
|
48 |
-
border-radius:
|
49 |
-
padding:
|
50 |
-
box-shadow: 0
|
51 |
}
|
52 |
h1 {
|
53 |
-
font-size:
|
54 |
-
font-weight:
|
55 |
-
color: #
|
56 |
text-align: center;
|
57 |
-
margin-bottom:
|
|
|
58 |
}
|
59 |
.subtitle {
|
60 |
-
font-size:
|
61 |
text-align: center;
|
62 |
-
color: #
|
63 |
-
margin-bottom:
|
64 |
}
|
65 |
.stTextArea textarea {
|
66 |
-
|
67 |
-
|
68 |
-
border: 1px solid #ced4da;
|
69 |
-
border-radius: 6px;
|
70 |
font-size: 1rem;
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
}
|
75 |
.stTextArea textarea:focus {
|
76 |
-
border-color: #
|
77 |
-
box-shadow: 0 0 0
|
78 |
}
|
79 |
.stButton button {
|
80 |
-
background-color: #
|
81 |
-
color:
|
82 |
border: none;
|
83 |
-
border-radius:
|
84 |
-
font-size: 1rem;
|
85 |
-
font-weight:
|
86 |
-
padding: 0.
|
87 |
-
transition: all 0.
|
|
|
|
|
88 |
}
|
89 |
.stButton button:hover {
|
90 |
-
background-color: #
|
91 |
-
transform: translateY(-
|
|
|
92 |
}
|
93 |
.output-container {
|
94 |
-
background: #
|
95 |
-
border-radius:
|
96 |
padding: 1.5rem;
|
97 |
margin-top: 2rem;
|
98 |
-
border: 1px solid #
|
99 |
}
|
100 |
.code-block {
|
101 |
-
background-color: #
|
102 |
-
|
103 |
-
|
104 |
-
font-size: 0.9rem;
|
105 |
-
border-radius: 6px;
|
106 |
-
padding: 1.25rem;
|
107 |
margin-top: 1rem;
|
108 |
overflow-x: auto;
|
109 |
}
|
110 |
.stAlert {
|
111 |
-
background-color: #
|
112 |
-
color: #
|
113 |
-
border-radius:
|
114 |
-
border:
|
115 |
-
padding:
|
116 |
margin-bottom: 1rem;
|
117 |
}
|
118 |
.stSpinner {
|
119 |
-
color: #
|
120 |
}
|
121 |
/* Custom scrollbar */
|
122 |
::-webkit-scrollbar {
|
123 |
-
width:
|
124 |
-
height:
|
125 |
}
|
126 |
::-webkit-scrollbar-track {
|
127 |
-
background: #
|
128 |
-
border-radius:
|
129 |
}
|
130 |
::-webkit-scrollbar-thumb {
|
131 |
-
background: #
|
132 |
-
border-radius:
|
133 |
}
|
134 |
::-webkit-scrollbar-thumb:hover {
|
135 |
-
background: #
|
136 |
}
|
137 |
-
.
|
138 |
-
|
139 |
-
}
|
140 |
-
.copy-button {
|
141 |
-
background-color: #e9ecef;
|
142 |
-
color: #495057;
|
143 |
-
border: none;
|
144 |
-
border-radius: 4px;
|
145 |
-
padding: 0.4rem 0.8rem;
|
146 |
font-size: 0.9rem;
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
149 |
}
|
150 |
-
.
|
151 |
-
|
|
|
152 |
}
|
153 |
</style>
|
154 |
""", unsafe_allow_html=True)
|
155 |
|
156 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
157 |
-
st.title("
|
158 |
-
st.markdown('<p class="subtitle">
|
159 |
-
|
160 |
-
languages = ["Python", "JavaScript", "Java", "C++", "Ruby", "Go", "Rust", "TypeScript", "PHP", "Swift"]
|
161 |
-
selected_language = st.selectbox("Select your programming language:", languages, key="language-selector")
|
162 |
-
|
163 |
-
prompt = st.text_area(f"What {selected_language} challenge can I help you with?", height=100)
|
164 |
|
165 |
-
|
166 |
-
with col1:
|
167 |
-
generate_button = st.button("Generate Solution")
|
168 |
-
with col2:
|
169 |
-
complexity = st.radio("Complexity", ["Low", "Medium", "High"], horizontal=True)
|
170 |
-
with col3:
|
171 |
-
style = st.radio("Style", ["Concise", "Detailed"], horizontal=True)
|
172 |
|
173 |
-
if
|
174 |
if prompt.strip() == "":
|
175 |
-
st.
|
176 |
else:
|
177 |
-
with st.spinner(
|
178 |
-
completed_text = generate_response(
|
179 |
if "An error occurred" in completed_text:
|
180 |
st.error(completed_text)
|
181 |
else:
|
182 |
-
st.success(
|
183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
185 |
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
186 |
-
st.
|
187 |
st.markdown('</div>', unsafe_allow_html=True)
|
188 |
-
|
189 |
-
# Add copy to clipboard button
|
190 |
-
st.markdown(
|
191 |
-
f"""
|
192 |
-
<button onclick="navigator.clipboard.writeText(`{completed_text}`)" class="copy-button">
|
193 |
-
Copy to Clipboard
|
194 |
-
</button>
|
195 |
-
""",
|
196 |
-
unsafe_allow_html=True
|
197 |
-
)
|
198 |
st.markdown('</div>', unsafe_allow_html=True)
|
199 |
|
200 |
st.markdown("""
|
201 |
-
<div style='text-align: center; margin-top: 2rem; color: #
|
202 |
-
|
203 |
</div>
|
204 |
""", unsafe_allow_html=True)
|
205 |
|
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
+
from pygments import highlight
|
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"])
|
|
|
30 |
except Exception as e:
|
31 |
return f"An error occurred: {e}"
|
32 |
|
33 |
+
def create_code_block(code, language):
|
34 |
+
lexer = get_lexer_by_name(language, stripall=True)
|
35 |
+
formatter = HtmlFormatter(style="monokai", linenos=True, cssclass="source")
|
36 |
+
highlighted_code = highlight(code, lexer, formatter)
|
37 |
+
css = formatter.get_style_defs('.source')
|
38 |
+
return highlighted_code, css
|
39 |
+
|
40 |
# Streamlit UI setup
|
41 |
+
st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="💻", layout="wide")
|
42 |
|
43 |
st.markdown("""
|
44 |
<style>
|
45 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
|
46 |
|
47 |
body {
|
48 |
+
font-family: 'Poppins', sans-serif;
|
49 |
+
background-color: #f0f7ff;
|
50 |
+
color: #333;
|
51 |
}
|
52 |
.stApp {
|
53 |
+
max-width: 1200px;
|
54 |
margin: 0 auto;
|
55 |
padding: 2rem;
|
56 |
}
|
57 |
.main-container {
|
58 |
background: #ffffff;
|
59 |
+
border-radius: 20px;
|
60 |
+
padding: 3rem;
|
61 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
|
62 |
}
|
63 |
h1 {
|
64 |
+
font-size: 3rem;
|
65 |
+
font-weight: 700;
|
66 |
+
color: #1e3a8a;
|
67 |
text-align: center;
|
68 |
+
margin-bottom: 1rem;
|
69 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
70 |
}
|
71 |
.subtitle {
|
72 |
+
font-size: 1.2rem;
|
73 |
text-align: center;
|
74 |
+
color: #64748b;
|
75 |
+
margin-bottom: 3rem;
|
76 |
}
|
77 |
.stTextArea textarea {
|
78 |
+
border: 2px solid #cbd5e1;
|
79 |
+
border-radius: 12px;
|
|
|
|
|
80 |
font-size: 1rem;
|
81 |
+
padding: 1rem;
|
82 |
+
transition: all 0.3s ease;
|
83 |
+
box-shadow: inset 0 2px 4px rgba(0,0,0,0.05);
|
84 |
}
|
85 |
.stTextArea textarea:focus {
|
86 |
+
border-color: #3b82f6;
|
87 |
+
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
|
88 |
}
|
89 |
.stButton button {
|
90 |
+
background-color: #3b82f6;
|
91 |
+
color: white;
|
92 |
border: none;
|
93 |
+
border-radius: 12px;
|
94 |
+
font-size: 1.1rem;
|
95 |
+
font-weight: 600;
|
96 |
+
padding: 0.75rem 2rem;
|
97 |
+
transition: all 0.3s ease;
|
98 |
+
width: 100%;
|
99 |
+
box-shadow: 0 4px 6px rgba(59, 130, 246, 0.3);
|
100 |
}
|
101 |
.stButton button:hover {
|
102 |
+
background-color: #2563eb;
|
103 |
+
transform: translateY(-2px);
|
104 |
+
box-shadow: 0 6px 8px rgba(59, 130, 246, 0.4);
|
105 |
}
|
106 |
.output-container {
|
107 |
+
background: #f8fafc;
|
108 |
+
border-radius: 12px;
|
109 |
padding: 1.5rem;
|
110 |
margin-top: 2rem;
|
111 |
+
border: 1px solid #e2e8f0;
|
112 |
}
|
113 |
.code-block {
|
114 |
+
background-color: #272822;
|
115 |
+
border-radius: 12px;
|
116 |
+
padding: 1.5rem;
|
|
|
|
|
|
|
117 |
margin-top: 1rem;
|
118 |
overflow-x: auto;
|
119 |
}
|
120 |
.stAlert {
|
121 |
+
background-color: #dbeafe;
|
122 |
+
color: #1e40af;
|
123 |
+
border-radius: 12px;
|
124 |
+
border: none;
|
125 |
+
padding: 1rem 1.5rem;
|
126 |
margin-bottom: 1rem;
|
127 |
}
|
128 |
.stSpinner {
|
129 |
+
color: #3b82f6;
|
130 |
}
|
131 |
/* Custom scrollbar */
|
132 |
::-webkit-scrollbar {
|
133 |
+
width: 8px;
|
134 |
+
height: 8px;
|
135 |
}
|
136 |
::-webkit-scrollbar-track {
|
137 |
+
background: #f1f5f9;
|
138 |
+
border-radius: 4px;
|
139 |
}
|
140 |
::-webkit-scrollbar-thumb {
|
141 |
+
background: #94a3b8;
|
142 |
+
border-radius: 4px;
|
143 |
}
|
144 |
::-webkit-scrollbar-thumb:hover {
|
145 |
+
background: #64748b;
|
146 |
}
|
147 |
+
.source {
|
148 |
+
font-family: 'Fira Code', monospace;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
font-size: 0.9rem;
|
150 |
+
line-height: 1.4;
|
151 |
+
}
|
152 |
+
.source .linenos {
|
153 |
+
color: #75715e;
|
154 |
+
padding-right: 10px;
|
155 |
+
border-right: 1px solid #49483e;
|
156 |
+
user-select: none;
|
157 |
}
|
158 |
+
.source pre {
|
159 |
+
margin: 0;
|
160 |
+
padding: 0;
|
161 |
}
|
162 |
</style>
|
163 |
""", unsafe_allow_html=True)
|
164 |
|
165 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
166 |
+
st.title("💻 Advanced AI Code Assistant")
|
167 |
+
st.markdown('<p class="subtitle">Powered by Google Gemini - Expert-level coding solutions</p>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
+
prompt = st.text_area("What advanced coding challenge can I assist you with today?", height=120)
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
+
if st.button("Generate Expert Code"):
|
172 |
if prompt.strip() == "":
|
173 |
+
st.error("Please enter a valid prompt.")
|
174 |
else:
|
175 |
+
with st.spinner("Generating advanced code solution..."):
|
176 |
+
completed_text = generate_response(prompt)
|
177 |
if "An error occurred" in completed_text:
|
178 |
st.error(completed_text)
|
179 |
else:
|
180 |
+
st.success("Expert-level code generated successfully!")
|
181 |
|
182 |
+
# Attempt to determine the language (this is a simple guess, you might want to improve this)
|
183 |
+
language = "python" if "def " in completed_text or "import " in completed_text else "javascript"
|
184 |
+
|
185 |
+
highlighted_code, css = create_code_block(completed_text, language)
|
186 |
+
|
187 |
+
st.markdown(f'<style>{css}</style>', unsafe_allow_html=True)
|
188 |
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
189 |
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
190 |
+
st.markdown(highlighted_code, unsafe_allow_html=True)
|
191 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
st.markdown('</div>', unsafe_allow_html=True)
|
193 |
|
194 |
st.markdown("""
|
195 |
+
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
196 |
+
Crafted with ❤️ by Your Advanced AI Code Assistant
|
197 |
</div>
|
198 |
""", unsafe_allow_html=True)
|
199 |
|