nsomabalint commited on
Commit
7a07b69
·
verified ·
1 Parent(s): 1b6a05a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+ import time
5
+ import requests
6
+ import json
7
+ import threading
8
+
9
+ # Start Rasa server in the background
10
+ def start_rasa_server():
11
+ subprocess.Popen(["rasa", "run", "--enable-api", "--cors", "*", "--port", "5005"])
12
+ # Wait for server to start
13
+ time.sleep(10)
14
+
15
+ # Start the Rasa server in a separate thread
16
+ threading.Thread(target=start_rasa_server).start()
17
+
18
+ # Function to send messages to Rasa and get responses
19
+ def chat_with_rasa(message, history):
20
+ try:
21
+ # Send message to Rasa server
22
+ response = requests.post(
23
+ "http://localhost:5005/webhooks/rest/webhook",
24
+ json={"sender": "user", "message": message}
25
+ )
26
+ rasa_response = response.json()
27
+
28
+ # Extract text from Rasa response
29
+ if rasa_response:
30
+ return "\n".join([msg.get("text", "") for msg in rasa_response])
31
+ else:
32
+ return "No response from the bot."
33
+ except Exception as e:
34
+ return f"Error: {str(e)}"
35
+
36
+ # Create Gradio interface
37
+ chatbot = gr.Chatbot(title="Rasa Assistant")
38
+ interface = gr.Interface(
39
+ fn=chat_with_rasa,
40
+ inputs=gr.Textbox(placeholder="Type your message here..."),
41
+ outputs="text",
42
+ title="Rasa Chatbot",
43
+ description="Chat with your Rasa assistant"
44
+ )
45
+
46
+ # Launch the interface
47
+ interface.launch()