NabilHzs commited on
Commit
99d3e83
·
verified ·
1 Parent(s): 40dc223

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +11 -30
app.py CHANGED
@@ -5,7 +5,8 @@ Combines:
5
  - FastAPI REST API at /api/* (for the Android app)
6
  - Gradio ChatInterface at / (for web testing)
7
 
8
- Both run on port 7860 (HF Spaces default).
 
9
  """
10
 
11
  import os
@@ -15,14 +16,11 @@ import sys
15
  sys.path.insert(0, os.path.dirname(__file__))
16
 
17
  import gradio as gr
18
- import uvicorn
19
  from fastapi import FastAPI
20
- from fastapi.middleware.cors import CORSMiddleware
21
- from fastapi.responses import JSONResponse
22
 
23
  from main import app as fastapi_app
24
  from nlu_engine import create_engine
25
- from schema import ChatRequest
26
  from session_store import SessionStore
27
  from state_manager import StateManager
28
 
@@ -35,15 +33,13 @@ fastapi_app.state.store = SessionStore()
35
  fastapi_app.state.manager = StateManager(nlu_engine=engine, use_llm_paraphrase=False)
36
 
37
  # --- Gradio Chat Interface (for web testing) ---
38
- def chat_with_dira(message: str, history: list) -> str:
39
- """Simple Gradio chat function that calls the backend."""
40
- import requests
41
- # Call our own FastAPI endpoint internally
42
- session_id = "gradio-web"
43
  try:
44
- # Use direct function call instead of HTTP
45
  store = fastapi_app.state.store
46
- state = store.get_or_create(session_id)
47
 
48
  nlu_result = fastapi_app.state.engine.extract(message, state=state)
49
  result = fastapi_app.state.manager.process(
@@ -52,22 +48,13 @@ def chat_with_dira(message: str, history: list) -> str:
52
  store.save(result.state)
53
 
54
  if result.is_complete:
55
- store.delete(session_id)
56
 
57
  return result.reply
58
  except Exception as e:
59
  return f"Error: {e}"
60
 
61
 
62
- def chat_interface(message, history):
63
- """Gradio ChatInterface wrapper."""
64
- if not message.strip():
65
- return ""
66
- reply = chat_with_dira(message, history)
67
- return reply
68
-
69
-
70
- # Create Gradio interface
71
  demo = gr.ChatInterface(
72
  fn=chat_interface,
73
  title="PayParse — Dira AI Assistant",
@@ -83,16 +70,10 @@ demo = gr.ChatInterface(
83
  textbox=gr.Textbox(placeholder="Ketik pesan...", scale=7),
84
  )
85
 
86
- # Mount Gradio onto FastAPI at root path
87
  app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
88
 
89
- # Also serve root as Gradio
90
  @app.get("/")
91
  def root():
92
- """Redirect root to Gradio interface."""
93
- from fastapi.responses import RedirectResponse
94
  return RedirectResponse(url="/gradio")
95
-
96
-
97
- if __name__ == "__main__":
98
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
5
  - FastAPI REST API at /api/* (for the Android app)
6
  - Gradio ChatInterface at / (for web testing)
7
 
8
+ HF Spaces (Gradio SDK) detects the `app` variable (FastAPI) and runs it
9
+ with uvicorn automatically. No need for `if __name__ == '__main__'`.
10
  """
11
 
12
  import os
 
16
  sys.path.insert(0, os.path.dirname(__file__))
17
 
18
  import gradio as gr
 
19
  from fastapi import FastAPI
20
+ from fastapi.responses import RedirectResponse
 
21
 
22
  from main import app as fastapi_app
23
  from nlu_engine import create_engine
 
24
  from session_store import SessionStore
25
  from state_manager import StateManager
26
 
 
33
  fastapi_app.state.manager = StateManager(nlu_engine=engine, use_llm_paraphrase=False)
34
 
35
  # --- Gradio Chat Interface (for web testing) ---
36
+ def chat_interface(message, history):
37
+ """Simple Gradio chat function that calls the backend directly."""
38
+ if not message.strip():
39
+ return ""
 
40
  try:
 
41
  store = fastapi_app.state.store
42
+ state = store.get_or_create("gradio-web")
43
 
44
  nlu_result = fastapi_app.state.engine.extract(message, state=state)
45
  result = fastapi_app.state.manager.process(
 
48
  store.save(result.state)
49
 
50
  if result.is_complete:
51
+ store.delete("gradio-web")
52
 
53
  return result.reply
54
  except Exception as e:
55
  return f"Error: {e}"
56
 
57
 
 
 
 
 
 
 
 
 
 
58
  demo = gr.ChatInterface(
59
  fn=chat_interface,
60
  title="PayParse — Dira AI Assistant",
 
70
  textbox=gr.Textbox(placeholder="Ketik pesan...", scale=7),
71
  )
72
 
73
+ # Mount Gradio onto FastAPI at /gradio
74
  app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
75
 
76
+ # Root redirects to Gradio
77
  @app.get("/")
78
  def root():
 
 
79
  return RedirectResponse(url="/gradio")