Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import openai
|
| 4 |
-
import os
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
-
# Get OpenAI API key
|
| 11 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
-
if not openai_api_key:
|
| 13 |
-
raise ValueError("❌ OPENAI_API_KEY environment variable not set.")
|
| 14 |
-
|
| 15 |
-
# Initialize OpenAI client
|
| 16 |
-
client = openai.OpenAI(api_key=openai_api_key)
|
| 17 |
-
|
| 18 |
-
# Load local model for code classification
|
| 19 |
code_analyzer = pipeline("text-classification", model="microsoft/codebert-base")
|
|
|
|
| 20 |
|
| 21 |
# Code Review Function
|
| 22 |
def analyze_code(code):
|
|
@@ -25,31 +12,22 @@ def analyze_code(code):
|
|
| 25 |
result = code_analyzer(code)
|
| 26 |
return result[0]["label"], "Consider refactoring for better performance", "Medium"
|
| 27 |
|
| 28 |
-
# Metadata Validator (Mock)
|
| 29 |
def validate_metadata(metadata):
|
| 30 |
if not metadata.strip():
|
| 31 |
return "No metadata provided.", "", ""
|
| 32 |
return "Field", "Unused field detected", "Remove it to improve performance"
|
| 33 |
|
| 34 |
-
# Natural Language Processor
|
| 35 |
def process_nlp_query(query):
|
| 36 |
if not query.strip():
|
| 37 |
return "No query provided."
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
model="gpt-3.5-turbo",
|
| 41 |
-
messages=[
|
| 42 |
-
{"role": "system", "content": "You are a helpful assistant specialized in Salesforce Apex and metadata."},
|
| 43 |
-
{"role": "user", "content": query}
|
| 44 |
-
]
|
| 45 |
-
)
|
| 46 |
-
return response.choices[0].message.content.strip()
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return f"❌ OpenAI API error: {str(e)}"
|
| 49 |
|
| 50 |
# Gradio UI
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
-
gr.Markdown("# 🤖 AI Code Review & Metadata Validator
|
| 53 |
|
| 54 |
with gr.Tab("Code Review"):
|
| 55 |
code_input = gr.Textbox(label="Apex / LWC Code", lines=8)
|
|
|
|
| 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-base")
|
| 7 |
|
| 8 |
# Code Review Function
|
| 9 |
def analyze_code(code):
|
|
|
|
| 12 |
result = code_analyzer(code)
|
| 13 |
return result[0]["label"], "Consider refactoring for better performance", "Medium"
|
| 14 |
|
| 15 |
+
# Metadata Validator (Mock for now)
|
| 16 |
def validate_metadata(metadata):
|
| 17 |
if not metadata.strip():
|
| 18 |
return "No metadata provided.", "", ""
|
| 19 |
return "Field", "Unused field detected", "Remove it to improve performance"
|
| 20 |
|
| 21 |
+
# Natural Language Processor (AI-only, no default)
|
| 22 |
def process_nlp_query(query):
|
| 23 |
if not query.strip():
|
| 24 |
return "No query provided."
|
| 25 |
+
result = nlp_model(query, max_length=60, do_sample=False)
|
| 26 |
+
return result[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Gradio UI
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("# 🤖 AI Code Review & Metadata Validator")
|
| 31 |
|
| 32 |
with gr.Tab("Code Review"):
|
| 33 |
code_input = gr.Textbox(label="Apex / LWC Code", lines=8)
|