Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import google.generativeai as genai
|
| 4 |
import requests
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Configure the Gemini API
|
| 7 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
|
@@ -18,7 +21,7 @@ generation_config = {
|
|
| 18 |
model = genai.GenerativeModel(
|
| 19 |
model_name="gemini-1.5-pro",
|
| 20 |
generation_config=generation_config,
|
| 21 |
-
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
|
| 22 |
)
|
| 23 |
chat_session = model.start_chat(history=[])
|
| 24 |
|
|
@@ -39,53 +42,16 @@ lottie_json = load_lottieurl(lottie_url)
|
|
| 39 |
# Streamlit UI setup
|
| 40 |
st.set_page_config(page_title="AI Code Assistant", page_icon="🤖", layout="wide")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
st.markdown("#### Powered by Google Gemini")
|
| 44 |
-
|
| 45 |
-
if lottie_json:
|
| 46 |
-
st_lottie(lottie_json, height=300, key="coding")
|
| 47 |
-
|
| 48 |
-
prompt = st.text_area("Enter your coding question or request:", height=150)
|
| 49 |
-
|
| 50 |
-
if st.button("Generate Code"):
|
| 51 |
-
if prompt.strip() == "":
|
| 52 |
-
st.error("Please enter a valid prompt.")
|
| 53 |
-
else:
|
| 54 |
-
with st.spinner("Generating code..."):
|
| 55 |
-
completed_text = generate_response(prompt)
|
| 56 |
-
st.success("Code generated successfully!")
|
| 57 |
-
|
| 58 |
-
st.subheader("Raw Response")
|
| 59 |
-
st.code(completed_text, language="text")
|
| 60 |
-
|
| 61 |
-
# Display code blocks based on predefined markers
|
| 62 |
-
html_code = ""
|
| 63 |
-
css_code = ""
|
| 64 |
-
js_code = ""
|
| 65 |
-
|
| 66 |
-
if "HTML:" in completed_text:
|
| 67 |
-
html_code = completed_text.split("HTML:")[1].split("CSS:")[0].strip()
|
| 68 |
-
st.subheader("HTML Code")
|
| 69 |
-
st.code(html_code, language="html")
|
| 70 |
-
|
| 71 |
-
if "CSS:" in completed_text:
|
| 72 |
-
css_code = completed_text.split("CSS:")[1].split("JavaScript:")[0].strip()
|
| 73 |
-
st.subheader("CSS Code")
|
| 74 |
-
st.code(css_code, language="css")
|
| 75 |
-
|
| 76 |
-
if "JavaScript:" in completed_text:
|
| 77 |
-
js_code = completed_text.split("JavaScript:")[1].strip()
|
| 78 |
-
st.subheader("JavaScript Code")
|
| 79 |
-
st.code(js_code, language="javascript")
|
| 80 |
-
|
| 81 |
st.markdown("""
|
| 82 |
<style>
|
| 83 |
.main {
|
| 84 |
background-color: #f0f2f6;
|
| 85 |
font-family: 'Roboto', sans-serif;
|
| 86 |
}
|
| 87 |
-
h1 {
|
| 88 |
color: #333;
|
|
|
|
| 89 |
}
|
| 90 |
.stTextArea label {
|
| 91 |
font-size: 1.2rem;
|
|
@@ -96,9 +62,12 @@ st.markdown("""
|
|
| 96 |
color: white;
|
| 97 |
border-radius: 10px;
|
| 98 |
font-size: 1.1rem;
|
|
|
|
| 99 |
}
|
| 100 |
.stButton button:hover {
|
| 101 |
background-color: #5a52d1;
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
.stCodeBlock {
|
| 104 |
background-color: #272822;
|
|
@@ -106,5 +75,62 @@ st.markdown("""
|
|
| 106 |
font-size: 1rem;
|
| 107 |
border-radius: 10px;
|
| 108 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
</style>
|
| 110 |
-
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import google.generativeai as genai
|
| 4 |
import requests
|
| 5 |
+
import json
|
| 6 |
+
from streamlit_lottie import st_lottie
|
| 7 |
+
from streamlit_ace import st_ace
|
| 8 |
|
| 9 |
# Configure the Gemini API
|
| 10 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
|
|
|
| 21 |
model = genai.GenerativeModel(
|
| 22 |
model_name="gemini-1.5-pro",
|
| 23 |
generation_config=generation_config,
|
| 24 |
+
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.",
|
| 25 |
)
|
| 26 |
chat_session = model.start_chat(history=[])
|
| 27 |
|
|
|
|
| 42 |
# Streamlit UI setup
|
| 43 |
st.set_page_config(page_title="AI Code Assistant", page_icon="🤖", layout="wide")
|
| 44 |
|
| 45 |
+
# Custom CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
st.markdown("""
|
| 47 |
<style>
|
| 48 |
.main {
|
| 49 |
background-color: #f0f2f6;
|
| 50 |
font-family: 'Roboto', sans-serif;
|
| 51 |
}
|
| 52 |
+
h1, h2 {
|
| 53 |
color: #333;
|
| 54 |
+
font-weight: bold;
|
| 55 |
}
|
| 56 |
.stTextArea label {
|
| 57 |
font-size: 1.2rem;
|
|
|
|
| 62 |
color: white;
|
| 63 |
border-radius: 10px;
|
| 64 |
font-size: 1.1rem;
|
| 65 |
+
transition: all 0.3s ease;
|
| 66 |
}
|
| 67 |
.stButton button:hover {
|
| 68 |
background-color: #5a52d1;
|
| 69 |
+
transform: translateY(-2px);
|
| 70 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 71 |
}
|
| 72 |
.stCodeBlock {
|
| 73 |
background-color: #272822;
|
|
|
|
| 75 |
font-size: 1rem;
|
| 76 |
border-radius: 10px;
|
| 77 |
}
|
| 78 |
+
.css-1v0mbdj.etr89bj1 {
|
| 79 |
+
display: flex;
|
| 80 |
+
flex-direction: column;
|
| 81 |
+
align-items: center;
|
| 82 |
+
}
|
| 83 |
</style>
|
| 84 |
+
""", unsafe_allow_html=True)
|
| 85 |
+
|
| 86 |
+
# App header
|
| 87 |
+
st.title("🤖 AI Code Assistant")
|
| 88 |
+
st.markdown("#### Powered by Google Gemini")
|
| 89 |
+
|
| 90 |
+
# Sidebar
|
| 91 |
+
st.sidebar.header("Settings")
|
| 92 |
+
language = st.sidebar.selectbox("Select programming language", ["Python", "JavaScript", "Java", "C++", "Ruby"])
|
| 93 |
+
|
| 94 |
+
# Main content
|
| 95 |
+
col1, col2 = st.columns([2, 1])
|
| 96 |
+
|
| 97 |
+
with col1:
|
| 98 |
+
prompt = st.text_area("Enter your coding question or request:", height=150)
|
| 99 |
+
|
| 100 |
+
if st.button("Generate Code"):
|
| 101 |
+
if prompt.strip() == "":
|
| 102 |
+
st.error("Please enter a valid prompt.")
|
| 103 |
+
else:
|
| 104 |
+
with st.spinner("Generating code..."):
|
| 105 |
+
completed_text = generate_response(prompt)
|
| 106 |
+
st.success("Code generated successfully!")
|
| 107 |
+
|
| 108 |
+
# Display code in an interactive editor
|
| 109 |
+
generated_code = st_ace(value=completed_text, language=language.lower(), theme="monokai", height=300)
|
| 110 |
+
|
| 111 |
+
with col2:
|
| 112 |
+
if lottie_json:
|
| 113 |
+
st_lottie(lottie_json, height=300, key="coding")
|
| 114 |
+
|
| 115 |
+
# Additional features
|
| 116 |
+
st.header("Additional Tools")
|
| 117 |
+
|
| 118 |
+
# Code explanation
|
| 119 |
+
if st.checkbox("Explain the generated code"):
|
| 120 |
+
if 'generated_code' in locals():
|
| 121 |
+
explanation = generate_response(f"Explain the following {language} code:\n\n{generated_code}")
|
| 122 |
+
st.write(explanation)
|
| 123 |
+
else:
|
| 124 |
+
st.warning("Generate some code first to get an explanation.")
|
| 125 |
+
|
| 126 |
+
# Code optimization
|
| 127 |
+
if st.checkbox("Optimize the generated code"):
|
| 128 |
+
if 'generated_code' in locals():
|
| 129 |
+
optimized_code = generate_response(f"Optimize the following {language} code:\n\n{generated_code}")
|
| 130 |
+
st.code(optimized_code, language=language.lower())
|
| 131 |
+
else:
|
| 132 |
+
st.warning("Generate some code first to optimize it.")
|
| 133 |
+
|
| 134 |
+
# Footer
|
| 135 |
+
st.markdown("---")
|
| 136 |
+
st.markdown("Created with ❤️ by Your Name")
|