Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,24 +28,29 @@ def analyze_code(code):
|
|
| 28 |
return "No code provided.", "", ""
|
| 29 |
|
| 30 |
result = code_analyzer(code)
|
| 31 |
-
label = result[0]["label"]
|
| 32 |
score = result[0]["score"]
|
| 33 |
|
| 34 |
-
# Map raw label to
|
| 35 |
-
issue_type = label_to_issue_type.get(label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Static suggestion and severity for now
|
| 38 |
-
suggestion = "Consider refactoring for better performance"
|
| 39 |
severity = "Medium"
|
| 40 |
|
| 41 |
try:
|
| 42 |
sf.CodeReviewResult__c.create({
|
| 43 |
"Name": f"Review_{issue_type}",
|
| 44 |
"CodeSnippet__c": code,
|
| 45 |
-
"IssueType__c": issue_type,
|
| 46 |
"Suggestion__c": suggestion,
|
| 47 |
"Severity__c": severity,
|
| 48 |
-
#
|
| 49 |
})
|
| 50 |
except Exception as e:
|
| 51 |
suggestion += f" (⚠️ Failed to log to Salesforce: {str(e)})"
|
|
@@ -82,18 +87,16 @@ def process_nlp_query(query):
|
|
| 82 |
return "No question provided."
|
| 83 |
|
| 84 |
prompt = (
|
| 85 |
-
"You are a certified Salesforce Apex expert.
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
f"Question: {query.strip()}\n\nAnswer:"
|
| 89 |
)
|
| 90 |
|
| 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 |
|
| 94 |
-
if "Answer:" in output
|
| 95 |
-
return output.split("Answer:")[-1].strip()
|
| 96 |
-
return output.strip()
|
| 97 |
|
| 98 |
# ---------- Gradio UI ----------
|
| 99 |
with gr.Blocks() as demo:
|
|
|
|
| 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 |
sf.CodeReviewResult__c.create({
|
| 48 |
"Name": f"Review_{issue_type}",
|
| 49 |
"CodeSnippet__c": code,
|
| 50 |
+
"IssueType__c": issue_type,
|
| 51 |
"Suggestion__c": suggestion,
|
| 52 |
"Severity__c": severity,
|
| 53 |
+
# Add Developer__c if needed
|
| 54 |
})
|
| 55 |
except Exception as e:
|
| 56 |
suggestion += f" (⚠️ Failed to log to Salesforce: {str(e)})"
|
|
|
|
| 87 |
return "No question provided."
|
| 88 |
|
| 89 |
prompt = (
|
| 90 |
+
"You are a certified Salesforce Apex expert. "
|
| 91 |
+
"Answer accurately using Apex best practices, governor limits, SOQL/DML limits, and examples. "
|
| 92 |
+
"Avoid repeating the question. Be concise, correct, and helpful.\n\n"
|
| 93 |
f"Question: {query.strip()}\n\nAnswer:"
|
| 94 |
)
|
| 95 |
|
| 96 |
result = qa_pipeline(prompt, max_length=512, temperature=0.3, top_p=0.9, repetition_penalty=1.1, do_sample=False)
|
| 97 |
output = result[0]["generated_text"]
|
| 98 |
|
| 99 |
+
return output.split("Answer:")[-1].strip() if "Answer:" in output else output.strip()
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# ---------- Gradio UI ----------
|
| 102 |
with gr.Blocks() as demo:
|