Bhanumani12 commited on
Commit
ce6e170
·
verified ·
1 Parent(s): 2589dca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -21
app.py CHANGED
@@ -1,21 +1,12 @@
1
  import gradio as gr
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():
@@ -24,27 +15,22 @@ def analyze_code(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,10 +43,11 @@ def process_nlp_query(query):
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())]
@@ -74,7 +61,7 @@ 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,7 +69,7 @@ with gr.Blocks() as demo:
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")
 
1
  import gradio as gr
 
2
  from transformers import pipeline
3
 
4
  # Load Hugging Face models
5
  code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
6
  nlp_model = pipeline("text2text-generation", model="google/flan-t5-large")
7
 
 
 
 
 
 
 
 
 
8
  # --------------------------
9
+ # Code Review Function
10
  # --------------------------
11
  def analyze_code(code):
12
  if not code.strip():
 
15
  return result[0]["label"], "Consider refactoring for better performance", "Medium"
16
 
17
  # --------------------------
18
+ # Metadata Validator (Mock)
19
  # --------------------------
20
  def validate_metadata(metadata):
21
  if not metadata.strip():
22
  return "No metadata provided.", "", ""
 
23
  return "Field", "Unused field detected", "Remove it to improve performance"
24
 
25
  # --------------------------
26
+ # AI Natural Language Q&A (No Fallback)
27
  # --------------------------
28
  def process_nlp_query(query):
29
  if not query.strip():
30
  return "No query provided."
31
 
 
 
 
 
32
  prompt = f"""
33
+ You are a Salesforce and Apex expert. Provide a clear and technically accurate answer to the following question:\n\n{query}\n\nAnswer:
34
  """
35
  result = nlp_model(
36
  prompt,
 
43
  )
44
 
45
  output = result[0]["generated_text"]
46
+
47
  if "Answer:" in output:
48
  output = output.split("Answer:")[-1]
49
 
50
+ # Clean response (remove duplicates, spacing)
51
  lines = output.strip().splitlines()
52
  seen = set()
53
  unique_lines = [line.strip() for line in lines if line.strip() not in seen and not seen.add(line.strip())]
 
61
  gr.Markdown("# 🤖 Salesforce AI Code Review & Metadata Assistant")
62
 
63
  with gr.Tab("Code Review"):
64
+ code_input = gr.Textbox(label="Apex / LWC Code", lines=8)
65
  issue_type = gr.Textbox(label="Issue Type")
66
  suggestion = gr.Textbox(label="AI Suggestion")
67
  severity = gr.Textbox(label="Severity")
 
69
  code_button.click(analyze_code, inputs=code_input, outputs=[issue_type, suggestion, severity])
70
 
71
  with gr.Tab("Metadata Validation"):
72
+ metadata_input = gr.Textbox(label="Metadata XML", lines=8)
73
  mtype = gr.Textbox(label="Type")
74
  issue = gr.Textbox(label="Issue")
75
  recommendation = gr.Textbox(label="Recommendation")