Bhanumani12 commited on
Commit
1a30944
·
verified ·
1 Parent(s): 45e4457

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -2,45 +2,49 @@ import gradio as gr
2
  import json
3
  from transformers import pipeline
4
 
5
- # Load AI models
6
  code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
7
  nlp_model = pipeline("text2text-generation", model="google/flan-t5-large")
8
 
9
- # Load FAQ fallback from JSON file
10
  try:
11
  with open("faq.json", "r") as f:
12
  faq_fallbacks = json.load(f)
13
  except FileNotFoundError:
14
  faq_fallbacks = {}
15
- print("⚠️ Warning: faq.json not found. Only AI model will be used.")
16
 
17
- # Code Review Function
 
 
18
  def analyze_code(code):
19
  if not code.strip():
20
  return "No code provided.", "", ""
21
  result = code_analyzer(code)
22
  return result[0]["label"], "Consider refactoring for better performance", "Medium"
23
 
24
- # Metadata Validator
 
 
25
  def validate_metadata(metadata):
26
  if not metadata.strip():
27
  return "No metadata provided.", "", ""
 
28
  return "Field", "Unused field detected", "Remove it to improve performance"
29
 
30
- # Ask AI (Natural Language)
 
 
31
  def process_nlp_query(query):
32
  if not query.strip():
33
  return "No query provided."
34
 
35
  normalized = query.lower().strip()
36
-
37
- # Check fallback JSON for exact match
38
  if normalized in faq_fallbacks:
39
  return faq_fallbacks[normalized]
40
 
41
- # Fallback to AI model if not in faq.json
42
  prompt = f"""
43
- You are a Salesforce and Apex expert. Provide a clear and accurate answer to the following question:\n\n{query}\n\nAnswer:
44
  """
45
  result = nlp_model(
46
  prompt,
@@ -53,23 +57,24 @@ def process_nlp_query(query):
53
  )
54
 
55
  output = result[0]["generated_text"]
56
-
57
- # Clean output
58
  if "Answer:" in output:
59
  output = output.split("Answer:")[-1]
60
 
 
61
  lines = output.strip().splitlines()
62
  seen = set()
63
  unique_lines = [line.strip() for line in lines if line.strip() not in seen and not seen.add(line.strip())]
64
 
65
  return "\n".join(unique_lines).strip()
66
 
 
67
  # Gradio UI
 
68
  with gr.Blocks() as demo:
69
- gr.Markdown("# 🤖 AI Code Review & Metadata Validator")
70
 
71
  with gr.Tab("Code Review"):
72
- code_input = gr.Textbox(label="Apex / LWC Code", lines=8)
73
  issue_type = gr.Textbox(label="Issue Type")
74
  suggestion = gr.Textbox(label="AI Suggestion")
75
  severity = gr.Textbox(label="Severity")
@@ -77,7 +82,7 @@ with gr.Blocks() as demo:
77
  code_button.click(analyze_code, inputs=code_input, outputs=[issue_type, suggestion, severity])
78
 
79
  with gr.Tab("Metadata Validation"):
80
- metadata_input = gr.Textbox(label="Metadata XML", lines=8)
81
  mtype = gr.Textbox(label="Type")
82
  issue = gr.Textbox(label="Issue")
83
  recommendation = gr.Textbox(label="Recommendation")
 
2
  import json
3
  from transformers import pipeline
4
 
5
+ # Load Hugging Face models
6
  code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
7
  nlp_model = pipeline("text2text-generation", model="google/flan-t5-large")
8
 
9
+ # Load FAQ fallback (if available)
10
  try:
11
  with open("faq.json", "r") as f:
12
  faq_fallbacks = json.load(f)
13
  except FileNotFoundError:
14
  faq_fallbacks = {}
15
+ print("⚠️ faq.json not found. AI will be used for all queries.")
16
 
17
+ # --------------------------
18
+ # Code Review Endpoint Logic
19
+ # --------------------------
20
  def analyze_code(code):
21
  if not code.strip():
22
  return "No code provided.", "", ""
23
  result = code_analyzer(code)
24
  return result[0]["label"], "Consider refactoring for better performance", "Medium"
25
 
26
+ # --------------------------
27
+ # Metadata Validation Logic
28
+ # --------------------------
29
  def validate_metadata(metadata):
30
  if not metadata.strip():
31
  return "No metadata provided.", "", ""
32
+ # Example: Simulate unused field
33
  return "Field", "Unused field detected", "Remove it to improve performance"
34
 
35
+ # --------------------------
36
+ # Ask AI with Fallback Logic
37
+ # --------------------------
38
  def process_nlp_query(query):
39
  if not query.strip():
40
  return "No query provided."
41
 
42
  normalized = query.lower().strip()
 
 
43
  if normalized in faq_fallbacks:
44
  return faq_fallbacks[normalized]
45
 
 
46
  prompt = f"""
47
+ You are a Salesforce and Apex expert. Provide a clear, accurate answer to the following question:\n\n{query}\n\nAnswer:
48
  """
49
  result = nlp_model(
50
  prompt,
 
57
  )
58
 
59
  output = result[0]["generated_text"]
 
 
60
  if "Answer:" in output:
61
  output = output.split("Answer:")[-1]
62
 
63
+ # Remove duplicate lines
64
  lines = output.strip().splitlines()
65
  seen = set()
66
  unique_lines = [line.strip() for line in lines if line.strip() not in seen and not seen.add(line.strip())]
67
 
68
  return "\n".join(unique_lines).strip()
69
 
70
+ # --------------------------
71
  # Gradio UI
72
+ # --------------------------
73
  with gr.Blocks() as demo:
74
+ gr.Markdown("# 🤖 Salesforce AI Code Review & Metadata Assistant")
75
 
76
  with gr.Tab("Code Review"):
77
+ code_input = gr.Textbox(label="Apex / LWC Code", lines=8, placeholder="Paste Apex or LWC code here")
78
  issue_type = gr.Textbox(label="Issue Type")
79
  suggestion = gr.Textbox(label="AI Suggestion")
80
  severity = gr.Textbox(label="Severity")
 
82
  code_button.click(analyze_code, inputs=code_input, outputs=[issue_type, suggestion, severity])
83
 
84
  with gr.Tab("Metadata Validation"):
85
+ metadata_input = gr.Textbox(label="Metadata XML", lines=8, placeholder="Paste Salesforce metadata here")
86
  mtype = gr.Textbox(label="Type")
87
  issue = gr.Textbox(label="Issue")
88
  recommendation = gr.Textbox(label="Recommendation")