Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,36 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import base64
|
| 4 |
-
from google import genai
|
| 5 |
import zipfile
|
| 6 |
import io
|
| 7 |
import re
|
|
|
|
| 8 |
|
| 9 |
# Initialize Gemini Client with API key from environment
|
| 10 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 11 |
|
| 12 |
-
#
|
| 13 |
SUPPORTED_EXTENSIONS = [
|
| 14 |
-
"py", "c", "cpp", "java", "js", "jsx",
|
| 15 |
-
"ts", "go", "rs", "php", "rb", "swift", "kt",
|
| 16 |
-
"html", "css"
|
| 17 |
]
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Helper: Read and decode code file
|
| 20 |
def read_code_file(file):
|
| 21 |
content = file.read().decode("utf-8")
|
|
@@ -42,7 +57,7 @@ def analyze_code(filename, code_text):
|
|
| 42 |
# Extract corrected code from response
|
| 43 |
def extract_corrected_code(response_text):
|
| 44 |
code_blocks = re.findall(r"```(?:[a-zA-Z]*\n)?(.*?)```", response_text, re.DOTALL)
|
| 45 |
-
return code_blocks[0] if code_blocks else ""
|
| 46 |
|
| 47 |
# Main App
|
| 48 |
def main():
|
|
@@ -61,16 +76,26 @@ def main():
|
|
| 61 |
|
| 62 |
for file in uploaded_files:
|
| 63 |
filename, code_text = read_code_file(file)
|
|
|
|
|
|
|
|
|
|
| 64 |
st.subheader(f"📄 Analysis: `{filename}`")
|
|
|
|
|
|
|
| 65 |
|
| 66 |
with st.spinner(f"Analyzing `{filename}`..."):
|
| 67 |
response_text = analyze_code(filename, code_text)
|
| 68 |
|
|
|
|
| 69 |
st.markdown(response_text)
|
| 70 |
|
| 71 |
corrected_code = extract_corrected_code(response_text)
|
| 72 |
if corrected_code:
|
|
|
|
|
|
|
|
|
|
| 73 |
fixed_files.append((filename, corrected_code))
|
|
|
|
| 74 |
st.download_button(
|
| 75 |
label=f"📥 Download fixed `{filename}`",
|
| 76 |
data=corrected_code,
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
import zipfile
|
| 4 |
import io
|
| 5 |
import re
|
| 6 |
+
from google import genai
|
| 7 |
|
| 8 |
# Initialize Gemini Client with API key from environment
|
| 9 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 10 |
|
| 11 |
+
# Supported extensions and corresponding language names for syntax highlighting
|
| 12 |
SUPPORTED_EXTENSIONS = [
|
| 13 |
+
"py", "c", "cpp", "java", "js", "jsx", "ts", "go", "rs", "php", "rb", "swift", "kt", "html", "css"
|
|
|
|
|
|
|
| 14 |
]
|
| 15 |
|
| 16 |
+
EXTENSION_LANGUAGE_MAP = {
|
| 17 |
+
"py": "python",
|
| 18 |
+
"c": "c",
|
| 19 |
+
"cpp": "cpp",
|
| 20 |
+
"java": "java",
|
| 21 |
+
"js": "javascript",
|
| 22 |
+
"jsx": "jsx",
|
| 23 |
+
"ts": "typescript",
|
| 24 |
+
"go": "go",
|
| 25 |
+
"rs": "rust",
|
| 26 |
+
"php": "php",
|
| 27 |
+
"rb": "ruby",
|
| 28 |
+
"swift": "swift",
|
| 29 |
+
"kt": "kotlin",
|
| 30 |
+
"html": "html",
|
| 31 |
+
"css": "css"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
# Helper: Read and decode code file
|
| 35 |
def read_code_file(file):
|
| 36 |
content = file.read().decode("utf-8")
|
|
|
|
| 57 |
# Extract corrected code from response
|
| 58 |
def extract_corrected_code(response_text):
|
| 59 |
code_blocks = re.findall(r"```(?:[a-zA-Z]*\n)?(.*?)```", response_text, re.DOTALL)
|
| 60 |
+
return code_blocks[0].strip() if code_blocks else ""
|
| 61 |
|
| 62 |
# Main App
|
| 63 |
def main():
|
|
|
|
| 76 |
|
| 77 |
for file in uploaded_files:
|
| 78 |
filename, code_text = read_code_file(file)
|
| 79 |
+
lang_ext = filename.split(".")[-1]
|
| 80 |
+
lang = EXTENSION_LANGUAGE_MAP.get(lang_ext, "")
|
| 81 |
+
|
| 82 |
st.subheader(f"📄 Analysis: `{filename}`")
|
| 83 |
+
st.markdown("### 🧾 Original Code:")
|
| 84 |
+
st.code(code_text, language=lang)
|
| 85 |
|
| 86 |
with st.spinner(f"Analyzing `{filename}`..."):
|
| 87 |
response_text = analyze_code(filename, code_text)
|
| 88 |
|
| 89 |
+
st.markdown("### 🧠 AI Analysis:")
|
| 90 |
st.markdown(response_text)
|
| 91 |
|
| 92 |
corrected_code = extract_corrected_code(response_text)
|
| 93 |
if corrected_code:
|
| 94 |
+
st.markdown("### ✅ Fixed Code:")
|
| 95 |
+
st.code(corrected_code, language=lang)
|
| 96 |
+
|
| 97 |
fixed_files.append((filename, corrected_code))
|
| 98 |
+
|
| 99 |
st.download_button(
|
| 100 |
label=f"📥 Download fixed `{filename}`",
|
| 101 |
data=corrected_code,
|