Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,68 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
-
from huggingface_hub import InferenceClient, HfApi
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
raise ValueError("❌ HUGGINGFACEHUB_API_TOKEN is not set. Go to your Space → Settings → Secrets.")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
if not
|
| 19 |
-
return "
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
)
|
| 28 |
-
return response.strip()
|
| 29 |
except Exception as e:
|
| 30 |
-
|
| 31 |
-
return f"❌ Error: {str(e)}"
|
| 32 |
|
| 33 |
# Gradio UI
|
| 34 |
-
with gr.Blocks(
|
| 35 |
-
gr.Markdown("
|
| 36 |
-
gr.Markdown("Ask any technical question (e.g., Apex, SOQL, Metadata concepts)")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import openai
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
+
# Get OpenAI API key securely
|
| 7 |
+
openai_api_key = os.getenv("OPENAI_API_KEY") or "sk-your-correct-key-here"
|
| 8 |
+
client = openai.OpenAI(api_key=openai_api_key)
|
| 9 |
|
| 10 |
+
# Load local model for code classification
|
| 11 |
+
code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
|
|
|
|
| 12 |
|
| 13 |
+
# Code Review Function
|
| 14 |
+
def analyze_code(code):
|
| 15 |
+
if not code.strip():
|
| 16 |
+
return "No code provided.", "", ""
|
| 17 |
+
result = code_analyzer(code)
|
| 18 |
+
return result[0]["label"], "Consider refactoring for better performance", "Medium"
|
| 19 |
|
| 20 |
+
# Metadata Validator (Mock)
|
| 21 |
+
def validate_metadata(metadata):
|
| 22 |
+
if not metadata.strip():
|
| 23 |
+
return "No metadata provided.", "", ""
|
| 24 |
+
return "Field", "Unused field detected", "Remove it to improve performance"
|
| 25 |
|
| 26 |
+
# Natural Language Processor using OpenAI GPT-3.5
|
| 27 |
+
def process_nlp_query(query):
|
| 28 |
+
if not query.strip():
|
| 29 |
+
return "No query provided."
|
| 30 |
try:
|
| 31 |
+
response = client.chat.completions.create(
|
| 32 |
+
model="gpt-3.5-turbo",
|
| 33 |
+
messages=[
|
| 34 |
+
{"role": "system", "content": "You are a helpful assistant specialized in Salesforce Apex and metadata."},
|
| 35 |
+
{"role": "user", "content": query}
|
| 36 |
+
]
|
| 37 |
)
|
| 38 |
+
return response.choices[0].message.content.strip()
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"❌ OpenAI API error: {str(e)}"
|
|
|
|
| 41 |
|
| 42 |
# Gradio UI
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# 🤖 AI Code Review & Metadata Validator (GPT-Powered)")
|
|
|
|
| 45 |
|
| 46 |
+
with gr.Tab("Code Review"):
|
| 47 |
+
code_input = gr.Textbox(label="Apex / LWC Code", lines=8)
|
| 48 |
+
issue_type = gr.Textbox(label="Issue Type")
|
| 49 |
+
suggestion = gr.Textbox(label="AI Suggestion")
|
| 50 |
+
severity = gr.Textbox(label="Severity")
|
| 51 |
+
code_button = gr.Button("Analyze Code")
|
| 52 |
+
code_button.click(analyze_code, inputs=code_input, outputs=[issue_type, suggestion, severity])
|
| 53 |
|
| 54 |
+
with gr.Tab("Metadata Validation"):
|
| 55 |
+
metadata_input = gr.Textbox(label="Metadata XML", lines=8)
|
| 56 |
+
mtype = gr.Textbox(label="Type")
|
| 57 |
+
issue = gr.Textbox(label="Issue")
|
| 58 |
+
recommendation = gr.Textbox(label="Recommendation")
|
| 59 |
+
metadata_button = gr.Button("Validate Metadata")
|
| 60 |
+
metadata_button.click(validate_metadata, inputs=metadata_input, outputs=[mtype, issue, recommendation])
|
| 61 |
+
|
| 62 |
+
with gr.Tab("Ask AI (Natural Language)"):
|
| 63 |
+
query_input = gr.Textbox(label="Your question", lines=2, placeholder="e.g. What is a governor limit in Apex?")
|
| 64 |
+
response_output = gr.Textbox(label="AI Response")
|
| 65 |
+
nlp_button = gr.Button("Ask")
|
| 66 |
+
nlp_button.click(process_nlp_query, inputs=query_input, outputs=response_output)
|
| 67 |
|
| 68 |
demo.launch()
|