Artificial-superintelligence commited on
Commit
921a07f
·
verified ·
1 Parent(s): 4319a04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -101
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="CodeGenius", page_icon="🚀", layout="wide")
31
 
32
  st.markdown("""
33
  <style>
34
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=JetBrains+Mono:wght@400;500&display=swap');
35
 
36
  body {
37
- font-family: 'Inter', sans-serif;
38
- background-color: #f8f9fa;
39
- color: #343a40;
40
  }
41
  .stApp {
42
- max-width: 900px;
43
  margin: 0 auto;
44
  padding: 2rem;
45
  }
46
  .main-container {
47
  background: #ffffff;
48
- border-radius: 12px;
49
- padding: 2rem;
50
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
51
  }
52
  h1 {
53
- font-size: 2.5rem;
54
- font-weight: 600;
55
- color: #212529;
56
  text-align: center;
57
- margin-bottom: 0.5rem;
 
58
  }
59
  .subtitle {
60
- font-size: 1rem;
61
  text-align: center;
62
- color: #6c757d;
63
- margin-bottom: 2rem;
64
  }
65
  .stTextArea textarea {
66
- background-color: #f1f3f5;
67
- color: #495057;
68
- border: 1px solid #ced4da;
69
- border-radius: 6px;
70
  font-size: 1rem;
71
- font-family: 'JetBrains Mono', monospace;
72
- padding: 0.75rem;
73
- transition: all 0.2s ease;
74
  }
75
  .stTextArea textarea:focus {
76
- border-color: #4dabf7;
77
- box-shadow: 0 0 0 2px rgba(77, 171, 247, 0.2);
78
  }
79
  .stButton button {
80
- background-color: #4dabf7;
81
- color: #ffffff;
82
  border: none;
83
- border-radius: 6px;
84
- font-size: 1rem;
85
- font-weight: 500;
86
- padding: 0.6rem 1.2rem;
87
- transition: all 0.2s ease;
 
 
88
  }
89
  .stButton button:hover {
90
- background-color: #3793dd;
91
- transform: translateY(-1px);
 
92
  }
93
  .output-container {
94
- background: #f8f9fa;
95
- border-radius: 6px;
96
  padding: 1.5rem;
97
  margin-top: 2rem;
98
- border: 1px solid #e9ecef;
99
  }
100
  .code-block {
101
- background-color: #f1f3f5;
102
- color: #212529;
103
- font-family: 'JetBrains Mono', monospace;
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: #e9ecef;
112
- color: #495057;
113
- border-radius: 6px;
114
- border: 1px solid #ced4da;
115
- padding: 0.75rem 1rem;
116
  margin-bottom: 1rem;
117
  }
118
  .stSpinner {
119
- color: #4dabf7;
120
  }
121
  /* Custom scrollbar */
122
  ::-webkit-scrollbar {
123
- width: 6px;
124
- height: 6px;
125
  }
126
  ::-webkit-scrollbar-track {
127
- background: #f1f3f5;
128
- border-radius: 3px;
129
  }
130
  ::-webkit-scrollbar-thumb {
131
- background: #adb5bd;
132
- border-radius: 3px;
133
  }
134
  ::-webkit-scrollbar-thumb:hover {
135
- background: #868e96;
136
  }
137
- .language-selector {
138
- margin-bottom: 1rem;
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
- cursor: pointer;
148
- transition: all 0.2s ease;
 
 
 
 
 
149
  }
150
- .copy-button:hover {
151
- background-color: #ced4da;
 
152
  }
153
  </style>
154
  """, unsafe_allow_html=True)
155
 
156
  st.markdown('<div class="main-container">', unsafe_allow_html=True)
157
- st.title("🚀 CodeGenius")
158
- st.markdown('<p class="subtitle">Elevate Your Code with AI-Powered Insights</p>', unsafe_allow_html=True)
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
- col1, col2, col3 = st.columns([2, 1, 1])
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 generate_button:
174
  if prompt.strip() == "":
175
- st.warning("Please enter a valid prompt.")
176
  else:
177
- with st.spinner(f"Crafting {complexity.lower()} complexity {selected_language} solution..."):
178
- completed_text = generate_response(f"{complexity} complexity, {style} {selected_language} code for: {prompt}")
179
  if "An error occurred" in completed_text:
180
  st.error(completed_text)
181
  else:
182
- st.success(f"Solution generated successfully!")
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.code(completed_text, language=selected_language.lower())
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: #adb5bd; font-size: 0.9rem;'>
202
- Powered by CodeGenius AI Simplifying Complex Code Challenges
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