Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
# Animated text function
|
| 5 |
+
def animated_text(text, speed=0.05):
|
| 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 |
+
# Display animated text
|
| 16 |
+
animated_text("Welcome to GenAI App - AI Code Reviewer!", speed=0.1)
|
| 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 App Title
|
| 24 |
+
st.title("π€ AI-Powered Code Debugging")
|
| 25 |
+
|
| 26 |
+
# Text Area for User Input (Buggy Code)
|
| 27 |
+
buggy_code = st.text_area("π Enter your buggy code here:", height=200)
|
| 28 |
+
|
| 29 |
+
# Function to Debug Code Using Gemini AI
|
| 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 Generate Code Improvement Suggestions
|
| 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 |
+
# Process Code When Input is Given
|
| 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 Fixed Code
|
| 56 |
+
st.subheader("β
Fixed Code:")
|
| 57 |
+
st.code(fixed_code, language="python") # Adjust language if needed
|
| 58 |
+
|
| 59 |
+
# Copy Button for Fixed Code
|
| 60 |
+
st.download_button(
|
| 61 |
+
label="π Copy Fixed Code",
|
| 62 |
+
data=fixed_code,
|
| 63 |
+
file_name="fixed_code.py",
|
| 64 |
+
mime="text/plain"
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Display AI-Generated Suggestions
|
| 68 |
+
st.subheader("π‘ Suggestions for Improvement:")
|
| 69 |
+
st.write(suggestions)
|