Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,22 +1,19 @@
|
|
| 1 |
"""
|
| 2 |
app.py — Hugging Face Spaces entry point (Gradio SDK)
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
Simple web UI at / for browser testing.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import os
|
| 10 |
import sys
|
|
|
|
| 11 |
|
| 12 |
sys.path.insert(0, os.path.dirname(__file__))
|
| 13 |
|
| 14 |
-
|
| 15 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 16 |
-
from fastapi.responses import HTMLResponse, JSONResponse
|
| 17 |
|
| 18 |
from nlu_engine import create_engine
|
| 19 |
-
from schema import ChatRequest, ChatResponse
|
| 20 |
from session_store import SessionStore
|
| 21 |
from state_manager import StateManager
|
| 22 |
|
|
@@ -26,86 +23,31 @@ engine = create_engine(offline=offline)
|
|
| 26 |
store = SessionStore()
|
| 27 |
manager = StateManager(nlu_engine=engine, use_llm_paraphrase=False)
|
| 28 |
|
| 29 |
-
# --- FastAPI app ---
|
| 30 |
-
app = FastAPI(title="PayParse — Indonesian NLU Assistant")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
@app.get("/health")
|
| 41 |
-
def health():
|
| 42 |
-
return {
|
| 43 |
-
"status": "ok",
|
| 44 |
-
"engine": type(engine).__name__,
|
| 45 |
-
"offline_mode": offline,
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
@app.post("/api/chat", response_model=ChatResponse)
|
| 50 |
-
def chat(request: ChatRequest) -> ChatResponse:
|
| 51 |
-
state = store.get_or_create(request.session_id)
|
| 52 |
-
nlu_result = engine.extract(request.message, state=state)
|
| 53 |
-
result = manager.process(state, nlu_result, request.message, request.resolve_contacts_locally)
|
| 54 |
-
store.save(result.state)
|
| 55 |
-
if result.is_complete:
|
| 56 |
-
store.delete(request.session_id)
|
| 57 |
-
return ChatResponse(
|
| 58 |
-
session_id=request.session_id,
|
| 59 |
-
reply=result.reply,
|
| 60 |
-
intent=result.state.intent,
|
| 61 |
-
is_complete=result.is_complete,
|
| 62 |
-
missing_slots=result.state.missing_slots,
|
| 63 |
-
payload=result.payload,
|
| 64 |
-
needs_contact_resolution=result.needs_contact_resolution,
|
| 65 |
-
target_kontak=result.target_kontak,
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
@app.delete("/api/session/{session_id}")
|
| 70 |
-
def reset_session(session_id: str):
|
| 71 |
-
deleted = store.delete(session_id)
|
| 72 |
-
return {"status": "reset" if deleted else "not_found", "session_id": session_id}
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# --- Simple web UI ---
|
| 76 |
-
HTML = """<!DOCTYPE html>
|
| 77 |
-
<html><head><meta charset="utf-8"><title>PayParse</title>
|
| 78 |
-
<style>
|
| 79 |
-
body{font-family:system-ui;max-width:600px;margin:40px auto;padding:20px}
|
| 80 |
-
#chat{height:300px;overflow-y:auto;border:1px solid #ccc;padding:10px;margin:10px 0}
|
| 81 |
-
.msg{margin:5px 0;padding:8px 12px;border-radius:8px;max-width:80%}
|
| 82 |
-
.user{background:#007aff;color:#fff;margin-left:auto;text-align:right}
|
| 83 |
-
.bot{background:#e8e8e8}
|
| 84 |
-
input{width:70%;padding:10px}
|
| 85 |
-
button{padding:10px 20px;background:#007aff;color:#fff;border:none;cursor:pointer}
|
| 86 |
-
</style></head><body>
|
| 87 |
-
<h1>PayParse — Dira AI</h1>
|
| 88 |
-
<div id="chat"></div>
|
| 89 |
-
<input id="msg" placeholder="Ketik pesan..." onkeypress="if(event.key==='Enter')send()">
|
| 90 |
-
<button onclick="send()">Kirim</button>
|
| 91 |
-
<script>
|
| 92 |
-
const sid=Math.random().toString(36);
|
| 93 |
-
const chat=document.getElementById('chat');
|
| 94 |
-
async function send(){
|
| 95 |
-
const m=document.getElementById('msg');
|
| 96 |
-
const t=m.value.trim();if(!t)return;
|
| 97 |
-
m.value='';
|
| 98 |
-
chat.innerHTML+='<div class="msg user">'+t+'</div>';
|
| 99 |
-
chat.scrollTop=chat.scrollHeight;
|
| 100 |
-
const r=await fetch('/api/chat',{method:'POST',headers:{'Content-Type':'application/json'},
|
| 101 |
-
body:JSON.stringify({session_id:sid,message:t,resolve_contacts_locally:false})});
|
| 102 |
-
const d=await r.json();
|
| 103 |
-
chat.innerHTML+='<div class="msg bot">'+d.reply.replace(/\\n/g,'<br>')+'</div>';
|
| 104 |
-
chat.scrollTop=chat.scrollHeight;
|
| 105 |
-
}
|
| 106 |
-
</script></body></html>"""
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
@app.get("/", response_class=HTMLResponse)
|
| 110 |
-
def index():
|
| 111 |
-
return HTML
|
|
|
|
| 1 |
"""
|
| 2 |
app.py — Hugging Face Spaces entry point (Gradio SDK)
|
| 3 |
|
| 4 |
+
Defines `demo` (gr.ChatInterface) for HF Spaces.
|
| 5 |
+
Backend functions are called directly — no FastAPI needed.
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
| 9 |
import sys
|
| 10 |
+
import uuid
|
| 11 |
|
| 12 |
sys.path.insert(0, os.path.dirname(__file__))
|
| 13 |
|
| 14 |
+
import gradio as gr
|
|
|
|
|
|
|
| 15 |
|
| 16 |
from nlu_engine import create_engine
|
|
|
|
| 17 |
from session_store import SessionStore
|
| 18 |
from state_manager import StateManager
|
| 19 |
|
|
|
|
| 23 |
store = SessionStore()
|
| 24 |
manager = StateManager(nlu_engine=engine, use_llm_paraphrase=False)
|
| 25 |
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def chat_fn(message, history):
|
| 28 |
+
"""Gradio chat function that calls the PayParse backend."""
|
| 29 |
+
if not message.strip():
|
| 30 |
+
return ""
|
| 31 |
+
session_id = "gradio-web"
|
| 32 |
+
try:
|
| 33 |
+
state = store.get_or_create(session_id)
|
| 34 |
+
nlu_result = engine.extract(message, state=state)
|
| 35 |
+
result = manager.process(state, nlu_result, message, False)
|
| 36 |
+
store.save(result.state)
|
| 37 |
+
if result.is_complete:
|
| 38 |
+
store.delete(session_id)
|
| 39 |
+
return result.reply
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Error: {e}"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
demo = gr.ChatInterface(
|
| 45 |
+
fn=chat_fn,
|
| 46 |
+
title="PayParse — Dira AI Assistant",
|
| 47 |
+
description=(
|
| 48 |
+
"Asisten suara dompet digital Indonesia.\n\n"
|
| 49 |
+
"Coba: 'transfer 50 ribu ke Budi' | 'beli pulsa 25rb' | "
|
| 50 |
+
"'bayar listrik id 4512345678' | 'gojek ke stasiun' | 'gofood nasi goreng'"
|
| 51 |
+
),
|
| 52 |
+
theme=gr.themes.Soft(),
|
| 53 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|