Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,35 +27,29 @@ def analyze_code(code):
|
|
| 27 |
if not code.strip():
|
| 28 |
return "No code provided.", "", ""
|
| 29 |
|
| 30 |
-
result = code_analyzer(code)
|
| 31 |
-
label = result[0]["label"].strip()
|
| 32 |
-
score = result[0]["score"]
|
| 33 |
-
|
| 34 |
-
# Map raw label to Salesforce-valid picklist value
|
| 35 |
-
issue_type = label_to_issue_type.get(label, "Error")
|
| 36 |
-
|
| 37 |
-
suggestion = {
|
| 38 |
-
"Performance": "Consider refactoring for better performance",
|
| 39 |
-
"Error": "Fix syntax or logic error",
|
| 40 |
-
"Security": "Avoid dynamic SOQL and validate inputs",
|
| 41 |
-
"Best Practice": "Follow Apex coding conventions"
|
| 42 |
-
}.get(issue_type, "Consider improving code quality")
|
| 43 |
-
|
| 44 |
-
severity = "Medium"
|
| 45 |
-
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
# ---------- Metadata Validator ----------
|
| 61 |
def validate_metadata(metadata):
|
|
@@ -87,16 +81,18 @@ def process_nlp_query(query):
|
|
| 87 |
return "No question provided."
|
| 88 |
|
| 89 |
prompt = (
|
| 90 |
-
"You are a certified Salesforce Apex expert. "
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
f"Question: {query.strip()}\n\nAnswer:"
|
| 94 |
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# ---------- Gradio UI ----------
|
| 102 |
with gr.Blocks() as demo:
|
|
|
|
| 27 |
if not code.strip():
|
| 28 |
return "No code provided.", "", ""
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
+
result = code_analyzer(code)
|
| 32 |
+
label = result[0]["label"].strip() # e.g., LABEL_0
|
| 33 |
+
issue_type = label_to_issue_type.get(label, "Error") # fallback to valid picklist
|
| 34 |
+
|
| 35 |
+
suggestion = "Consider refactoring for better performance"
|
| 36 |
+
severity = "Medium"
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
sf.CodeReviewResult__c.create({
|
| 40 |
+
"Name": f"Review_{issue_type}",
|
| 41 |
+
"CodeSnippet__c": code,
|
| 42 |
+
"IssueType__c": issue_type,
|
| 43 |
+
"Suggestion__c": suggestion,
|
| 44 |
+
"Severity__c": severity,
|
| 45 |
+
})
|
| 46 |
+
except Exception as sf_err:
|
| 47 |
+
suggestion += f" (⚠️ Failed to log to Salesforce: {str(sf_err)})"
|
| 48 |
+
|
| 49 |
+
return issue_type, suggestion, severity
|
| 50 |
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return "Error", f"⚠️ Model failed: {str(e)}", "High"
|
| 53 |
|
| 54 |
# ---------- Metadata Validator ----------
|
| 55 |
def validate_metadata(metadata):
|
|
|
|
| 81 |
return "No question provided."
|
| 82 |
|
| 83 |
prompt = (
|
| 84 |
+
"You are a certified Salesforce Apex expert. Answer the question below accurately and clearly. "
|
| 85 |
+
"Include governor limits, code examples, best practices, and terminology when appropriate. Do not repeat the question. "
|
| 86 |
+
"Give only a factual answer.\n\n"
|
| 87 |
f"Question: {query.strip()}\n\nAnswer:"
|
| 88 |
)
|
| 89 |
|
| 90 |
+
try:
|
| 91 |
+
result = qa_pipeline(prompt, max_length=512, temperature=0.3, top_p=0.9, repetition_penalty=1.1, do_sample=False)
|
| 92 |
+
output = result[0]["generated_text"]
|
| 93 |
+
return output.split("Answer:")[-1].strip() if "Answer:" in output else output.strip()
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"⚠️ AI failed to answer: {str(e)}"
|
| 96 |
|
| 97 |
# ---------- Gradio UI ----------
|
| 98 |
with gr.Blocks() as demo:
|