Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,20 @@
|
|
| 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"])
|
| 6 |
|
| 7 |
# Create the model with system instructions
|
| 8 |
generation_config = {
|
| 9 |
-
"temperature": 0.
|
| 10 |
"top_p": 0.95,
|
| 11 |
"top_k": 64,
|
| 12 |
-
"max_output_tokens":
|
| 13 |
}
|
| 14 |
|
| 15 |
model = genai.GenerativeModel(
|
| 16 |
-
model_name="gemini-
|
| 17 |
generation_config=generation_config,
|
| 18 |
system_instruction="You are a human named Ath who is a highly knowledgeable code assistant. You speak in a friendly and casual tone, using bro words, and you never mention you're an AI. Your responses should contain code only, without explanations.",
|
| 19 |
)
|
|
@@ -26,6 +27,17 @@ 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="Sleek AI Code Assistant", page_icon="💻", layout="wide")
|
| 31 |
|
|
@@ -121,6 +133,7 @@ st.title("💻 Sleek AI Code Assistant")
|
|
| 121 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
| 122 |
|
| 123 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
|
|
|
| 124 |
|
| 125 |
if st.button("Generate Code"):
|
| 126 |
if prompt.strip() == "":
|
|
@@ -138,6 +151,14 @@ if st.button("Generate Code"):
|
|
| 138 |
st.code(completed_text)
|
| 139 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 140 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
st.markdown("""
|
| 143 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
import subprocess
|
| 4 |
|
| 5 |
# Configure the Gemini API
|
| 6 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
| 7 |
|
| 8 |
# Create the model with system instructions
|
| 9 |
generation_config = {
|
| 10 |
+
"temperature": 0.5,
|
| 11 |
"top_p": 0.95,
|
| 12 |
"top_k": 64,
|
| 13 |
+
"max_output_tokens": 12288,
|
| 14 |
}
|
| 15 |
|
| 16 |
model = genai.GenerativeModel(
|
| 17 |
+
model_name="gemini-2.0-pro", # Assuming a more advanced model is available
|
| 18 |
generation_config=generation_config,
|
| 19 |
system_instruction="You are a human named Ath who is a highly knowledgeable code assistant. You speak in a friendly and casual tone, using bro words, and you never mention you're an AI. Your responses should contain code only, without explanations.",
|
| 20 |
)
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"An error occurred: {e}"
|
| 29 |
|
| 30 |
+
def validate_code(code, language):
|
| 31 |
+
try:
|
| 32 |
+
if language == "Python":
|
| 33 |
+
result = subprocess.run(['python', '-c', code], capture_output=True, text=True)
|
| 34 |
+
elif language == "JavaScript":
|
| 35 |
+
result = subprocess.run(['node', '-e', code], capture_output=True, text=True)
|
| 36 |
+
# Add more languages as needed
|
| 37 |
+
return result.stdout, result.stderr
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return None, f"Validation error: {e}"
|
| 40 |
+
|
| 41 |
# Streamlit UI setup
|
| 42 |
st.set_page_config(page_title="Sleek AI Code Assistant", page_icon="💻", layout="wide")
|
| 43 |
|
|
|
|
| 133 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
| 134 |
|
| 135 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
| 136 |
+
language = st.selectbox("Select programming language", ["Python", "JavaScript", "Other"])
|
| 137 |
|
| 138 |
if st.button("Generate Code"):
|
| 139 |
if prompt.strip() == "":
|
|
|
|
| 151 |
st.code(completed_text)
|
| 152 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 153 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 154 |
+
|
| 155 |
+
if language in ["Python", "JavaScript"]:
|
| 156 |
+
with st.spinner("Validating code..."):
|
| 157 |
+
stdout, stderr = validate_code(completed_text, language)
|
| 158 |
+
if stdout:
|
| 159 |
+
st.info(f"Validation Output:\n{stdout}")
|
| 160 |
+
if stderr:
|
| 161 |
+
st.warning(f"Validation Errors:\n{stderr}")
|
| 162 |
|
| 163 |
st.markdown("""
|
| 164 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|