Spaces:
Sleeping
Sleeping
File size: 4,067 Bytes
070969f 357d7d8 5de3cb1 5afe68b 9ab0263 357d7d8 dd803ed 357d7d8 9ab0263 5afe68b 474d75e 5afe68b e45191b 9ab0263 e45191b 432f223 5de3cb1 9ab0263 5de3cb1 dd803ed 357d7d8 b1c638a 5afe68b 5de3cb1 5afe68b dd803ed 5de3cb1 9ab0263 5de3cb1 9ab0263 5de3cb1 9ab0263 5de3cb1 9ab0263 5de3cb1 9ab0263 5de3cb1 3875b2a 5de3cb1 357d7d8 5de3cb1 dd803ed 357d7d8 e45191b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import streamlit as st
import os
import zipfile
import io
import re
from google import genai
# Initialize Gemini Client with API key from environment
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
# Supported extensions and corresponding language names for syntax highlighting
SUPPORTED_EXTENSIONS = [
"py", "c", "cpp", "java", "js", "jsx", "go", "rs", "php", "rb", "swift", "kt", "html", "css"
]
EXTENSION_LANGUAGE_MAP = {
"py": "python",
"c": "c",
"cpp": "cpp",
"java": "java",
"js": "javascript",
"jsx": "jsx",
"go": "go",
"rs": "rust",
"php": "php",
"rb": "ruby",
"swift": "swift",
"kt": "kotlin",
"html": "html",
"css": "css"
}
# Helper: Read and decode code file
def read_code_file(file):
content = file.read().decode("utf-8")
filename = file.name
return filename, content
# Analyze a single file and return result
def analyze_code(filename, code_text):
prompt = (
f"You are a software code analysis assistant. Given the code below, perform the following tasks:\n\n"
f"1. Provide a short README-style summary of what the code is intended to do.\n"
f"2. Identify any bugs or issues in the code.\n"
f"3. Provide a corrected, clean version of the entire code.\n"
f"4. List the changes you made in bullet points.\n\n"
f"--- FILE: {filename} ---\n{code_text}\n--- END ---"
)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[{"text": prompt}]
)
return response.text
# Extract corrected code from response
def extract_corrected_code(response_text):
code_blocks = re.findall(r"```(?:[a-zA-Z]*\n)?(.*?)```", response_text, re.DOTALL)
return code_blocks[0].strip() if code_blocks else ""
# Main App
def main():
st.set_page_config(page_title="Multi-Language Code Analyzer & Fixer", layout="centered")
st.title("Multi-Language Code Analyzer & Bug Fixer")
st.write("Upload one or more code files and get a full analysis with downloadable fixed versions.")
uploaded_files = st.file_uploader(
"Upload your code file(s)",
type=SUPPORTED_EXTENSIONS,
accept_multiple_files=True
)
if uploaded_files and st.button("🔍 Analyze and Fix All"):
fixed_files = []
for file in uploaded_files:
filename, code_text = read_code_file(file)
lang_ext = filename.split(".")[-1]
lang = EXTENSION_LANGUAGE_MAP.get(lang_ext, "")
st.subheader(f"📄 Analysis: `{filename}`")
st.markdown("### 🧾 Original Code:")
st.code(code_text, language=lang)
with st.spinner(f"Analyzing `{filename}`..."):
response_text = analyze_code(filename, code_text)
st.markdown("### 🧠 AI Analysis:")
st.markdown(response_text)
corrected_code = extract_corrected_code(response_text)
if corrected_code:
st.markdown("### ✅ Fixed Code:")
st.code(corrected_code, language=lang)
fixed_files.append((filename, corrected_code))
st.download_button(
label=f"📥 Download fixed `{filename}`",
data=corrected_code,
file_name=f"fixed_{filename}",
mime="text/plain"
)
else:
st.warning(f"❌ Could not extract corrected code for `{filename}`.")
if fixed_files:
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w") as zip_file:
for filename, fixed_code in fixed_files:
zip_file.writestr(f"fixed_{filename}", fixed_code)
zip_buffer.seek(0)
st.download_button(
label="📦 Download All Fixed Files as ZIP",
data=zip_buffer,
file_name="fixed_code_files.zip",
mime="application/zip"
)
if __name__ == "__main__":
main() |