Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,148 +1,119 @@
|
|
1 |
-
import streamlit as st
|
2 |
import difflib
|
3 |
-
import
|
4 |
-
import re
|
5 |
-
import hashlib
|
6 |
from groq import Groq
|
|
|
7 |
|
8 |
-
# ---
|
9 |
-
st.set_page_config(page_title="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# --- Groq API Setup ---
|
12 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
13 |
if not GROQ_API_KEY:
|
14 |
-
st.error("
|
15 |
st.stop()
|
16 |
client = Groq(api_key=GROQ_API_KEY)
|
17 |
|
18 |
-
# ---
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
return embedding
|
28 |
-
|
29 |
-
def cosine_similarity(vec1, vec2):
|
30 |
-
dot = sum(a*b for a,b in zip(vec1, vec2))
|
31 |
-
norm1 = sum(a*a for a in vec1) ** 0.5
|
32 |
-
norm2 = sum(b*b for b in vec2) ** 0.5
|
33 |
-
return dot / (norm1 * norm2 + 1e-8)
|
34 |
-
|
35 |
-
def split_code_into_chunks(code, lang):
|
36 |
-
if lang.lower() == "python":
|
37 |
-
pattern = r'(def\s+\w+\(.*?\):|class\s+\w+\(?.*?\)?:)'
|
38 |
-
splits = re.split(pattern, code)
|
39 |
-
chunks = []
|
40 |
-
for i in range(1, len(splits), 2):
|
41 |
-
header = splits[i]
|
42 |
-
body = splits[i+1] if (i+1) < len(splits) else ""
|
43 |
-
chunks.append(header + body)
|
44 |
-
return chunks if chunks else [code]
|
45 |
-
else:
|
46 |
-
return [code]
|
47 |
|
48 |
-
def
|
49 |
-
|
50 |
messages=[{"role": "user", "content": prompt}],
|
51 |
model="llama3-70b-8192",
|
52 |
)
|
53 |
-
return
|
54 |
-
|
55 |
-
def
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
combined_code = "\n\n".join(top_chunks)
|
66 |
-
prompt = (
|
67 |
-
f"You are a friendly and insightful {lang} expert helping a {skill} {role}.\n"
|
68 |
-
f"Based on these relevant code snippets:\n{combined_code}\n"
|
69 |
-
f"Answer this question in {explain_lang}:\n{question}\n"
|
70 |
-
f"Explain which parts handle the question and how to modify them if needed."
|
71 |
-
)
|
72 |
-
return groq_call(prompt)
|
73 |
-
|
74 |
-
def error_detection_and_fixes(refactored_code, lang, skill, role, explain_lang):
|
75 |
-
prompt = (
|
76 |
-
f"You are a senior {lang} developer. Analyze this code for bugs, security flaws, "
|
77 |
-
f"and performance issues. Suggest fixes with explanations in {explain_lang}:\n\n{refactored_code}"
|
78 |
-
)
|
79 |
-
return groq_call(prompt)
|
80 |
-
|
81 |
-
def agentic_workflow(code, skill_level, programming_language, explanation_language, user_role):
|
82 |
-
timeline = []
|
83 |
-
suggestions = []
|
84 |
-
|
85 |
-
# Explanation
|
86 |
-
explain_prompt = (
|
87 |
-
f"You are a friendly and insightful {programming_language} expert helping a {skill_level} {user_role}. "
|
88 |
-
f"Explain this code in {explanation_language} with clear examples, analogies, and why each part matters:\n\n{code}"
|
89 |
-
)
|
90 |
-
explanation = groq_call(explain_prompt)
|
91 |
-
timeline.append({"step": "Explain", "description": "Detailed explanation", "output": explanation, "code": code})
|
92 |
-
suggestions.append("Consider refactoring your code to improve readability and performance.")
|
93 |
-
|
94 |
-
# Refactor
|
95 |
-
refactor_prompt = (
|
96 |
-
f"Refactor this {programming_language} code. Explain the changes like a mentor helping a {skill_level} {user_role}. "
|
97 |
-
f"Include best practices and improvements:\n\n{code}"
|
98 |
-
)
|
99 |
-
refactor_response = groq_call(refactor_prompt)
|
100 |
-
if "```" in refactor_response:
|
101 |
-
parts = refactor_response.split("```")
|
102 |
-
refactored_code = ""
|
103 |
-
for part in parts:
|
104 |
-
if part.strip().startswith(programming_language.lower()):
|
105 |
-
refactored_code = part.strip().split('\n', 1)[1] if '\n' in part else ""
|
106 |
-
break
|
107 |
-
if not refactored_code:
|
108 |
-
refactored_code = refactor_response
|
109 |
-
else:
|
110 |
-
refactored_code = refactor_response
|
111 |
-
timeline.append({"step": "Refactor", "description": "Refactored code with improvements", "output": refactored_code, "code": refactored_code})
|
112 |
-
suggestions.append("Review the refactored code and adapt it to your style or project needs.")
|
113 |
-
|
114 |
-
# Review
|
115 |
-
review_prompt = (
|
116 |
-
f"As a senior {programming_language} developer, review the refactored code. "
|
117 |
-
f"Give constructive feedback on strengths, weaknesses, performance, security, and improvements in {explanation_language}:\n\n{refactored_code}"
|
118 |
-
)
|
119 |
-
review = groq_call(review_prompt)
|
120 |
-
timeline.append({"step": "Review", "description": "Code review and suggestions", "output": review, "code": refactored_code})
|
121 |
-
suggestions.append("Incorporate review feedback for cleaner, robust code.")
|
122 |
-
|
123 |
-
# Error detection & fixes
|
124 |
-
errors = error_detection_and_fixes(refactored_code, programming_language, skill_level, user_role, explanation_language)
|
125 |
-
timeline.append({"step": "Error Detection", "description": "Bugs, security, performance suggestions", "output": errors, "code": refactored_code})
|
126 |
-
suggestions.append("Apply fixes to improve code safety and performance.")
|
127 |
-
|
128 |
-
# Test generation
|
129 |
-
test_prompt = (
|
130 |
-
f"Write clear, effective unit tests for this {programming_language} code. "
|
131 |
-
f"Explain what each test does in {explanation_language}, for a {skill_level} {user_role}:\n\n{refactored_code}"
|
132 |
-
)
|
133 |
-
tests = groq_call(test_prompt)
|
134 |
-
timeline.append({"step": "Test Generation", "description": "Generated unit tests", "output": tests, "code": tests})
|
135 |
-
suggestions.append("Run generated tests locally to validate changes.")
|
136 |
-
|
137 |
-
return timeline, suggestions
|
138 |
-
|
139 |
-
def get_inline_diff_html(original, modified):
|
140 |
differ = difflib.HtmlDiff(tabsize=4, wrapcolumn=80)
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
)
|
145 |
-
|
|
|
|
|
|
|
146 |
|
147 |
def detect_code_type(code, programming_language):
|
148 |
backend_keywords = [
|
@@ -172,156 +143,258 @@ def detect_code_type(code, programming_language):
|
|
172 |
return 'frontend'
|
173 |
return 'unknown'
|
174 |
|
175 |
-
def code_complexity(code):
|
176 |
-
lines = code.count('\n') + 1
|
177 |
-
functions = code.count('def ')
|
178 |
-
classes = code.count('class ')
|
179 |
-
comments = code.count('#')
|
180 |
-
return f"Lines: {lines}, Functions: {functions}, Classes: {classes}, Comments: {comments}"
|
181 |
-
|
182 |
def code_matches_language(code: str, language: str) -> bool:
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
"
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
"
|
198 |
-
|
199 |
-
"System.out.println", "try {", "catch(", "throw new ", "implements ", "extends ",
|
200 |
-
"@Override", "interface ", "enum ", "synchronized ", "final ",
|
201 |
-
],
|
202 |
-
"c#": [
|
203 |
-
"using System", "namespace ", "class ", "interface ", "public static void Main",
|
204 |
-
"Console.WriteLine", "try {", "catch(", "throw ", "async ", "await ", "get;", "set;",
|
205 |
-
"List<", "Dictionary<", "[Serializable]", "[Obsolete]",
|
206 |
-
],
|
207 |
-
"javascript": [
|
208 |
-
"function ", "const ", "let ", "var ", "document.", "window.", "console.log",
|
209 |
-
"if(", "for(", "while(", "switch(", "try {", "catch(", "export ", "import ", "async ",
|
210 |
-
"await ", "=>", "this.", "class ", "prototype", "new ", "$(",
|
211 |
-
],
|
212 |
-
"typescript": [
|
213 |
-
"function ", "const ", "let ", "interface ", "type ", ": string", ": number", ": boolean",
|
214 |
-
"implements ", "extends ", "enum ", "public ", "private ", "protected ", "readonly ",
|
215 |
-
"import ", "export ", "console.log", "async ", "await ", "=>", "this.",
|
216 |
-
],
|
217 |
-
"html": [
|
218 |
-
"<!doctype html", "<html", "<head>", "<body>", "<script", "<style", "<meta ", "<link ",
|
219 |
-
"<title>", "<div", "<span", "<p>", "<h1>", "<ul>", "<li>", "<form", "<input", "<button",
|
220 |
-
"<table", "<footer", "<header", "<section", "<article", "<nav", "<img", "<a ", "</html>",
|
221 |
-
],
|
222 |
-
}
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
# Require at least one pattern to match for validation to succeed
|
228 |
-
return match_count >= 1
|
229 |
-
|
230 |
-
|
231 |
-
# --- Sidebar ---
|
232 |
-
st.sidebar.title("🔧 Configuration")
|
233 |
-
lang = st.sidebar.selectbox("Programming Language", ["Python", "JavaScript", "C++", "Java", "C#", "TypeScript"])
|
234 |
-
skill = st.sidebar.selectbox("Skill Level", ["Beginner", "Intermediate", "Expert"])
|
235 |
-
role = st.sidebar.selectbox("Your Role", ["Student", "Frontend Developer", "Backend Developer", "Data Scientist"])
|
236 |
-
explain_lang = st.sidebar.selectbox("Explanation Language", ["English", "Spanish", "Chinese", "Urdu"])
|
237 |
-
st.sidebar.markdown("---")
|
238 |
-
st.sidebar.markdown("<span style='color:#fff;'>Powered by <b>BLACKBOX.AI</b></span>", unsafe_allow_html=True)
|
239 |
-
|
240 |
-
tabs = st.tabs(["🧠 Full AI Workflow", "🔍 Semantic Search"])
|
241 |
-
# --- Tab 1: Full AI Workflow ---
|
242 |
-
with tabs[0]:
|
243 |
-
st.title("🧠 Full AI Workflow")
|
244 |
-
file_types = {
|
245 |
-
"Python": ["py"],
|
246 |
-
"JavaScript": ["js"],
|
247 |
-
"C++": ["cpp", "h", "hpp"],
|
248 |
-
"Java": ["java"],
|
249 |
-
"C#": ["cs"],
|
250 |
-
"TypeScript": ["ts"],
|
251 |
-
}
|
252 |
|
253 |
-
|
254 |
-
f"
|
255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
)
|
257 |
-
|
258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
else:
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
else:
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
# --- Tab 2: Semantic Search ---
|
314 |
-
with tabs[1]:
|
315 |
-
st.title("🔍 Semantic Search")
|
316 |
-
sem_code = st.text_area("Your Code", height=300, placeholder="Paste your code...")
|
317 |
-
sem_q = st.text_input("Your Question", placeholder="E.g., What does this function do?")
|
318 |
-
if st.button("Run Semantic Search"):
|
319 |
-
if not sem_code.strip() or not sem_q.strip():
|
320 |
-
st.warning("Code and question required.")
|
321 |
else:
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
st.markdown("---")
|
|
|
|
|
|
1 |
import difflib
|
2 |
+
import streamlit as st
|
|
|
|
|
3 |
from groq import Groq
|
4 |
+
import os
|
5 |
|
6 |
+
# --- Set page config FIRST! ---
|
7 |
+
st.set_page_config(page_title="AI Code Assistant", layout="wide")
|
8 |
+
|
9 |
+
# --- Custom CSS for Professional Look ---
|
10 |
+
st.markdown("""
|
11 |
+
<style>
|
12 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
|
13 |
+
html, body, [class*="css"] {
|
14 |
+
font-family: 'Inter', sans-serif;
|
15 |
+
background-color: #f7f9fb;
|
16 |
+
}
|
17 |
+
.stApp {
|
18 |
+
background-color: #f7f9fb;
|
19 |
+
}
|
20 |
+
.stSidebar {
|
21 |
+
background-color: #22304a !important;
|
22 |
+
}
|
23 |
+
.stButton>button {
|
24 |
+
background-color: #22304a;
|
25 |
+
color: #fff;
|
26 |
+
border-radius: 6px;
|
27 |
+
border: none;
|
28 |
+
font-weight: 600;
|
29 |
+
padding: 0.5rem 1.5rem;
|
30 |
+
margin-top: 0.5rem;
|
31 |
+
margin-bottom: 0.5rem;
|
32 |
+
transition: background 0.2s;
|
33 |
+
}
|
34 |
+
.stButton>button:hover {
|
35 |
+
background-color: #1a2333;
|
36 |
+
color: #fff;
|
37 |
+
}
|
38 |
+
.stTextInput>div>div>input, .stTextArea>div>textarea {
|
39 |
+
background: #fff;
|
40 |
+
border: 1px solid #d1d5db;
|
41 |
+
border-radius: 6px;
|
42 |
+
color: #22304a;
|
43 |
+
font-size: 1rem;
|
44 |
+
}
|
45 |
+
.stDownloadButton>button {
|
46 |
+
background-color: #22304a;
|
47 |
+
color: #fff;
|
48 |
+
border-radius: 6px;
|
49 |
+
border: none;
|
50 |
+
font-weight: 600;
|
51 |
+
padding: 0.5rem 1.5rem;
|
52 |
+
margin-top: 0.5rem;
|
53 |
+
margin-bottom: 0.5rem;
|
54 |
+
transition: background 0.2s;
|
55 |
+
}
|
56 |
+
.stDownloadButton>button:hover {
|
57 |
+
background-color: #1a2333;
|
58 |
+
color: #fff;
|
59 |
+
}
|
60 |
+
.stExpanderHeader {
|
61 |
+
font-weight: 600;
|
62 |
+
color: #22304a;
|
63 |
+
font-size: 1.1rem;
|
64 |
+
}
|
65 |
+
.stMarkdown {
|
66 |
+
color: #22304a;
|
67 |
+
}
|
68 |
+
.stAlert {
|
69 |
+
border-radius: 6px;
|
70 |
+
}
|
71 |
+
</style>
|
72 |
+
""", unsafe_allow_html=True)
|
73 |
|
74 |
# --- Groq API Setup ---
|
75 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
76 |
if not GROQ_API_KEY:
|
77 |
+
st.error("GROQ_API_KEY environment variable not set. Please set it in your Hugging Face Space secrets.")
|
78 |
st.stop()
|
79 |
client = Groq(api_key=GROQ_API_KEY)
|
80 |
|
81 |
+
# --- Blackbox AI Agent Setup ---
|
82 |
+
BLACKBOX_API_KEY = os.environ.get("BLACKBOX_API_KEY")
|
83 |
+
if not BLACKBOX_API_KEY:
|
84 |
+
st.error("BLACKBOX_API_KEY environment variable not set. Please set it in your Hugging Face Space secrets.")
|
85 |
+
st.stop()
|
86 |
+
|
87 |
+
# Chat history management
|
88 |
+
if "chat_history" not in st.session_state:
|
89 |
+
st.session_state.chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
def groq_api_call(prompt):
|
92 |
+
chat_completion = client.chat.completions.create(
|
93 |
messages=[{"role": "user", "content": prompt}],
|
94 |
model="llama3-70b-8192",
|
95 |
)
|
96 |
+
return chat_completion.choices[0].message.content
|
97 |
+
|
98 |
+
def blackbox_ai_call(messages):
|
99 |
+
# This is a placeholder for actual Blackbox AI API call using BLACKBOX_API_KEY
|
100 |
+
# For demonstration, we simulate a response by echoing last user message
|
101 |
+
last_user_message = messages[-1]["content"] if messages else ""
|
102 |
+
response = f"Blackbox AI response to: {last_user_message}"
|
103 |
+
return response
|
104 |
+
|
105 |
+
def get_diff_html(original, modified):
|
106 |
+
original_lines = original.splitlines()
|
107 |
+
modified_lines = modified.splitlines()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
differ = difflib.HtmlDiff(tabsize=4, wrapcolumn=80)
|
109 |
+
return differ.make_table(original_lines, modified_lines, "Original", "Modified", context=True, numlines=2)
|
110 |
+
|
111 |
+
def code_complexity(code):
|
112 |
+
lines = code.count('\n') + 1
|
113 |
+
functions = code.count('def ')
|
114 |
+
classes = code.count('class ')
|
115 |
+
comments = code.count('#')
|
116 |
+
return f"Lines: {lines}, Functions: {functions}, Classes: {classes}, Comments: {comments}"
|
117 |
|
118 |
def detect_code_type(code, programming_language):
|
119 |
backend_keywords = [
|
|
|
143 |
return 'frontend'
|
144 |
return 'unknown'
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
def code_matches_language(code: str, language: str) -> bool:
|
147 |
+
code = code.strip().lower()
|
148 |
+
if language.lower() == "python":
|
149 |
+
return "def " in code or "import " in code or ".py" in code
|
150 |
+
if language.lower() == "c++":
|
151 |
+
return "#include" in code or "int main" in code or ".cpp" in code or "std::" in code
|
152 |
+
if language.lower() == "java":
|
153 |
+
return "public class" in code or "public static void main" in code or ".java" in code
|
154 |
+
if language.lower() == "c#":
|
155 |
+
return "using system" in code or "namespace" in code or ".cs" in code
|
156 |
+
if language.lower() == "javascript":
|
157 |
+
return "function " in code or "const " in code or "let " in code or "var " in code or ".js" in code
|
158 |
+
if language.lower() == "typescript":
|
159 |
+
return "function " in code or "const " in code or "let " in code or "var " in code or ": string" in code or ".ts" in code
|
160 |
+
if language.lower() == "html":
|
161 |
+
return "<html" in code or "<!doctype html" in code
|
162 |
+
return True # fallback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
def agentic_workflow(code, skill_level, programming_language, explanation_language, user_role):
|
165 |
+
timeline = []
|
166 |
+
suggestions = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
explain_prompt = (
|
169 |
+
f"Explain the following {programming_language} code line by line or function by function "
|
170 |
+
f"for a {skill_level.lower()} {user_role} in {explanation_language}:\n{code}"
|
171 |
+
)
|
172 |
+
explanation = groq_api_call(explain_prompt)
|
173 |
+
timeline.append({
|
174 |
+
"step": "Explain",
|
175 |
+
"description": "Step-by-step explanation of your code.",
|
176 |
+
"output": explanation,
|
177 |
+
"code": code
|
178 |
+
})
|
179 |
+
suggestions.append("Refactor your code for better readability and performance.")
|
180 |
+
|
181 |
+
refactor_prompt = (
|
182 |
+
f"Refactor the following {programming_language} code for better readability, performance, and structure. "
|
183 |
+
f"Explain what changes you made and why, for a {skill_level.lower()} {user_role} in {explanation_language}:\n{code}"
|
184 |
)
|
185 |
+
refactor_response = groq_api_call(refactor_prompt)
|
186 |
+
if "```" in refactor_response:
|
187 |
+
parts = refactor_response.split("```")
|
188 |
+
refactor_explanation = parts[0].strip()
|
189 |
+
refactored_code = ""
|
190 |
+
for i in range(1, len(parts)):
|
191 |
+
if parts[i].strip().startswith(programming_language.lower()):
|
192 |
+
refactored_code = parts[i].strip().split('\n', 1)[1] if '\n' in parts[i] else ""
|
193 |
+
break
|
194 |
+
elif i == 1:
|
195 |
+
refactored_code = parts[i].strip().split('\n', 1)[1] if '\n' in parts[i] else ""
|
196 |
+
if not refactored_code:
|
197 |
+
refactored_code = refactor_response.strip()
|
198 |
else:
|
199 |
+
refactor_explanation = "Refactored code below."
|
200 |
+
refactored_code = refactor_response.strip()
|
201 |
+
timeline.append({
|
202 |
+
"step": "Refactor",
|
203 |
+
"description": refactor_explanation,
|
204 |
+
"output": refactored_code,
|
205 |
+
"code": refactored_code
|
206 |
+
})
|
207 |
+
suggestions.append("Review the refactored code for best practices and improvements.")
|
208 |
+
|
209 |
+
review_prompt = (
|
210 |
+
f"Provide a code review for the following {programming_language} code. "
|
211 |
+
f"Include feedback on best practices, code smells, optimization, and security issues, for a {skill_level.lower()} {user_role} in {explanation_language}:\n{refactored_code}"
|
212 |
+
)
|
213 |
+
review_feedback = groq_api_call(review_prompt)
|
214 |
+
timeline.append({
|
215 |
+
"step": "Review",
|
216 |
+
"description": "AI code review and feedback.",
|
217 |
+
"output": review_feedback,
|
218 |
+
"code": refactored_code
|
219 |
+
})
|
220 |
+
suggestions.append("Generate unit tests for your code.")
|
221 |
+
|
222 |
+
test_prompt = (
|
223 |
+
f"Write unit tests for the following {programming_language} code. "
|
224 |
+
f"Use pytest style and cover all functions. For a {skill_level.lower()} {user_role} in {explanation_language}:\n{refactored_code}"
|
225 |
+
)
|
226 |
+
test_code = groq_api_call(test_prompt)
|
227 |
+
timeline.append({
|
228 |
+
"step": "Test Generation",
|
229 |
+
"description": "AI-generated unit tests for your code.",
|
230 |
+
"output": test_code,
|
231 |
+
"code": test_code
|
232 |
+
})
|
233 |
+
suggestions.append("Run the generated tests in your local environment.")
|
234 |
+
|
235 |
+
return timeline, suggestions
|
236 |
+
|
237 |
+
st.markdown(
|
238 |
+
"<h2 style='text-align: center; color: #22304a; font-weight: 600; margin-bottom: 0.5em;'>AI Code Assistant</h2>",
|
239 |
+
unsafe_allow_html=True
|
240 |
+
)
|
241 |
+
|
242 |
+
with st.sidebar:
|
243 |
+
st.title("Settings")
|
244 |
+
programming_language = st.selectbox(
|
245 |
+
"Programming Language",
|
246 |
+
["Python", "C++", "Java", "C#", "JavaScript", "TypeScript", "HTML"]
|
247 |
+
)
|
248 |
+
explanation_language = st.selectbox(
|
249 |
+
"Explanation Language",
|
250 |
+
["English", "Urdu", "Chinese", "Spanish"]
|
251 |
+
)
|
252 |
+
skill_level = st.selectbox("Skill Level", ["Beginner", "Intermediate", "Expert"])
|
253 |
+
user_role = st.selectbox(
|
254 |
+
"Choose Role",
|
255 |
+
["Data Scientist", "Backend Developer", "Frontend Developer", "Student"]
|
256 |
+
)
|
257 |
+
st.markdown("---")
|
258 |
+
st.markdown("<span style='color:#fff;'>Powered by <b>BLACKBOX.AI</b></span>", unsafe_allow_html=True)
|
259 |
+
|
260 |
+
if "code" not in st.session_state:
|
261 |
+
st.session_state.code = ""
|
262 |
+
if "timeline" not in st.session_state:
|
263 |
+
st.session_state.timeline = []
|
264 |
+
if "suggestions" not in st.session_state:
|
265 |
+
st.session_state.suggestions = []
|
266 |
+
if "chat_history" not in st.session_state:
|
267 |
+
st.session_state.chat_history = []
|
268 |
+
|
269 |
+
col1, col2 = st.columns([2, 3], gap="large")
|
270 |
+
|
271 |
+
with col1:
|
272 |
+
st.subheader(f"{programming_language} Code")
|
273 |
+
uploaded_file = st.file_uploader(f"Upload .{programming_language.lower()} file", type=[programming_language.lower()])
|
274 |
+
code_input = st.text_area(
|
275 |
+
"Paste or edit your code here:",
|
276 |
+
height=300,
|
277 |
+
value=st.session_state.code,
|
278 |
+
key="main_code_input"
|
279 |
+
)
|
280 |
+
if uploaded_file is not None:
|
281 |
+
code = uploaded_file.read().decode("utf-8")
|
282 |
+
st.session_state.code = code
|
283 |
+
st.success("File uploaded successfully.")
|
284 |
+
elif code_input:
|
285 |
+
st.session_state.code = code_input
|
286 |
+
|
287 |
+
st.markdown(f"<b>Complexity:</b> {code_complexity(st.session_state.code)}", unsafe_allow_html=True)
|
288 |
+
|
289 |
+
st.markdown("---")
|
290 |
+
st.markdown("#### Agent Suggestions")
|
291 |
+
for suggestion in st.session_state.suggestions[-3:]:
|
292 |
+
st.markdown(f"- {suggestion}")
|
293 |
+
|
294 |
+
st.markdown("---")
|
295 |
+
st.markdown("#### Download Full Report")
|
296 |
+
if st.session_state.timeline:
|
297 |
+
report = ""
|
298 |
+
for step in st.session_state.timeline:
|
299 |
+
report += f"## {step['step']}\n{step['description']}\n\n{step['output']}\n\n"
|
300 |
+
st.download_button("Download Report", report, file_name="ai_code_assistant_report.txt")
|
301 |
+
|
302 |
+
with col2:
|
303 |
+
st.subheader("Agentic Workflow")
|
304 |
+
if st.button("Run Full AI Agent Workflow"):
|
305 |
+
if not st.session_state.code.strip():
|
306 |
+
st.warning("Please enter or upload code first.")
|
307 |
+
else:
|
308 |
+
# Language check
|
309 |
+
if not code_matches_language(st.session_state.code, programming_language):
|
310 |
+
st.error(f"It looks like you provided code in a different language. Please provide {programming_language} code.")
|
311 |
else:
|
312 |
+
code_type = detect_code_type(st.session_state.code, programming_language)
|
313 |
+
# Role/code type enforcement
|
314 |
+
if code_type == "data_science" and user_role != "Data Scientist":
|
315 |
+
st.error("It looks like you provided data science code. Please select 'Data Scientist' as your role.")
|
316 |
+
elif code_type == "frontend" and user_role != "Frontend Developer":
|
317 |
+
st.error("It looks like you provided frontend code. Please select 'Frontend Developer' as your role.")
|
318 |
+
elif code_type == "backend" and user_role != "Backend Developer":
|
319 |
+
st.error("It looks like you provided backend code. Please select 'Backend Developer' as your role.")
|
320 |
+
elif code_type == "unknown":
|
321 |
+
st.warning("Could not determine the code type. Please make sure your code is complete and clear.")
|
322 |
+
else:
|
323 |
+
with st.spinner("AI Agent is working through all steps..."):
|
324 |
+
timeline, suggestions = agentic_workflow(
|
325 |
+
st.session_state.code,
|
326 |
+
skill_level,
|
327 |
+
programming_language,
|
328 |
+
explanation_language,
|
329 |
+
user_role
|
330 |
+
)
|
331 |
+
st.session_state.timeline = timeline
|
332 |
+
st.session_state.suggestions = suggestions
|
333 |
+
st.success("Agentic workflow complete. See timeline below.")
|
334 |
+
|
335 |
+
# Chatbox with history using Blackbox AI agent
|
336 |
+
st.subheader("Chat with Blackbox AI Agent")
|
337 |
+
user_input = st.text_input("Enter your message:", key="chat_input")
|
338 |
+
if user_input:
|
339 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
340 |
+
response = blackbox_ai_call(st.session_state.chat_history)
|
341 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
342 |
+
|
343 |
+
for chat in st.session_state.chat_history:
|
344 |
+
if chat["role"] == "user":
|
345 |
+
st.markdown(f"**You:** {chat['content']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
else:
|
347 |
+
st.markdown(f"**Blackbox AI:** {chat['content']}")
|
348 |
+
|
349 |
+
# --- Semantic Search with history ---
|
350 |
+
st.markdown("---")
|
351 |
+
st.subheader("Semantic Search with Contextual History")
|
352 |
+
|
353 |
+
if "semantic_search_history" not in st.session_state:
|
354 |
+
st.session_state.semantic_search_history = []
|
355 |
+
|
356 |
+
sem_code = st.text_area("Your Code for Semantic Search", height=300, placeholder="Paste your code here...")
|
357 |
+
sem_question = st.text_input("Ask a question about your code:")
|
358 |
+
|
359 |
+
if st.button("Ask Semantic Search"):
|
360 |
+
if not sem_code.strip() or not sem_question.strip():
|
361 |
+
st.warning("Please provide both code and a question.")
|
362 |
+
else:
|
363 |
+
# Append current question to history
|
364 |
+
st.session_state.semantic_search_history.append({"question": sem_question, "answer": None})
|
365 |
+
|
366 |
+
# Build context from history
|
367 |
+
context = ""
|
368 |
+
for entry in st.session_state.semantic_search_history:
|
369 |
+
if entry["answer"]:
|
370 |
+
context += f"Q: {entry['question']}\nA: {entry['answer']}\n\n"
|
371 |
+
else:
|
372 |
+
context += f"Q: {entry['question']}\n"
|
373 |
+
|
374 |
+
# Combine context with current code and question
|
375 |
+
prompt = (
|
376 |
+
f"You are a helpful {programming_language} expert assisting a user.\n"
|
377 |
+
f"Here is the code:\n{sem_code}\n\n"
|
378 |
+
f"Conversation history:\n{context}\n"
|
379 |
+
f"Please answer the latest question."
|
380 |
+
)
|
381 |
+
|
382 |
+
# Call Blackbox AI agent with accumulated context
|
383 |
+
# For demonstration, we use semantic_search_improved as placeholder
|
384 |
+
answer = semantic_search_improved(sem_code, sem_question, programming_language, skill_level, user_role, explanation_language)
|
385 |
+
|
386 |
+
# Update the last answer in history
|
387 |
+
st.session_state.semantic_search_history[-1]["answer"] = answer
|
388 |
+
|
389 |
+
st.markdown("### Answer")
|
390 |
+
st.markdown(answer)
|
391 |
+
|
392 |
+
if st.session_state.semantic_search_history:
|
393 |
+
st.markdown("### Semantic Search History")
|
394 |
+
for entry in st.session_state.semantic_search_history:
|
395 |
+
st.markdown(f"**Q:** {entry['question']}")
|
396 |
+
if entry["answer"]:
|
397 |
+
st.markdown(f"**A:** {entry['answer']}")
|
398 |
|
399 |
st.markdown("---")
|
400 |
+
st.markdown('<div style="text-align: center; color: #22304a; font-size: 1rem; margin-top: 2em;">Powered by <b>BLACKBOX.AI</b></div>', unsafe_allow_html=True)
|