Bhanumani12 commited on
Commit
0a6b8fa
·
verified ·
1 Parent(s): 98f3b61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -28
app.py CHANGED
@@ -1,44 +1,68 @@
1
  import gradio as gr
 
 
2
  import os
3
- from huggingface_hub import InferenceClient, HfApi
4
 
5
- # Load token from Hugging Face Secrets (Hugging Face Spaces handles this)
6
- HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
 
7
 
8
- # Check if token is valid
9
- if HF_TOKEN is None:
10
- raise ValueError("❌ HUGGINGFACEHUB_API_TOKEN is not set. Go to your Space → Settings → Secrets.")
11
 
12
- # Use FLAN-T5 for general QA
13
- MODEL_NAME = "google/flan-t5-base"
14
- client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN)
 
 
 
15
 
16
- # Define Q&A function
17
- def ask_ai(question):
18
- if not question.strip():
19
- return "⚠️ Please enter a valid question."
 
20
 
 
 
 
 
21
  try:
22
- prompt = f"Answer the following question clearly:\n{question}"
23
- response = client.text_generation(
24
- prompt=prompt,
25
- max_new_tokens=100,
26
- temperature=0.7,
 
27
  )
28
- return response.strip()
29
  except Exception as e:
30
- # Return the full error message for debugging
31
- return f"❌ Error: {str(e)}"
32
 
33
  # Gradio UI
34
- with gr.Blocks(title="AI Code Review & Metadata Validator") as demo:
35
- gr.Markdown("## 🤖 AI Code Review & Metadata Validator")
36
- gr.Markdown("Ask any technical question (e.g., Apex, SOQL, Metadata concepts)")
37
 
38
- question = gr.Textbox(label="Your question", placeholder="What is a governor limit in Apex?")
39
- answer = gr.Textbox(label="AI Response")
 
 
 
 
 
40
 
41
- ask_btn = gr.Button("Ask")
42
- ask_btn.click(fn=ask_ai, inputs=question, outputs=answer)
 
 
 
 
 
 
 
 
 
 
 
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()