Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import difflib | |
| import requests | |
| import datetime | |
| import streamlit.components.v1 as components | |
| # --- CONFIG --- | |
| # Place your API keys here | |
| GROQ_API_KEY = st.secrets.get('GROQ_API_KEY', 'YOUR_GROQ_API_KEY') | |
| BLACKBOX_API_KEY = st.secrets.get('BLACKBOX_API_KEY', 'YOUR_BLACKBOX_API_KEY') | |
| PROGRAMMING_LANGUAGES = ["Python", "JavaScript", "TypeScript", "Java", "C++", "C#"] | |
| SKILL_LEVELS = ["Beginner", "Intermediate", "Expert"] | |
| USER_ROLES = ["Student", "Frontend Developer", "Backend Developer", "Data Scientist"] | |
| EXPLANATION_LANGUAGES = ["English", "Spanish", "Chinese", "Urdu"] | |
| EXAMPLE_QUESTIONS = [ | |
| "What does this function do?", | |
| "How can I optimize this code?", | |
| "What are the potential bugs in this code?", | |
| "How does this algorithm work?", | |
| "What design patterns are used here?", | |
| "How can I make this code more readable?" | |
| ] | |
| # --- API STUBS --- | |
| def call_groq_api(prompt, model="llama3-70b-8192"): | |
| # Replace with actual Groq API call | |
| headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"} | |
| data = {"model": model, "messages": [{"role": "user", "content": prompt}]} | |
| response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=data, headers=headers) | |
| if response.status_code == 200: | |
| return response.json()['choices'][0]['message']['content'] | |
| else: | |
| return f"[Groq API Error] {response.text}" | |
| def call_blackbox_agent(messages): | |
| url = "https://api.code.blackbox.ai/v1/chat/completions" | |
| headers = { | |
| "Content-Type": "application/json", | |
| "Authorization": f"Bearer {BLACKBOX_API_KEY}" | |
| } | |
| data = { | |
| "model": "code-chat", | |
| "messages": messages | |
| } | |
| response = requests.post(url, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["message"]["content"] | |
| else: | |
| return call_groq_api(messages[-1]["content"]) | |
| # --- UTILS --- | |
| def code_matches_language(code, language): | |
| # Simple heuristic, can be improved | |
| if language.lower() in code.lower(): | |
| return True | |
| return True # For demo, always True | |
| def calculate_code_complexity(code): | |
| # Dummy complexity metric | |
| lines = code.count('\n') + 1 | |
| return f"{lines} lines" | |
| def get_inline_diff(original, modified): | |
| diff = difflib.unified_diff( | |
| original.splitlines(), | |
| modified.splitlines(), | |
| lineterm='', | |
| fromfile='Original', | |
| tofile='Refactored' | |
| ) | |
| return '\n'.join(diff) | |
| def is_coding_question(question): | |
| """ | |
| Uses Blackbox AI agent to check if the question is about programming/code. | |
| Returns True if yes, False otherwise. | |
| """ | |
| messages = [ | |
| {"role": "system", "content": "You are a helpful coding assistant."}, | |
| {"role": "user", "content": f"Is the following question about programming or code? Answer only 'yes' or 'no'. Question: {question}"} | |
| ] | |
| try: | |
| response = call_blackbox_agent(messages) | |
| return 'yes' in response.lower() | |
| except Exception: | |
| return False | |
| # --- STREAMLIT APP --- | |
| st.set_page_config(page_title="AI Workflow App", layout="wide") | |
| st.title("AI Assistant with Workflow (Streamlit Edition)") | |
| # Navigation | |
| page = st.sidebar.radio("Navigate", ["Home", "AI Workflow", "Semantic Search", "Code Comment Generator"]) | |
| if page == "Home": | |
| st.header("Welcome to the AI Assistant!") | |
| st.markdown(""" | |
| - **Full AI Workflow:** Complete code analysis pipeline with explanation, refactoring, review, and testing (powered by Groq/Blackbox) | |
| - **Semantic Search:** Ask natural language questions about your code and get intelligent answers | |
| """) | |
| st.info("Select a feature from the sidebar to get started.") | |
| elif page == "AI Workflow": | |
| st.header("Full AI Workflow") | |
| code_input = st.text_area("Paste your code here", height=200) | |
| uploaded_file = st.file_uploader("Or upload a code file", type=["py", "js", "ts", "java", "cpp", "cs"]) | |
| if uploaded_file: | |
| code_input = uploaded_file.read().decode("utf-8") | |
| st.text_area("File content", code_input, height=200, key="file_content") | |
| col1, col2, col3, col4 = st.columns(4) | |
| with col1: | |
| programming_language = st.selectbox("Programming Language", PROGRAMMING_LANGUAGES) | |
| with col2: | |
| skill_level = st.selectbox("Skill Level", SKILL_LEVELS) | |
| with col3: | |
| user_role = st.selectbox("Your Role", USER_ROLES) | |
| with col4: | |
| explanation_language = st.selectbox("Explanation Language", EXPLANATION_LANGUAGES) | |
| if code_input: | |
| st.caption(f"Complexity: {calculate_code_complexity(code_input)}") | |
| if st.button("Run Workflow", type="primary"): | |
| if not code_input.strip(): | |
| st.error("Please paste or upload your code.") | |
| elif not code_matches_language(code_input, programming_language): | |
| st.error(f"Language mismatch. Please check your code and language selection.") | |
| else: | |
| with st.spinner("Running AI Workflow..."): | |
| # Simulate workflow steps | |
| steps = [ | |
| ("Explain", call_groq_api(f"Explain this {programming_language} code for a {skill_level} {user_role} in {explanation_language}:\n{code_input}")), | |
| ("Refactor", call_blackbox_agent([ | |
| {"role": "system", "content": "You are a helpful coding assistant."}, | |
| {"role": "user", "content": f"Refactor this {programming_language} code: {code_input}"} | |
| ])), | |
| ("Review", call_groq_api(f"Review this {programming_language} code for errors and improvements: {code_input}")), | |
| ("ErrorDetection", call_groq_api(f"Find bugs in this {programming_language} code: {code_input}")), | |
| ("TestGeneration", call_groq_api(f"Generate tests for this {programming_language} code: {code_input}")), | |
| ] | |
| timeline = [] | |
| for step, output in steps: | |
| timeline.append({"step": step, "output": output}) | |
| st.success("Workflow complete!") | |
| for t in timeline: | |
| st.subheader(t["step"]) | |
| st.write(t["output"]) | |
| # Show code diff (dummy for now) | |
| st.subheader("Code Diff (Original vs Refactored)") | |
| refactored_code = steps[1][1] # Blackbox agent output | |
| st.code(get_inline_diff(code_input, refactored_code), language=programming_language.lower()) | |
| # Download report | |
| report = f"AI Workflow Report\nGenerated on: {datetime.datetime.now()}\nLanguage: {programming_language}\nSkill Level: {skill_level}\nRole: {user_role}\n\n" | |
| for t in timeline: | |
| report += f"## {t['step']}\n{t['output']}\n\n---\n\n" | |
| st.download_button("Download Report", report, file_name="ai_workflow_report.txt") | |
| elif page == "Semantic Search": | |
| st.header("Semantic Search") | |
| code_input = st.text_area("Paste your code here", height=200, key="sem_code") | |
| uploaded_file = st.file_uploader("Or upload a code file", type=["py", "js", "ts", "java", "cpp", "cs"], key="sem_file") | |
| if uploaded_file: | |
| code_input = uploaded_file.read().decode("utf-8") | |
| st.text_area("File content", code_input, height=200, key="sem_file_content") | |
| col1, col2, col3, col4 = st.columns(4) | |
| with col1: | |
| programming_language = st.selectbox("Programming Language", PROGRAMMING_LANGUAGES, key="sem_lang") | |
| with col2: | |
| skill_level = st.selectbox("Skill Level", SKILL_LEVELS, key="sem_skill") | |
| with col3: | |
| user_role = st.selectbox("Your Role", USER_ROLES, key="sem_role") | |
| with col4: | |
| explanation_language = st.selectbox("Explanation Language", EXPLANATION_LANGUAGES, key="sem_expl") | |
| st.caption("Example questions:") | |
| st.write(", ".join(EXAMPLE_QUESTIONS)) | |
| # --- Voice input widget --- | |
| if "sem_question" not in st.session_state: | |
| st.session_state["sem_question"] = "" | |
| voice_input = components.html(''' | |
| <button id="voice-btn" style="margin-bottom:8px;">🎤 Speak your question</button> | |
| <span id="voice-status" style="margin-left:8px;"></span> | |
| <script> | |
| const btn = document.getElementById('voice-btn'); | |
| const status = document.getElementById('voice-status'); | |
| let recognition; | |
| if ('webkitSpeechRecognition' in window) { | |
| recognition = new webkitSpeechRecognition(); | |
| recognition.lang = 'en-US'; | |
| recognition.continuous = false; | |
| recognition.interimResults = false; | |
| btn.onclick = function() { | |
| recognition.start(); | |
| status.textContent = 'Listening...'; | |
| }; | |
| recognition.onresult = function(event) { | |
| const transcript = event.results[0][0].transcript; | |
| window.parent.postMessage({isStreamlitMessage: true, type: 'streamlit:setComponentValue', value: transcript}, '*'); | |
| status.textContent = 'Heard: ' + transcript; | |
| }; | |
| recognition.onerror = function() { | |
| status.textContent = 'Voice error'; | |
| }; | |
| recognition.onend = function() { | |
| if (status.textContent === 'Listening...') status.textContent = ''; | |
| }; | |
| } else { | |
| btn.disabled = true; | |
| status.textContent = 'Voice not supported'; | |
| } | |
| </script> | |
| ''', height=60) | |
| # If voice input is received, update the question field directly in session state | |
| if voice_input and isinstance(voice_input, str) and voice_input.strip(): | |
| if is_coding_question(voice_input): | |
| st.session_state["sem_question"] = voice_input | |
| st.success(f"Question recognized: {voice_input}") | |
| else: | |
| st.warning("Please ask a relevant question.") | |
| # Single input field for question (typed or spoken) | |
| question = st.text_input("Ask a question about your code", key="sem_question") | |
| # Run Semantic Search button | |
| if st.button("Run Semantic Search"): | |
| if not code_input.strip() or not question.strip(): | |
| st.error("Both code and question are required.") | |
| elif not code_matches_language(code_input, programming_language): | |
| st.error(f"Language mismatch. Please check your code and language selection.") | |
| else: | |
| with st.spinner("Running Semantic Search..."): | |
| answer = call_groq_api(f"{question}\n\nCode:\n{code_input}") | |
| st.success("Answer:") | |
| st.write(answer) | |
| elif page == "Code Comment Generator": | |
| st.header("Code Comment Generator") | |
| code_input = st.text_area("Paste your code here", height=200, key="comment_code") | |
| uploaded_file = st.file_uploader("Or upload a code file", type=["py", "js", "ts", "java", "cpp", "cs"], key="comment_file") | |
| if uploaded_file: | |
| code_input = uploaded_file.read().decode("utf-8") | |
| st.text_area("File content", code_input, height=200, key="comment_file_content") | |
| programming_language = st.selectbox("Programming Language", PROGRAMMING_LANGUAGES, key="comment_lang") | |
| if st.button("Generate Comments"): | |
| if not code_input.strip(): | |
| st.error("Please paste or upload your code.") | |
| else: | |
| with st.spinner("Generating commented code..."): | |
| prompt = ( | |
| f"Add clear, helpful comments to this {programming_language} code. " | |
| "Keep the code unchanged except for adding comments. " | |
| "Return the full code with comments:\n\n" | |
| f"{code_input}" | |
| ) | |
| commented_code = call_blackbox_agent([ | |
| {"role": "system", "content": "You are a helpful coding assistant."}, | |
| {"role": "user", "content": prompt} | |
| ]) | |
| st.success("Commented code generated!") | |
| st.code(commented_code, language=programming_language.lower()) | |
| st.download_button("Download Commented Code", commented_code, file_name="commented_code.txt") |