Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,76 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
def animated_text(text, speed=0.
|
| 6 |
placeholder = st.empty()
|
| 7 |
displayed_text = ""
|
| 8 |
for letter in text:
|
| 9 |
displayed_text += letter
|
| 10 |
-
placeholder.markdown(f""
|
| 11 |
-
<h1 style="text-align:center; color: #FF4500;">{displayed_text} π</h1>
|
| 12 |
-
""", unsafe_allow_html=True) # Corrected f-string formatting
|
| 13 |
time.sleep(speed)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
animated_text("Welcome to
|
| 17 |
-
|
| 18 |
-
import google.generativeai as genai
|
| 19 |
-
|
| 20 |
-
# Set up Google Gemini API Key
|
| 21 |
-
genai.configure(api_key="AIzaSyC7op0zN_EESTSFhXGtzHRYImp5nhvDz-c")
|
| 22 |
|
| 23 |
-
# Streamlit
|
| 24 |
-
st.
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
buggy_code = st.text_area("π
|
| 28 |
|
| 29 |
-
# Function to
|
| 30 |
def debug_code_with_gemini(code):
|
| 31 |
prompt = f"Debug the following code and provide the corrected version:\n\n{code}"
|
| 32 |
-
|
| 33 |
model = genai.GenerativeModel("models/gemini-2.0-flash")
|
| 34 |
response = model.generate_content(prompt)
|
| 35 |
-
|
| 36 |
return response.text if response.text else "β οΈ Error in AI response."
|
| 37 |
|
| 38 |
-
# Function to
|
| 39 |
def get_suggestions_with_gemini(code):
|
| 40 |
prompt = f"Suggest improvements for the following code, focusing on best practices, performance optimization, and readability:\n\n{code}"
|
| 41 |
-
|
| 42 |
model = genai.GenerativeModel("gemini-pro")
|
| 43 |
response = model.generate_content(prompt)
|
| 44 |
-
|
| 45 |
return response.text if response.text else "β οΈ No suggestions available."
|
| 46 |
|
| 47 |
-
#
|
| 48 |
if buggy_code:
|
| 49 |
with st.spinner("π οΈ Debugging your code..."):
|
| 50 |
fixed_code = debug_code_with_gemini(buggy_code)
|
| 51 |
|
| 52 |
-
with st.spinner("π Generating suggestions..."):
|
| 53 |
suggestions = get_suggestions_with_gemini(fixed_code)
|
| 54 |
|
| 55 |
-
# Display
|
| 56 |
-
st.
|
| 57 |
-
st.code(fixed_code, language="python")
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
st.download_button(
|
| 61 |
-
label="
|
| 62 |
data=fixed_code,
|
| 63 |
file_name="fixed_code.py",
|
| 64 |
mime="text/plain"
|
| 65 |
)
|
| 66 |
-
|
| 67 |
-
# Display AI
|
| 68 |
-
st.
|
| 69 |
-
st.write(suggestions)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import time
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
# Configure Google Gemini API Key
|
| 8 |
+
genai.configure(api_key="AIzaSyC7op0zN_EESTSFhXGtzHRYImp5nhvDz-c")
|
| 9 |
+
|
| 10 |
+
# Custom CSS for advanced styling
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
body {background-color: #0E0E0E; color: #FFFFFF;}
|
| 14 |
+
.main-title {text-align: center; font-size: 40px; font-weight: bold; color: #FFA500;}
|
| 15 |
+
.sub-header {text-align: center; font-size: 25px; font-weight: bold; color: #FFD700;}
|
| 16 |
+
.stTextArea textarea {background-color: #222; color: #FFA500; border-radius: 10px;}
|
| 17 |
+
.stButton button {background-color: #FF4500; color: white; font-size: 18px; border-radius: 10px;}
|
| 18 |
+
.stDownloadButton button {background-color: #32CD32; color: white; font-size: 18px; border-radius: 10px;}
|
| 19 |
+
</style>
|
| 20 |
+
""", unsafe_allow_html=True)
|
| 21 |
|
| 22 |
+
# Function to display animated text
|
| 23 |
+
def animated_text(text, speed=0.07):
|
| 24 |
placeholder = st.empty()
|
| 25 |
displayed_text = ""
|
| 26 |
for letter in text:
|
| 27 |
displayed_text += letter
|
| 28 |
+
placeholder.markdown(f'<h1 class="main-title">{displayed_text} π</h1>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 29 |
time.sleep(speed)
|
| 30 |
|
| 31 |
+
# Animated Welcome Text
|
| 32 |
+
animated_text("Welcome to AI Code Debugger & Optimizer!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Streamlit Layout
|
| 35 |
+
st.markdown("<h2 class='sub-header'>π AI-Powered Code Debugging & Enhancement</h2>", unsafe_allow_html=True)
|
| 36 |
|
| 37 |
+
# User Input for Buggy Code
|
| 38 |
+
buggy_code = st.text_area("π Paste your buggy code below:", height=200)
|
| 39 |
|
| 40 |
+
# Function to debug code
|
| 41 |
def debug_code_with_gemini(code):
|
| 42 |
prompt = f"Debug the following code and provide the corrected version:\n\n{code}"
|
|
|
|
| 43 |
model = genai.GenerativeModel("models/gemini-2.0-flash")
|
| 44 |
response = model.generate_content(prompt)
|
|
|
|
| 45 |
return response.text if response.text else "β οΈ Error in AI response."
|
| 46 |
|
| 47 |
+
# Function to get suggestions for improvement
|
| 48 |
def get_suggestions_with_gemini(code):
|
| 49 |
prompt = f"Suggest improvements for the following code, focusing on best practices, performance optimization, and readability:\n\n{code}"
|
|
|
|
| 50 |
model = genai.GenerativeModel("gemini-pro")
|
| 51 |
response = model.generate_content(prompt)
|
|
|
|
| 52 |
return response.text if response.text else "β οΈ No suggestions available."
|
| 53 |
|
| 54 |
+
# Processing user input
|
| 55 |
if buggy_code:
|
| 56 |
with st.spinner("π οΈ Debugging your code..."):
|
| 57 |
fixed_code = debug_code_with_gemini(buggy_code)
|
| 58 |
|
| 59 |
+
with st.spinner("π Generating optimization suggestions..."):
|
| 60 |
suggestions = get_suggestions_with_gemini(fixed_code)
|
| 61 |
|
| 62 |
+
# Display Debugged Code
|
| 63 |
+
st.markdown("<h2 class='sub-header'>β
Fixed Code:</h2>", unsafe_allow_html=True)
|
| 64 |
+
st.code(fixed_code, language="python")
|
| 65 |
+
|
| 66 |
+
# Download button for Fixed Code
|
| 67 |
st.download_button(
|
| 68 |
+
label="π₯ Download Fixed Code",
|
| 69 |
data=fixed_code,
|
| 70 |
file_name="fixed_code.py",
|
| 71 |
mime="text/plain"
|
| 72 |
)
|
| 73 |
+
|
| 74 |
+
# Display AI Suggestions
|
| 75 |
+
st.markdown("<h2 class='sub-header'>π‘ AI-Generated Suggestions:</h2>", unsafe_allow_html=True)
|
| 76 |
+
st.write(suggestions)
|