Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,19 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load models
|
| 5 |
code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
|
| 6 |
nlp_model = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Code Review Function
|
| 9 |
def analyze_code(code):
|
| 10 |
if not code.strip():
|
|
@@ -12,7 +21,7 @@ def analyze_code(code):
|
|
| 12 |
result = code_analyzer(code)
|
| 13 |
return result[0]["label"], "Consider refactoring for better performance", "Medium"
|
| 14 |
|
| 15 |
-
# Metadata Validator
|
| 16 |
def validate_metadata(metadata):
|
| 17 |
if not metadata.strip():
|
| 18 |
return "No metadata provided.", "", ""
|
|
@@ -23,15 +32,16 @@ def process_nlp_query(query):
|
|
| 23 |
if not query.strip():
|
| 24 |
return "No query provided."
|
| 25 |
|
| 26 |
-
|
| 27 |
-
prompt = f"""
|
| 28 |
-
You are a Salesforce and Apex expert. Provide a clear and technically accurate answer to the following question:
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
result = nlp_model(
|
| 36 |
prompt,
|
| 37 |
max_length=256,
|
|
@@ -44,18 +54,13 @@ def process_nlp_query(query):
|
|
| 44 |
|
| 45 |
output = result[0]["generated_text"]
|
| 46 |
|
| 47 |
-
#
|
| 48 |
if "Answer:" in output:
|
| 49 |
output = output.split("Answer:")[-1]
|
| 50 |
|
| 51 |
-
# Remove duplicate lines
|
| 52 |
lines = output.strip().splitlines()
|
| 53 |
-
unique_lines = []
|
| 54 |
seen = set()
|
| 55 |
-
for line in lines
|
| 56 |
-
if line.strip() not in seen:
|
| 57 |
-
seen.add(line.strip())
|
| 58 |
-
unique_lines.append(line.strip())
|
| 59 |
|
| 60 |
return "\n".join(unique_lines).strip()
|
| 61 |
|
|
|
|
| 1 |
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():
|
|
|
|
| 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.", "", ""
|
|
|
|
| 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,
|
| 47 |
max_length=256,
|
|
|
|
| 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 |
|