Naz786 commited on
Commit
1ba6c2b
·
verified ·
1 Parent(s): 1332463

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -22,6 +22,15 @@ EXAMPLE_QUESTIONS = [
22
  "How can I make this code more readable?"
23
  ]
24
 
 
 
 
 
 
 
 
 
 
25
  # --- API STUBS ---
26
  def call_groq_api(prompt, model="llama3-70b-8192"):
27
  # Replace with actual Groq API call
@@ -51,10 +60,8 @@ def call_blackbox_agent(messages):
51
 
52
  # --- UTILS ---
53
  def code_matches_language(code, language):
54
- # Simple heuristic, can be improved
55
- if language.lower() in code.lower():
56
- return True
57
- return True # For demo, always True
58
 
59
  def calculate_code_complexity(code):
60
  # Dummy complexity metric
@@ -86,6 +93,16 @@ def is_coding_question(question):
86
  except Exception:
87
  return False
88
 
 
 
 
 
 
 
 
 
 
 
89
  # --- STREAMLIT APP ---
90
  st.set_page_config(page_title="Code Workflows", layout="wide")
91
  st.title("Code Genie")
@@ -127,9 +144,8 @@ elif page == "Code Workflows":
127
  st.error(f"Language mismatch. Please check your code and language selection.")
128
  else:
129
  with st.spinner("Running AI Workflow..."):
130
- # Simulate workflow steps
131
  steps = [
132
- ("Explain", call_groq_api(f"Explain this {programming_language} code for a {skill_level} {user_role} in {explanation_language}:\n{code_input}")),
133
  ("Refactor", call_blackbox_agent([
134
  {"role": "system", "content": "You are a helpful coding assistant."},
135
  {"role": "user", "content": f"Refactor this {programming_language} code: {code_input}"}
@@ -186,7 +202,8 @@ elif page == "Semantic Search":
186
  st.error(f"Language mismatch. Please check your code and language selection.")
187
  else:
188
  with st.spinner("Running Semantic Search..."):
189
- answer = call_groq_api(f"{question}\n\nCode:\n{code_input}")
 
190
  st.success("Answer:")
191
  st.write(answer)
192
 
@@ -201,6 +218,8 @@ elif page == "Code Comment Generator":
201
  if st.button("Generate Comments"):
202
  if not code_input.strip():
203
  st.error("Please paste or upload your code.")
 
 
204
  else:
205
  with st.spinner("Generating commented code..."):
206
  prompt = (
 
22
  "How can I make this code more readable?"
23
  ]
24
 
25
+ LANGUAGE_KEYWORDS = {
26
+ "Python": ["def ", "import ", "self", "print(", "lambda", "None"],
27
+ "JavaScript": ["function ", "console.log", "var ", "let ", "const ", "=>"],
28
+ "TypeScript": ["interface ", "type ", ": string", ": number", "export ", "import "],
29
+ "Java": ["public class", "System.out.println", "void main", "import java.", "new "],
30
+ "C++": ["#include", "std::", "cout <<", "cin >>", "int main(", "using namespace"],
31
+ "C#": ["using System;", "namespace ", "public class", "Console.WriteLine", "static void Main"]
32
+ }
33
+
34
  # --- API STUBS ---
35
  def call_groq_api(prompt, model="llama3-70b-8192"):
36
  # Replace with actual Groq API call
 
60
 
61
  # --- UTILS ---
62
  def code_matches_language(code, language):
63
+ keywords = LANGUAGE_KEYWORDS.get(language, [])
64
+ return any(kw in code for kw in keywords)
 
 
65
 
66
  def calculate_code_complexity(code):
67
  # Dummy complexity metric
 
93
  except Exception:
94
  return False
95
 
96
+ def get_explanation_prompt(code, programming_language, skill_level, user_role, explanation_language, question=None):
97
+ lang_instruction = ""
98
+ if explanation_language != "English":
99
+ lang_instruction = f" Respond in {explanation_language}."
100
+ if question:
101
+ return f"{question}\n\nCode:\n{code}\n{lang_instruction}"
102
+ return (
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")
 
144
  st.error(f"Language mismatch. Please check your code and language selection.")
145
  else:
146
  with st.spinner("Running AI Workflow..."):
 
147
  steps = [
148
+ ("Explain", call_groq_api(get_explanation_prompt(code_input, programming_language, skill_level, user_role, explanation_language))),
149
  ("Refactor", call_blackbox_agent([
150
  {"role": "system", "content": "You are a helpful coding assistant."},
151
  {"role": "user", "content": f"Refactor this {programming_language} code: {code_input}"}
 
202
  st.error(f"Language mismatch. Please check your code and language selection.")
203
  else:
204
  with st.spinner("Running Semantic Search..."):
205
+ prompt = get_explanation_prompt(code_input, programming_language, skill_level, user_role, explanation_language, question=question)
206
+ answer = call_groq_api(prompt)
207
  st.success("Answer:")
208
  st.write(answer)
209
 
 
218
  if st.button("Generate Comments"):
219
  if not code_input.strip():
220
  st.error("Please paste or upload your code.")
221
+ elif not code_matches_language(code_input, programming_language):
222
+ st.error(f"Language mismatch. Please check your code and language selection.")
223
  else:
224
  with st.spinner("Generating commented code..."):
225
  prompt = (