Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ sf = Salesforce(
|
|
| 11 |
domain="login"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# ---------- Load Pipelines ----------
|
| 15 |
code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
|
| 16 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-xl")
|
| 17 |
|
|
@@ -23,7 +23,6 @@ def analyze_code(code):
|
|
| 23 |
result = code_analyzer(code)
|
| 24 |
label = result[0]["label"].strip()
|
| 25 |
|
| 26 |
-
# Strict label-to-picklist mapping
|
| 27 |
label_to_issue_type = {
|
| 28 |
"LABEL_0": "Performance",
|
| 29 |
"LABEL_1": "Error",
|
|
@@ -38,6 +37,9 @@ def analyze_code(code):
|
|
| 38 |
suggestion = "Consider refactoring for better performance"
|
| 39 |
severity = "Medium"
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
sf.CodeReviewResult__c.create({
|
| 43 |
"Name": f"Review_{issue_type}",
|
|
@@ -45,14 +47,14 @@ def analyze_code(code):
|
|
| 45 |
"IssueType__c": issue_type,
|
| 46 |
"Suggestion__c": suggestion,
|
| 47 |
"Severity__c": severity,
|
| 48 |
-
"Developer__c":
|
| 49 |
})
|
| 50 |
except Exception as e:
|
| 51 |
suggestion += f" (⚠️ Failed to log to Salesforce: {str(e)})"
|
| 52 |
|
| 53 |
return issue_type, suggestion, severity
|
| 54 |
|
| 55 |
-
# ---------- Metadata
|
| 56 |
def validate_metadata(metadata):
|
| 57 |
mtype = "Field"
|
| 58 |
issue = "Unused field detected"
|
|
@@ -71,7 +73,7 @@ def validate_metadata(metadata):
|
|
| 71 |
|
| 72 |
return mtype, issue, recommendation
|
| 73 |
|
| 74 |
-
# ----------
|
| 75 |
def process_nlp_query(query):
|
| 76 |
try:
|
| 77 |
query = query.strip()
|
|
@@ -79,9 +81,9 @@ def process_nlp_query(query):
|
|
| 79 |
return "Please enter a valid Salesforce-related question."
|
| 80 |
|
| 81 |
prompt = (
|
| 82 |
-
"You are a Salesforce-certified Apex
|
| 83 |
-
"Include
|
| 84 |
-
"
|
| 85 |
f"Question: {query}\n\nAnswer:"
|
| 86 |
)
|
| 87 |
|
|
@@ -123,7 +125,7 @@ with gr.Blocks() as demo:
|
|
| 123 |
metadata_button.click(validate_metadata, inputs=metadata_input, outputs=[mtype, issue, recommendation])
|
| 124 |
|
| 125 |
with gr.Tab("Ask AI (Natural Language)"):
|
| 126 |
-
query_input = gr.Textbox(label="Your question", lines=2, placeholder="e.g.
|
| 127 |
response_output = gr.Textbox(label="AI Response", lines=8)
|
| 128 |
nlp_button = gr.Button("Ask")
|
| 129 |
nlp_button.click(process_nlp_query, inputs=query_input, outputs=response_output)
|
|
|
|
| 11 |
domain="login"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# ---------- Load HuggingFace Pipelines ----------
|
| 15 |
code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
|
| 16 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-xl")
|
| 17 |
|
|
|
|
| 23 |
result = code_analyzer(code)
|
| 24 |
label = result[0]["label"].strip()
|
| 25 |
|
|
|
|
| 26 |
label_to_issue_type = {
|
| 27 |
"LABEL_0": "Performance",
|
| 28 |
"LABEL_1": "Error",
|
|
|
|
| 37 |
suggestion = "Consider refactoring for better performance"
|
| 38 |
severity = "Medium"
|
| 39 |
|
| 40 |
+
# ✅ FIX: Replace Apex-style UserInfo with hardcoded or environment-driven value
|
| 41 |
+
developer_id = os.getenv("DEVELOPER_ID", "005XXXXXXXXXXXXXXX") # <-- Replace with actual User Id
|
| 42 |
+
|
| 43 |
try:
|
| 44 |
sf.CodeReviewResult__c.create({
|
| 45 |
"Name": f"Review_{issue_type}",
|
|
|
|
| 47 |
"IssueType__c": issue_type,
|
| 48 |
"Suggestion__c": suggestion,
|
| 49 |
"Severity__c": severity,
|
| 50 |
+
"Developer__c": developer_id
|
| 51 |
})
|
| 52 |
except Exception as e:
|
| 53 |
suggestion += f" (⚠️ Failed to log to Salesforce: {str(e)})"
|
| 54 |
|
| 55 |
return issue_type, suggestion, severity
|
| 56 |
|
| 57 |
+
# ---------- Metadata Validator ----------
|
| 58 |
def validate_metadata(metadata):
|
| 59 |
mtype = "Field"
|
| 60 |
issue = "Unused field detected"
|
|
|
|
| 73 |
|
| 74 |
return mtype, issue, recommendation
|
| 75 |
|
| 76 |
+
# ---------- Natural Language AI Assistant ----------
|
| 77 |
def process_nlp_query(query):
|
| 78 |
try:
|
| 79 |
query = query.strip()
|
|
|
|
| 81 |
return "Please enter a valid Salesforce-related question."
|
| 82 |
|
| 83 |
prompt = (
|
| 84 |
+
"You are a Salesforce-certified Apex developer. Answer the question clearly and completely. "
|
| 85 |
+
"Include governor limits, DML operations, SOQL queries, CPU limits, heap size, best practices, and real examples. "
|
| 86 |
+
"Be specific and educational.\n\n"
|
| 87 |
f"Question: {query}\n\nAnswer:"
|
| 88 |
)
|
| 89 |
|
|
|
|
| 125 |
metadata_button.click(validate_metadata, inputs=metadata_input, outputs=[mtype, issue, recommendation])
|
| 126 |
|
| 127 |
with gr.Tab("Ask AI (Natural Language)"):
|
| 128 |
+
query_input = gr.Textbox(label="Your question", lines=2, placeholder="e.g. What is a governor limit in Apex?")
|
| 129 |
response_output = gr.Textbox(label="AI Response", lines=8)
|
| 130 |
nlp_button = gr.Button("Ask")
|
| 131 |
nlp_button.click(process_nlp_query, inputs=query_input, outputs=response_output)
|