Luigi commited on
Commit
59e8d1c
·
1 Parent(s): 34f4608

initial commit

Browse files
Dockerfile ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use Python 3.8 slim
2
+ FROM python:3.8-slim
3
+
4
+ # 1) System deps
5
+ RUN apt-get update && \
6
+ apt-get install -y --no-install-recommends git build-essential && \
7
+ rm -rf /var/lib/apt/lists/*
8
+
9
+ WORKDIR /app
10
+
11
+ # 2) Install Rasa 2.8.3 from source (patch typing-extensions)
12
+ RUN git clone --depth 1 --branch 2.8.3 https://github.com/RasaHQ/rasa.git /rasa-src && \
13
+ cd /rasa-src && \
14
+ sed -i "s/typing-extensions<4.0.0 and >=3.7.4/typing-extensions<5.0.0 and >=3.7.4/" setup.cfg && \
15
+ pip install --no-cache-dir .
16
+
17
+ # 3) Install Rasa SDK so your custom actions work
18
+ RUN pip install --no-cache-dir rasa-sdk==2.8.3
19
+
20
+ # 4) Pin protobuf so TensorFlow protos load
21
+ RUN pip install --no-cache-dir "protobuf<3.21.0,>=3.20.0"
22
+
23
+ # 5) Pin TensorFlow to the 2.8.x line Rasa 2.8.3 expects
24
+ RUN pip install --no-cache-dir "tensorflow<2.9.0,>=2.8.0"
25
+
26
+ # 6) Pin NumPy to a 1.20–1.23 release for C-API compatibility & deprecated aliases
27
+ RUN pip install --no-cache-dir "numpy>=1.20.0,<1.24.0"
28
+
29
+ # 7) Copy in your Rasa project
30
+ COPY domain.yml config.yml endpoints.yml /app/
31
+ COPY data /app/data
32
+ COPY actions /app/actions
33
+
34
+ # 8) Patch out the obsolete decorator before training
35
+ RUN sed -i '/@training.enable_multi_worker/d' \
36
+ /usr/local/lib/python3.8/site-packages/rasa/utils/tensorflow/temp_keras_modules.py
37
+
38
+ # 9) Train your model (writes into /app/models)
39
+ RUN rasa train
40
+
41
+ # 10) Install Gradio v4 + HTTP client
42
+ COPY requirements.txt /app/requirements.txt
43
+ RUN pip install --no-cache-dir -r requirements.txt
44
+
45
+ # 11) Copy your Gradio wrapper
46
+ COPY app.py /app/app.py
47
+
48
+ # 12) Expose ports (actions, Rasa API, Gradio UI)
49
+ EXPOSE 5055 5005 7860
50
+
51
+ # 13) Launch everything: action server, Rasa REST API, then Gradio
52
+ ENTRYPOINT ["bash","-lc","rasa run actions & rasa run --enable-api --port 5005 & python app.py"]
README.md CHANGED
@@ -10,4 +10,31 @@ pinned: false
10
  license: mit
11
  ---
12
 
13
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  license: mit
11
  ---
12
 
13
+ # RasaBot + Gradio v4 Demo on HuggingFace Spaces (CPU)
14
+
15
+ This Space runs a tiny Rasa 2.8.3 assistant inside a **Gradio v4** chat UI on a **CPU** plan.
16
+
17
+ ## Files
18
+
19
+ - **Dockerfile**
20
+ Python 3.8 → installs Rasa 2.8.3 + Gradio v4 → `rasa init` → `rasa train` → launches `app.py`.
21
+
22
+ - **requirements.txt**
23
+ Pins Rasa and Gradio to v4-compatible versions.
24
+
25
+ - **app.py**
26
+ Loads the Rasa model and serves a chat UI via Gradio Blocks.
27
+
28
+ - **README.md** (this file)
29
+
30
+ ## Deploy on HuggingFace Spaces
31
+
32
+ 1. Create a new Space with **Hardware: CPU** and **Environment: Docker**.
33
+ 2. Push these four files to your repo.
34
+ 3. The Space will build, train, and expose the Gradio chat at `/`.
35
+
36
+ ## Local Testing
37
+
38
+ ```bash
39
+ docker build -t rasa-gradio-v4 .
40
+ docker run -it --rm -p 7860:7860 rasa-gradio-v4
actions/__pycache__/actions.cpython-38.pyc ADDED
Binary file (1.92 kB). View file
 
actions/actions.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This files contains your custom actions which can be used to run
2
+ # custom Python code.
3
+ #
4
+ # See this guide on how to implement these action:
5
+ # https://rasa.com/docs/rasa/custom-actions
6
+
7
+
8
+ # This is a simple example for a custom action which utters "Hello World!"
9
+
10
+ from typing import Any, Text, Dict, List
11
+
12
+ from rasa_sdk import Action, Tracker
13
+ from rasa_sdk.executor import CollectingDispatcher
14
+
15
+
16
+ class action_ReversePhoneLookup(Action):
17
+
18
+ def name(self) -> Text:
19
+ return "action_ReversePhoneLookup"
20
+
21
+ def run(self, dispatcher: CollectingDispatcher,
22
+ tracker: Tracker,
23
+ domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
24
+
25
+ dispatcher.utter_message(text="反查來電處理")
26
+
27
+ return []
28
+
29
+ class action_CallReservationHotline(Action):
30
+
31
+ def name(self) -> Text:
32
+ return "action_CallReservationHotline"
33
+
34
+ def run(self, dispatcher: CollectingDispatcher,
35
+ tracker: Tracker,
36
+ domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
37
+
38
+ dispatcher.utter_message(text="預約或修改掛號處理")
39
+
40
+ return []
41
+
42
+ class action_human_handoff(Action):
43
+
44
+ def name(self) -> Text:
45
+ return "action_human_handoff"
46
+
47
+ def run(self, dispatcher: CollectingDispatcher,
48
+ tracker: Tracker,
49
+ domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
50
+
51
+ dispatcher.utter_message(text="其他處理")
52
+
53
+ return []
app.py CHANGED
@@ -1,64 +1,22 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
+ import requests
3
 
4
+ RASA_URL = "http://localhost:5005/webhooks/rest/webhook"
 
 
 
5
 
6
+ def bot_response(user_message, chat_history):
7
+ resp = requests.post(RASA_URL, json={
8
+ "sender": "user",
9
+ "message": user_message
10
+ }).json()
11
+ bot_reply = resp[0].get("text") if resp else "🤖 (no reply)"
12
+ chat_history.append((user_message, bot_reply))
13
+ return chat_history
14
 
15
+ with gr.Blocks(title="Rasa 2.8.3 + Gradio v4 Demo") as demo:
16
+ chatbot = gr.Chatbot(label="Rasa Bot", height=400)
17
+ user_input = gr.Textbox(placeholder="Type here...", show_label=False)
18
+ user_input.submit(bot_response, inputs=[user_input, chatbot], outputs=chatbot)
19
+ gr.Button("Clear").click(lambda: [], None, chatbot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  if __name__ == "__main__":
22
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
config.yml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Configuration for Rasa NLU.
2
+ # https://rasa.com/docs/rasa/nlu/components/
3
+ language: zh
4
+
5
+ pipeline:
6
+ # # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
7
+ # # If you'd like to customize it, uncomment and adjust the pipeline.
8
+ # # See https://rasa.com/docs/rasa/tuning-your-model for more information.
9
+ - name: "KeywordIntentClassifier"
10
+
11
+ # Configuration for Rasa Core.
12
+ # https://rasa.com/docs/rasa/core/policies/
13
+ policies:
14
+ - name: RulePolicy
data/nlu.yml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: "2.0"
2
+
3
+ nlu:
4
+ - intent: ReversePhoneLookup
5
+ examples: |
6
+ - 我剛才有未接來電,想知道是誰打來的
7
+ - 我看到有未接來電,請問找我有什麼事
8
+
9
+ - intent: CallReservationHotline
10
+ examples: |
11
+ - 我想要預約心臟科的門診
12
+ - 我想預約健康檢查
13
+
14
+ - intent: out_of_scope
15
+ examples: |
16
+ - 請問醫院的營業時間是什麼時候
17
+ - 我想查詢我的檢查報告結果
data/rules.yml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: "2.0"
2
+
3
+ rules:
4
+
5
+ - rule: reverse phone lookup
6
+ steps:
7
+ - intent: ReversePhoneLookup
8
+ - action: action_ReversePhoneLookup
9
+
10
+ - rule: book or modify an appointment
11
+ steps:
12
+ - intent: CallReservationHotline
13
+ - action: action_CallReservationHotline
14
+
15
+ - rule: out of scope handling
16
+ steps:
17
+ - intent: out_of_scope
18
+ - action: action_human_handoff
domain.yml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: "2.0"
2
+
3
+ intents:
4
+ - ReversePhoneLookup
5
+ - CallReservationHotline
6
+ - out_of_scope
7
+
8
+ actions:
9
+ - action_ReversePhoneLookup
10
+ - action_CallReservationHotline
11
+ - action_human_handoff
12
+
13
+
14
+ session_config:
15
+ session_expiration_time: 60
16
+ carry_over_slots_to_new_session: true
endpoints.yml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file contains the different endpoints your bot can use.
2
+
3
+ # Server where the models are pulled from.
4
+ # https://rasa.com/docs/rasa/model-storage#fetching-models-from-a-server
5
+
6
+ #models:
7
+ # url: http://my-server.com/models/default_core@latest
8
+ # wait_time_between_pulls: 10 # [optional](default: 100)
9
+
10
+ # Server which runs your custom actions.
11
+ # https://rasa.com/docs/rasa/custom-actions
12
+
13
+ action_endpoint:
14
+ url: "http://localhost:5055/webhook"
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- huggingface_hub==0.25.2
 
 
1
+ gradio==4.44.1
2
+ requests