payparse / app.py
NabilHzs's picture
Upload app.py with huggingface_hub
c60dda0 verified
Raw History Blame Contribute Delete
1.49 kB
"""
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(),
)