Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,44 +18,48 @@ def validate_metadata(metadata):
|
|
| 18 |
return "No metadata provided.", "", ""
|
| 19 |
return "Field", "Unused field detected", "Remove it to improve performance"
|
| 20 |
|
| 21 |
-
#
|
| 22 |
def process_nlp_query(query):
|
| 23 |
if not query.strip():
|
| 24 |
return "No query provided."
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
prompt = f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# Generate response with good sampling and repetition control
|
| 30 |
result = nlp_model(
|
| 31 |
prompt,
|
| 32 |
-
max_length=
|
| 33 |
temperature=0.7,
|
| 34 |
-
top_k=
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
if "Answer:" in
|
| 44 |
-
|
| 45 |
|
| 46 |
# Remove duplicate lines
|
| 47 |
-
lines =
|
| 48 |
-
seen = set()
|
| 49 |
unique_lines = []
|
|
|
|
| 50 |
for line in lines:
|
| 51 |
if line.strip() not in seen:
|
| 52 |
seen.add(line.strip())
|
| 53 |
-
unique_lines.append(line)
|
| 54 |
-
cleaned_response = "\n".join(unique_lines)
|
| 55 |
|
| 56 |
-
return
|
| 57 |
|
| 58 |
-
# Gradio
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
gr.Markdown("# 🤖 AI Code Review & Metadata Validator")
|
| 61 |
|
|
@@ -76,8 +80,8 @@ with gr.Blocks() as demo:
|
|
| 76 |
metadata_button.click(validate_metadata, inputs=metadata_input, outputs=[mtype, issue, recommendation])
|
| 77 |
|
| 78 |
with gr.Tab("Ask AI (Natural Language)"):
|
| 79 |
-
query_input = gr.Textbox(label="Your question", lines=2, placeholder="e.g.
|
| 80 |
-
response_output = gr.Textbox(label="AI Response", lines=
|
| 81 |
nlp_button = gr.Button("Ask")
|
| 82 |
nlp_button.click(process_nlp_query, inputs=query_input, outputs=response_output)
|
| 83 |
|
|
|
|
| 18 |
return "No metadata provided.", "", ""
|
| 19 |
return "Field", "Unused field detected", "Remove it to improve performance"
|
| 20 |
|
| 21 |
+
# Ask AI (Natural Language)
|
| 22 |
def process_nlp_query(query):
|
| 23 |
if not query.strip():
|
| 24 |
return "No query provided."
|
| 25 |
|
| 26 |
+
# Enhanced prompt
|
| 27 |
+
prompt = f"""
|
| 28 |
+
You are a Salesforce and Apex expert. Provide a clear and technically accurate answer to the following question:
|
| 29 |
+
|
| 30 |
+
{query}
|
| 31 |
+
|
| 32 |
+
Answer:
|
| 33 |
+
"""
|
| 34 |
|
|
|
|
| 35 |
result = nlp_model(
|
| 36 |
prompt,
|
| 37 |
+
max_length=256,
|
| 38 |
temperature=0.7,
|
| 39 |
+
top_k=40,
|
| 40 |
+
top_p=0.9,
|
| 41 |
+
repetition_penalty=1.3,
|
| 42 |
+
do_sample=True
|
| 43 |
)
|
| 44 |
|
| 45 |
+
output = result[0]["generated_text"]
|
| 46 |
|
| 47 |
+
# Extract answer after "Answer:"
|
| 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 |
|
| 62 |
+
# Gradio UI
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
gr.Markdown("# 🤖 AI Code Review & Metadata Validator")
|
| 65 |
|
|
|
|
| 80 |
metadata_button.click(validate_metadata, inputs=metadata_input, outputs=[mtype, issue, recommendation])
|
| 81 |
|
| 82 |
with gr.Tab("Ask AI (Natural Language)"):
|
| 83 |
+
query_input = gr.Textbox(label="Your question", lines=2, placeholder="e.g. What is a governor limit in Apex?")
|
| 84 |
+
response_output = gr.Textbox(label="AI Response", lines=8)
|
| 85 |
nlp_button = gr.Button("Ask")
|
| 86 |
nlp_button.click(process_nlp_query, inputs=query_input, outputs=response_output)
|
| 87 |
|