HeyJavis's picture
Upload 5 files
d69d28f verified
Raw History Blame Contribute Delete
2.62 kB
import logging
import time
import gradio as gr
import spaces
from support_rag import ask_support
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("experiment7.gradio")
# Hugging Face ZeroGPU Spaces expect at least one GPU-decorated function.
# This no-op is not called by the RAG pipeline; inference is handled by Groq.
@spaces.GPU
def zerogpu_compatibility_check():
return "ZeroGPU Space is configured."
def answer_question(question):
question = (question or "").strip()
if not question:
return (
"Please enter a question.",
"-",
"[]",
"0",
"0 ms",
)
start = time.perf_counter()
try:
result = ask_support(question)
elapsed_ms = round((time.perf_counter() - start) * 1000, 2)
sources = result.get("sources", [])
return (
result.get("answer", "Sorry, I could not generate an answer."),
result.get("intent", "general_support"),
", ".join(sources) if sources else "No relevant FAQ source found",
str(result.get("retry_count", 0)),
f"{elapsed_ms} ms",
)
except Exception:
logger.exception("Customer-support request failed.")
return (
"The support service encountered an error. Please try again later. "
"Check the Space logs for technical details.",
"error",
"[]",
"0",
"Error",
)
with gr.Blocks(title="Customer Support RAG") as demo:
gr.Markdown(
"# Customer Support RAG Assistant\n"
"Ask questions about order tracking, returns, refunds, account access, "
"billing, or technical support. Answers are grounded in the FAQ."
)
question = gr.Textbox(
label="Customer question",
placeholder="How can I request a refund?",
lines=2,
max_lines=4,
)
submit = gr.Button("Ask", variant="primary")
answer = gr.Textbox(label="Answer", lines=4)
with gr.Row():
intent = gr.Textbox(label="Detected intent")
source = gr.Textbox(label="Retrieved source")
with gr.Row():
retries = gr.Textbox(label="Retry count")
response_time = gr.Textbox(label="Response time")
submit.click(
fn=answer_question,
inputs=question,
outputs=[answer, intent, source, retries, response_time],
)
question.submit(
fn=answer_question,
inputs=question,
outputs=[answer, intent, source, retries, response_time],
)
if __name__ == "__main__":
demo.launch()