File size: 2,624 Bytes
d69d28f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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()