Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
# Load Hugging Face
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 7 |
-
MODEL_NAME = "google/flan-t5-base"
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN)
|
| 11 |
|
| 12 |
-
# Define function
|
| 13 |
def ask_ai(question):
|
| 14 |
if not question.strip():
|
| 15 |
-
return "
|
| 16 |
-
|
| 17 |
try:
|
| 18 |
-
prompt = f"Answer
|
| 19 |
response = client.text_generation(
|
| 20 |
prompt=prompt,
|
| 21 |
max_new_tokens=100,
|
|
@@ -23,20 +27,18 @@ def ask_ai(question):
|
|
| 23 |
)
|
| 24 |
return response.strip()
|
| 25 |
except Exception as e:
|
|
|
|
| 26 |
return f"β Error: {str(e)}"
|
| 27 |
|
| 28 |
-
# Gradio
|
| 29 |
-
with gr.Blocks(title="AI Code Review & Metadata Validator
|
| 30 |
gr.Markdown("## π€ AI Code Review & Metadata Validator")
|
| 31 |
gr.Markdown("Ask any technical question (e.g., Apex, SOQL, Metadata concepts)")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
question = gr.Textbox(label="Your question", placeholder="e.g. What is a governor limit in Apex?")
|
| 35 |
answer = gr.Textbox(label="AI Response")
|
| 36 |
|
| 37 |
ask_btn = gr.Button("Ask")
|
| 38 |
-
|
| 39 |
ask_btn.click(fn=ask_ai, inputs=question, outputs=answer)
|
| 40 |
|
| 41 |
-
# Launch the app
|
| 42 |
demo.launch()
|
|
|
|
| 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,
|
|
|
|
| 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()
|