Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import streamlit as st
|
|
| 2 |
import os
|
| 3 |
import base64
|
| 4 |
from google import genai
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Initialize Gemini Client with API key from environment
|
| 7 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
|
@@ -15,40 +17,74 @@ def read_code_file(file):
|
|
| 15 |
filename = file.name
|
| 16 |
return filename, content
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Main App
|
| 19 |
def main():
|
| 20 |
-
st.set_page_config(page_title="๐ง Code Analyzer & Fixer", layout="centered")
|
| 21 |
-
st.title("๐ ๏ธ Code Analyzer & Bug Fixer")
|
| 22 |
-
st.write("Upload
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"- Corrected code\n"
|
| 26 |
-
"- Summary of changes")
|
| 27 |
-
|
| 28 |
-
uploaded_file = st.file_uploader("Upload your code file", type=SUPPORTED_EXTENSIONS)
|
| 29 |
-
|
| 30 |
-
if uploaded_file:
|
| 31 |
-
filename, code_text = read_code_file(uploaded_file)
|
| 32 |
-
st.code(code_text, language=filename.split(".")[-1])
|
| 33 |
-
|
| 34 |
-
if st.button("๐ Analyze and Fix Code"):
|
| 35 |
-
with st.spinner("Analyzing your code using Gemini..."):
|
| 36 |
-
prompt = (
|
| 37 |
-
f"You are a software code analysis assistant. Given the code below, perform the following tasks:\n\n"
|
| 38 |
-
f"1. Provide a short README-style summary of what the code is intended to do.\n"
|
| 39 |
-
f"2. Identify any bugs or issues in the code.\n"
|
| 40 |
-
f"3. Provide a corrected, clean version of the entire code.\n"
|
| 41 |
-
f"4. List the changes you made in bullet points.\n\n"
|
| 42 |
-
f"--- CODE START ---\n{code_text}\n--- CODE END ---"
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
main()
|
|
|
|
| 2 |
import os
|
| 3 |
import base64
|
| 4 |
from google import genai
|
| 5 |
+
import zipfile
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
# Initialize Gemini Client with API key from environment
|
| 9 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
|
|
|
| 17 |
filename = file.name
|
| 18 |
return filename, content
|
| 19 |
|
| 20 |
+
# Analyze a single file and return result
|
| 21 |
+
def analyze_code(filename, code_text):
|
| 22 |
+
prompt = (
|
| 23 |
+
f"You are a software code analysis assistant. Given the code below, perform the following tasks:\n\n"
|
| 24 |
+
f"1. Provide a short README-style summary of what the code is intended to do.\n"
|
| 25 |
+
f"2. Identify any bugs or issues in the code.\n"
|
| 26 |
+
f"3. Provide a corrected, clean version of the entire code.\n"
|
| 27 |
+
f"4. List the changes you made in bullet points.\n\n"
|
| 28 |
+
f"--- FILE: {filename} ---\n{code_text}\n--- END ---"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
response = client.models.generate_content(
|
| 32 |
+
model="gemini-2.5-flash",
|
| 33 |
+
contents=[{"text": prompt}]
|
| 34 |
+
)
|
| 35 |
+
return response.text
|
| 36 |
+
|
| 37 |
+
# Extract corrected code from response
|
| 38 |
+
def extract_corrected_code(response_text):
|
| 39 |
+
import re
|
| 40 |
+
code_blocks = re.findall(r"```(?:[a-zA-Z]*\n)?(.*?)```", response_text, re.DOTALL)
|
| 41 |
+
return code_blocks[0] if code_blocks else ""
|
| 42 |
+
|
| 43 |
# Main App
|
| 44 |
def main():
|
| 45 |
+
st.set_page_config(page_title="๐ง Multi-Code Analyzer & Fixer", layout="centered")
|
| 46 |
+
st.title("๐ ๏ธ Multi-Code Analyzer & Bug Fixer")
|
| 47 |
+
st.write("Upload one or more code files (Python, C, C++, Java, or React) and get a full analysis with downloadable fixed versions.")
|
| 48 |
+
|
| 49 |
+
uploaded_files = st.file_uploader("Upload your code file(s)", type=SUPPORTED_EXTENSIONS, accept_multiple_files=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
if uploaded_files and st.button("๐ Analyze and Fix All"):
|
| 52 |
+
fixed_files = []
|
| 53 |
+
|
| 54 |
+
for file in uploaded_files:
|
| 55 |
+
filename, code_text = read_code_file(file)
|
| 56 |
+
st.subheader(f"๐ Analysis: `{filename}`")
|
| 57 |
+
|
| 58 |
+
with st.spinner(f"Analyzing `{filename}`..."):
|
| 59 |
+
response_text = analyze_code(filename, code_text)
|
| 60 |
+
|
| 61 |
+
st.markdown(response_text)
|
| 62 |
+
|
| 63 |
+
corrected_code = extract_corrected_code(response_text)
|
| 64 |
+
if corrected_code:
|
| 65 |
+
fixed_files.append((filename, corrected_code))
|
| 66 |
+
st.download_button(
|
| 67 |
+
label=f"๐ฅ Download fixed `{filename}`",
|
| 68 |
+
data=corrected_code,
|
| 69 |
+
file_name=f"fixed_{filename}",
|
| 70 |
+
mime="text/plain"
|
| 71 |
)
|
| 72 |
+
else:
|
| 73 |
+
st.warning(f"โ Could not extract corrected code for `{filename}`.")
|
| 74 |
+
|
| 75 |
+
if fixed_files:
|
| 76 |
+
zip_buffer = io.BytesIO()
|
| 77 |
+
with zipfile.ZipFile(zip_buffer, "w") as zip_file:
|
| 78 |
+
for filename, fixed_code in fixed_files:
|
| 79 |
+
zip_file.writestr(f"fixed_{filename}", fixed_code)
|
| 80 |
+
zip_buffer.seek(0)
|
| 81 |
|
| 82 |
+
st.download_button(
|
| 83 |
+
label="๐ฆ Download All Fixed Files as ZIP",
|
| 84 |
+
data=zip_buffer,
|
| 85 |
+
file_name="fixed_code_files.zip",
|
| 86 |
+
mime="application/zip"
|
| 87 |
+
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
main()
|