Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from simple_salesforce import Salesforce
|
| 4 |
-
import os
|
| 5 |
-
import re
|
| 6 |
|
| 7 |
# ---------- Salesforce Login ----------
|
| 8 |
sf = Salesforce(
|
|
@@ -12,7 +12,7 @@ sf = Salesforce(
|
|
| 12 |
domain="login"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# ----------
|
| 16 |
label_to_issue_type = {
|
| 17 |
"LABEL_0": "Performance",
|
| 18 |
"LABEL_1": "Error",
|
|
@@ -20,25 +20,32 @@ label_to_issue_type = {
|
|
| 20 |
"LABEL_3": "Best Practice"
|
| 21 |
}
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# ----------
|
| 27 |
-
|
| 28 |
-
dml_keywords = ["insert", "update", "delete", "upsert", "merge"]
|
| 29 |
-
return sum(len(re.findall(rf"\\b{kw}\\b", code, flags=re.IGNORECASE)) for kw in dml_keywords)
|
| 30 |
|
| 31 |
-
# ---------- Code Analyzer
|
| 32 |
def analyze_code(code):
|
| 33 |
if not code.strip():
|
| 34 |
return "No code provided.", "", ""
|
| 35 |
|
| 36 |
-
|
| 37 |
-
label =
|
| 38 |
-
issue_type = label_to_issue_type
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
severity = "Medium"
|
| 42 |
|
| 43 |
try:
|
| 44 |
sf.CodeReviewResult__c.create({
|
|
@@ -46,11 +53,10 @@ def analyze_code(code):
|
|
| 46 |
"CodeSnippet__c": code,
|
| 47 |
"IssueType__c": issue_type,
|
| 48 |
"Suggestion__c": suggestion,
|
| 49 |
-
"Severity__c": severity
|
| 50 |
-
# Add Developer__c if available
|
| 51 |
})
|
| 52 |
except Exception as e:
|
| 53 |
-
suggestion += f"
|
| 54 |
|
| 55 |
return issue_type, suggestion, severity
|
| 56 |
|
|
@@ -72,35 +78,29 @@ def validate_metadata(metadata):
|
|
| 72 |
"Status__c": "Open"
|
| 73 |
})
|
| 74 |
except Exception as e:
|
| 75 |
-
recommendation += f"
|
| 76 |
|
| 77 |
return mtype, issue, recommendation
|
| 78 |
|
| 79 |
-
# ----------
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
def process_nlp_query(query, code_snippet=""):
|
| 83 |
if not query.strip():
|
| 84 |
return "No question provided."
|
| 85 |
|
| 86 |
-
# Handle DML-specific queries manually
|
| 87 |
-
if "dml" in query.lower() and code_snippet:
|
| 88 |
-
dml_count = count_dml_statements(code_snippet)
|
| 89 |
-
return f"{dml_count} DML statement(s) detected in the provided code."
|
| 90 |
-
|
| 91 |
prompt = (
|
| 92 |
-
"You are a
|
| 93 |
-
"
|
| 94 |
-
"Give only a factual answer.\n\n"
|
| 95 |
f"Question: {query.strip()}\n\nAnswer:"
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# ---------- Gradio UI ----------
|
| 106 |
with gr.Blocks() as demo:
|
|
@@ -124,9 +124,10 @@ with gr.Blocks() as demo:
|
|
| 124 |
|
| 125 |
with gr.Tab("Ask AI (Natural Language)"):
|
| 126 |
query_input = gr.Textbox(label="Your question", lines=2, placeholder="e.g. What is a trigger?")
|
| 127 |
-
code_context = gr.Textbox(label="(Optional) Provide code for context", lines=4)
|
| 128 |
response_output = gr.Textbox(label="AI Response", lines=8)
|
| 129 |
nlp_button = gr.Button("Ask")
|
| 130 |
-
nlp_button.click(process_nlp_query, inputs=
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
from simple_salesforce import Salesforce
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ---------- Salesforce Login ----------
|
| 8 |
sf = Salesforce(
|
|
|
|
| 12 |
domain="login"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# ---------- Label Mapping ----------
|
| 16 |
label_to_issue_type = {
|
| 17 |
"LABEL_0": "Performance",
|
| 18 |
"LABEL_1": "Error",
|
|
|
|
| 20 |
"LABEL_3": "Best Practice"
|
| 21 |
}
|
| 22 |
|
| 23 |
+
suggestions = {
|
| 24 |
+
"Performance": "Consider optimizing loops and database access.",
|
| 25 |
+
"Error": "Add proper error handling and null checks.",
|
| 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",
|
| 32 |
+
"Security": "High",
|
| 33 |
+
"Best Practice": "Low"
|
| 34 |
+
}
|
| 35 |
|
| 36 |
+
# ---------- Load Dummy Classifier & QnA Model ----------
|
| 37 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# ---------- Code Analyzer ----------
|
| 40 |
def analyze_code(code):
|
| 41 |
if not code.strip():
|
| 42 |
return "No code provided.", "", ""
|
| 43 |
|
| 44 |
+
# Simulate classification
|
| 45 |
+
label = random.choice(list(label_to_issue_type.keys()))
|
| 46 |
+
issue_type = label_to_issue_type[label]
|
| 47 |
+
suggestion = suggestions[issue_type]
|
| 48 |
+
severity = severities[issue_type]
|
|
|
|
| 49 |
|
| 50 |
try:
|
| 51 |
sf.CodeReviewResult__c.create({
|
|
|
|
| 53 |
"CodeSnippet__c": code,
|
| 54 |
"IssueType__c": issue_type,
|
| 55 |
"Suggestion__c": suggestion,
|
| 56 |
+
"Severity__c": severity
|
|
|
|
| 57 |
})
|
| 58 |
except Exception as e:
|
| 59 |
+
suggestion += f" ⚠️ Salesforce logging failed: {str(e)}"
|
| 60 |
|
| 61 |
return issue_type, suggestion, severity
|
| 62 |
|
|
|
|
| 78 |
"Status__c": "Open"
|
| 79 |
})
|
| 80 |
except Exception as e:
|
| 81 |
+
recommendation += f" ⚠️ Salesforce logging failed: {str(e)}"
|
| 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 |
+
"You are a Salesforce Apex expert. "
|
| 92 |
+
"Answer the question clearly and include governor limits, code best practices, and real examples.\n\n"
|
|
|
|
| 93 |
f"Question: {query.strip()}\n\nAnswer:"
|
| 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)}"
|
| 104 |
|
| 105 |
# ---------- Gradio UI ----------
|
| 106 |
with gr.Blocks() as demo:
|
|
|
|
| 124 |
|
| 125 |
with gr.Tab("Ask AI (Natural Language)"):
|
| 126 |
query_input = gr.Textbox(label="Your question", lines=2, placeholder="e.g. What is a trigger?")
|
|
|
|
| 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)
|
| 130 |
|
| 131 |
+
# ---------- Start UI ----------
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
demo.launch()
|