Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,23 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import difflib
|
|
|
|
| 3 |
import requests
|
| 4 |
import datetime
|
| 5 |
import streamlit.components.v1 as components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- CONFIG ---
|
| 8 |
# Place your API keys here
|
|
@@ -103,6 +118,14 @@ def get_explanation_prompt(code, programming_language, skill_level, user_role, e
|
|
| 103 |
f"Explain this {programming_language} code for a {skill_level} {user_role}.{lang_instruction}\n{code}"
|
| 104 |
)
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# --- STREAMLIT APP ---
|
| 107 |
st.set_page_config(page_title="Code Workflows", layout="wide")
|
| 108 |
st.title("Code Genie")
|
|
@@ -113,7 +136,7 @@ page = st.sidebar.radio("Navigate", ["Home", "Code Workflows", "Semantic Search"
|
|
| 113 |
if page == "Home":
|
| 114 |
st.header("Welcome to the Code Genie!")
|
| 115 |
st.markdown("""
|
| 116 |
-
- **Full Code Workflow:** Complete code analysis pipeline with explanation, refactoring, review, and testing
|
| 117 |
- **Semantic Search:** Ask natural language questions about your code and get intelligent answers
|
| 118 |
- **Code Comment Generator:** Helps you add helpful comments to your code for better readability
|
| 119 |
""")
|
|
@@ -170,6 +193,32 @@ elif page == "Code Workflows":
|
|
| 170 |
for t in timeline:
|
| 171 |
report += f"## {t['step']}\n{t['output']}\n\n---\n\n"
|
| 172 |
st.download_button("Download Report", report, file_name="ai_workflow_report.txt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
elif page == "Semantic Search":
|
| 175 |
st.header("Semantic Search")
|
|
@@ -191,7 +240,7 @@ elif page == "Semantic Search":
|
|
| 191 |
st.caption("Example questions:")
|
| 192 |
st.write(", ".join(EXAMPLE_QUESTIONS))
|
| 193 |
|
| 194 |
-
#
|
| 195 |
question = st.text_input("Ask a question about your code", key="sem_question")
|
| 196 |
|
| 197 |
# Run Semantic Search button
|
|
@@ -206,6 +255,29 @@ elif page == "Semantic Search":
|
|
| 206 |
answer = call_groq_api(prompt)
|
| 207 |
st.success("Answer:")
|
| 208 |
st.write(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
elif page == "Code Comment Generator":
|
| 211 |
st.header("Code Comment Generator")
|
|
@@ -234,4 +306,40 @@ elif page == "Code Comment Generator":
|
|
| 234 |
])
|
| 235 |
st.success("Commented code generated!")
|
| 236 |
st.code(commented_code, language=programming_language.lower())
|
| 237 |
-
st.download_button("Download Commented Code", commented_code, file_name="commented_code.txt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
import torch
|
| 8 |
import difflib
|
| 9 |
+
import re
|
| 10 |
import requests
|
| 11 |
import datetime
|
| 12 |
import streamlit.components.v1 as components
|
| 13 |
+
import io
|
| 14 |
+
|
| 15 |
+
from diffusers import DiffusionPipeline
|
| 16 |
+
from moviepy.editor import ImageSequenceClip
|
| 17 |
+
|
| 18 |
+
import insightface
|
| 19 |
+
from insightface.app import FaceAnalysis
|
| 20 |
+
from insightface.utils import face_align
|
| 21 |
|
| 22 |
# --- CONFIG ---
|
| 23 |
# Place your API keys here
|
|
|
|
| 118 |
f"Explain this {programming_language} code for a {skill_level} {user_role}.{lang_instruction}\n{code}"
|
| 119 |
)
|
| 120 |
|
| 121 |
+
# --- SESSION STATE FOR CHAT HISTORY ---
|
| 122 |
+
if "workflow_history" not in st.session_state:
|
| 123 |
+
st.session_state.workflow_history = []
|
| 124 |
+
if "semantic_history" not in st.session_state:
|
| 125 |
+
st.session_state.semantic_history = []
|
| 126 |
+
if "comment_history" not in st.session_state:
|
| 127 |
+
st.session_state.comment_history = []
|
| 128 |
+
|
| 129 |
# --- STREAMLIT APP ---
|
| 130 |
st.set_page_config(page_title="Code Workflows", layout="wide")
|
| 131 |
st.title("Code Genie")
|
|
|
|
| 136 |
if page == "Home":
|
| 137 |
st.header("Welcome to the Code Genie!")
|
| 138 |
st.markdown("""
|
| 139 |
+
- **Full Code Workflow:** Complete code analysis pipeline with explanation, refactoring, review, and testing (powered by Groq/Blackbox)
|
| 140 |
- **Semantic Search:** Ask natural language questions about your code and get intelligent answers
|
| 141 |
- **Code Comment Generator:** Helps you add helpful comments to your code for better readability
|
| 142 |
""")
|
|
|
|
| 193 |
for t in timeline:
|
| 194 |
report += f"## {t['step']}\n{t['output']}\n\n---\n\n"
|
| 195 |
st.download_button("Download Report", report, file_name="ai_workflow_report.txt")
|
| 196 |
+
# Save to chat history
|
| 197 |
+
st.session_state.workflow_history.append({
|
| 198 |
+
"timestamp": str(datetime.datetime.now()),
|
| 199 |
+
"user_code": code_input,
|
| 200 |
+
"params": {
|
| 201 |
+
"language": programming_language,
|
| 202 |
+
"skill": skill_level,
|
| 203 |
+
"role": user_role,
|
| 204 |
+
"explanation_language": explanation_language
|
| 205 |
+
},
|
| 206 |
+
"timeline": timeline,
|
| 207 |
+
"refactored_code": refactored_code
|
| 208 |
+
})
|
| 209 |
+
# Show chat history for workflows
|
| 210 |
+
st.markdown("### Workflow Chat History")
|
| 211 |
+
if st.button("Clear Workflow History"):
|
| 212 |
+
st.session_state.workflow_history = []
|
| 213 |
+
for entry in reversed(st.session_state.workflow_history):
|
| 214 |
+
st.markdown(f"**[{entry['timestamp']}]**")
|
| 215 |
+
st.code(entry["user_code"], language=entry["params"]["language"].lower())
|
| 216 |
+
for t in entry["timeline"]:
|
| 217 |
+
st.subheader(t["step"])
|
| 218 |
+
st.write(t["output"])
|
| 219 |
+
st.subheader("Code Diff (Original vs Refactored)")
|
| 220 |
+
st.code(get_inline_diff(entry["user_code"], entry["refactored_code"]), language=entry["params"]["language"].lower())
|
| 221 |
+
st.markdown("---")
|
| 222 |
|
| 223 |
elif page == "Semantic Search":
|
| 224 |
st.header("Semantic Search")
|
|
|
|
| 240 |
st.caption("Example questions:")
|
| 241 |
st.write(", ".join(EXAMPLE_QUESTIONS))
|
| 242 |
|
| 243 |
+
# Only text input for question
|
| 244 |
question = st.text_input("Ask a question about your code", key="sem_question")
|
| 245 |
|
| 246 |
# Run Semantic Search button
|
|
|
|
| 255 |
answer = call_groq_api(prompt)
|
| 256 |
st.success("Answer:")
|
| 257 |
st.write(answer)
|
| 258 |
+
# Save to chat history
|
| 259 |
+
st.session_state.semantic_history.append({
|
| 260 |
+
"timestamp": str(datetime.datetime.now()),
|
| 261 |
+
"user_code": code_input,
|
| 262 |
+
"question": question,
|
| 263 |
+
"params": {
|
| 264 |
+
"language": programming_language,
|
| 265 |
+
"skill": skill_level,
|
| 266 |
+
"role": user_role,
|
| 267 |
+
"explanation_language": explanation_language
|
| 268 |
+
},
|
| 269 |
+
"answer": answer
|
| 270 |
+
})
|
| 271 |
+
# Show chat history for semantic search
|
| 272 |
+
st.markdown("### Semantic Search Chat History")
|
| 273 |
+
if st.button("Clear Semantic History"):
|
| 274 |
+
st.session_state.semantic_history = []
|
| 275 |
+
for entry in reversed(st.session_state.semantic_history):
|
| 276 |
+
st.markdown(f"**[{entry['timestamp']}]**")
|
| 277 |
+
st.code(entry["user_code"], language=entry["params"]["language"].lower())
|
| 278 |
+
st.markdown(f"**Q:** {entry['question']}")
|
| 279 |
+
st.markdown(f"**A:** {entry['answer']}")
|
| 280 |
+
st.markdown("---")
|
| 281 |
|
| 282 |
elif page == "Code Comment Generator":
|
| 283 |
st.header("Code Comment Generator")
|
|
|
|
| 306 |
])
|
| 307 |
st.success("Commented code generated!")
|
| 308 |
st.code(commented_code, language=programming_language.lower())
|
| 309 |
+
st.download_button("Download Commented Code", commented_code, file_name="commented_code.txt")
|
| 310 |
+
# Save to chat history
|
| 311 |
+
st.session_state.comment_history.append({
|
| 312 |
+
"timestamp": str(datetime.datetime.now()),
|
| 313 |
+
"user_code": code_input,
|
| 314 |
+
"params": {
|
| 315 |
+
"language": programming_language
|
| 316 |
+
},
|
| 317 |
+
"commented_code": commented_code
|
| 318 |
+
})
|
| 319 |
+
# Show chat history for code comments
|
| 320 |
+
st.markdown("### Code Comment Chat History")
|
| 321 |
+
if st.button("Clear Comment History"):
|
| 322 |
+
st.session_state.comment_history = []
|
| 323 |
+
for entry in reversed(st.session_state.comment_history):
|
| 324 |
+
st.markdown(f"**[{entry['timestamp']}]**")
|
| 325 |
+
st.code(entry["user_code"], language=entry["params"]["language"].lower())
|
| 326 |
+
st.markdown("**Commented Code:**")
|
| 327 |
+
st.code(entry["commented_code"], language=entry["params"]["language"].lower())
|
| 328 |
+
st.markdown("---")
|
| 329 |
+
|
| 330 |
+
st.markdown("---")
|
| 331 |
+
st.write("Powered by AnimateDiff (zeroscope), InsightFace, and moviepy.")
|
| 332 |
+
|
| 333 |
+
def split_code_into_chunks(code, lang):
|
| 334 |
+
if lang.lower() == "python":
|
| 335 |
+
# Corrected regex pattern for Python code splitting
|
| 336 |
+
pattern = r'(def\s+\w+\(.*?\):|class\s+\w+\(.*?\)?:)'
|
| 337 |
+
splits = re.split(pattern, code)
|
| 338 |
+
chunks = []
|
| 339 |
+
for i in range(1, len(splits), 2):
|
| 340 |
+
header = splits[i]
|
| 341 |
+
body = splits[i+1] if (i+1) < len(splits) else ""
|
| 342 |
+
chunks.append(header + body)
|
| 343 |
+
return chunks if chunks else [code]
|
| 344 |
+
else:
|
| 345 |
+
return [code]
|