Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,6 +26,7 @@ suggestions = {
|
|
| 26 |
"Security": "Avoid dynamic SOQL. Use binding variables.",
|
| 27 |
"Best Practice": "Refactor for readability and use bulk-safe patterns."
|
| 28 |
}
|
|
|
|
| 29 |
severities = {
|
| 30 |
"Performance": "Medium",
|
| 31 |
"Error": "High",
|
|
@@ -33,7 +34,7 @@ severities = {
|
|
| 33 |
"Best Practice": "Low"
|
| 34 |
}
|
| 35 |
|
| 36 |
-
# ---------- Load
|
| 37 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 38 |
|
| 39 |
# ---------- Code Analyzer ----------
|
|
@@ -82,22 +83,41 @@ def validate_metadata(metadata):
|
|
| 82 |
|
| 83 |
return mtype, issue, recommendation
|
| 84 |
|
| 85 |
-
# ---------- Natural Language Assistant ----------
|
| 86 |
def process_nlp_query(query):
|
| 87 |
if not query.strip():
|
| 88 |
return "No question provided."
|
| 89 |
|
| 90 |
-
prompt
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
try:
|
| 97 |
result = qa_pipeline(prompt, max_new_tokens=256, do_sample=False)
|
| 98 |
output = result[0]["generated_text"]
|
| 99 |
-
if "Answer:" in output:
|
| 100 |
-
return output.split("Answer:")[-1].strip()
|
| 101 |
return output.strip()
|
| 102 |
except Exception as e:
|
| 103 |
return f"⚠️ AI Response Error: {str(e)}"
|
|
@@ -130,4 +150,4 @@ with gr.Blocks() as demo:
|
|
| 130 |
|
| 131 |
# ---------- Start UI ----------
|
| 132 |
if __name__ == "__main__":
|
| 133 |
-
demo.launch()
|
|
|
|
| 26 |
"Security": "Avoid dynamic SOQL. Use binding variables.",
|
| 27 |
"Best Practice": "Refactor for readability and use bulk-safe patterns."
|
| 28 |
}
|
| 29 |
+
|
| 30 |
severities = {
|
| 31 |
"Performance": "Medium",
|
| 32 |
"Error": "High",
|
|
|
|
| 34 |
"Best Practice": "Low"
|
| 35 |
}
|
| 36 |
|
| 37 |
+
# ---------- Load QnA Model ----------
|
| 38 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 39 |
|
| 40 |
# ---------- Code Analyzer ----------
|
|
|
|
| 83 |
|
| 84 |
return mtype, issue, recommendation
|
| 85 |
|
| 86 |
+
# ---------- Natural Language Assistant (Improved Prompt) ----------
|
| 87 |
def process_nlp_query(query):
|
| 88 |
if not query.strip():
|
| 89 |
return "No question provided."
|
| 90 |
|
| 91 |
+
# Few-shot examples included in the prompt to simulate training
|
| 92 |
+
prompt = f"""
|
| 93 |
+
You are an expert Salesforce Apex developer and trainer. Your job is to answer questions accurately, concisely, and based strictly on Salesforce best practices and official documentation. Always include real values like governor limits, correct terminology, and coding insights.
|
| 94 |
+
|
| 95 |
+
Examples:
|
| 96 |
+
|
| 97 |
+
Q: How many DML operations are allowed in Apex?
|
| 98 |
+
A: You can perform up to 150 DML statements per Apex transaction.
|
| 99 |
+
|
| 100 |
+
Q: How many SOQL queries are allowed in Apex?
|
| 101 |
+
A: You can perform up to 100 SOQL queries per Apex transaction.
|
| 102 |
+
|
| 103 |
+
Q: What is a trigger in Salesforce?
|
| 104 |
+
A: A trigger is a piece of Apex code that executes before or after records are inserted, updated, deleted, or undeleted. Triggers help automate tasks and enforce business rules.
|
| 105 |
+
|
| 106 |
+
Q: What are governor limits in Apex?
|
| 107 |
+
A: Governor limits are runtime limits enforced by Salesforce to ensure efficient multi-tenancy. They include limits like 100 SOQL queries, 150 DML statements, and 10,000 records returned in queries per transaction.
|
| 108 |
+
|
| 109 |
+
Q: What is bulkification in Apex?
|
| 110 |
+
A: Bulkification is the practice of writing code that can handle multiple records at once, such as using loops and collections with DML to avoid hitting governor limits.
|
| 111 |
+
|
| 112 |
+
Now answer this new question as an expert would:
|
| 113 |
+
|
| 114 |
+
Q: {query.strip()}
|
| 115 |
+
A:
|
| 116 |
+
"""
|
| 117 |
|
| 118 |
try:
|
| 119 |
result = qa_pipeline(prompt, max_new_tokens=256, do_sample=False)
|
| 120 |
output = result[0]["generated_text"]
|
|
|
|
|
|
|
| 121 |
return output.strip()
|
| 122 |
except Exception as e:
|
| 123 |
return f"⚠️ AI Response Error: {str(e)}"
|
|
|
|
| 150 |
|
| 151 |
# ---------- Start UI ----------
|
| 152 |
if __name__ == "__main__":
|
| 153 |
+
demo.launch()
|