Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,30 +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"] # e.g., LABEL_0
|
| 32 |
-
score = result[0]["score"]
|
| 33 |
-
|
| 34 |
-
# Map raw label to issue type (no lower(), no mismatch)
|
| 35 |
-
issue_type = label_to_issue_type.get(label.strip(), "Error")
|
| 36 |
-
|
| 37 |
-
# Static suggestion and severity for now
|
| 38 |
-
suggestion = "Consider refactoring for better performance"
|
| 39 |
-
severity = "Medium"
|
| 40 |
-
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
# ---------- Metadata Validator ----------
|
| 56 |
def validate_metadata(metadata):
|
|
@@ -88,12 +87,12 @@ def process_nlp_query(query):
|
|
| 88 |
f"Question: {query.strip()}\n\nAnswer:"
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
# ---------- Gradio UI ----------
|
| 99 |
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):
|
|
|
|
| 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:
|