File size: 1,492 Bytes
20e9e63
 
 
c60dda0
 
20e9e63
 
 
 
c60dda0
20e9e63
 
 
c60dda0
20e9e63
 
 
 
 
547f350
20e9e63
 
547f350
 
 
 
c60dda0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20e9e63
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
"""
app.py — Hugging Face Spaces entry point (Gradio SDK)

Defines `demo` (gr.ChatInterface) for HF Spaces.
Backend functions are called directly — no FastAPI needed.
"""

import os
import sys
import uuid

sys.path.insert(0, os.path.dirname(__file__))

import gradio as gr

from nlu_engine import create_engine
from session_store import SessionStore
from state_manager import StateManager

# --- Initialize engine ---
offline = os.getenv("PAYPARSE_OFFLINE", "0") == "1"
engine = create_engine(offline=offline)
store = SessionStore()
manager = StateManager(nlu_engine=engine, use_llm_paraphrase=False)


def chat_fn(message, history):
    """Gradio chat function that calls the PayParse backend."""
    if not message.strip():
        return ""
    session_id = "gradio-web"
    try:
        state = store.get_or_create(session_id)
        nlu_result = engine.extract(message, state=state)
        result = manager.process(state, nlu_result, message, False)
        store.save(result.state)
        if result.is_complete:
            store.delete(session_id)
        return result.reply
    except Exception as e:
        return f"Error: {e}"


demo = gr.ChatInterface(
    fn=chat_fn,
    title="PayParse — Dira AI Assistant",
    description=(
        "Asisten suara dompet digital Indonesia.\n\n"
        "Coba: 'transfer 50 ribu ke Budi' | 'beli pulsa 25rb' | "
        "'bayar listrik id 4512345678' | 'gojek ke stasiun' | 'gofood nasi goreng'"
    ),
    theme=gr.themes.Soft(),
)