Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,9 @@
|
|
| 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 |
-
# Set environment variables to fix permission issues
|
| 10 |
-
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 11 |
os.environ["RASA_USER_HOME"] = "/tmp/rasa"
|
| 12 |
os.environ["SQLALCHEMY_SILENCE_UBER_WARNING"] = "1"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
subprocess.Popen(["rasa", "run", "--enable-api", "--cors", "*", "--port", "5005"])
|
| 17 |
-
# Wait for server to start
|
| 18 |
-
time.sleep(10)
|
| 19 |
-
|
| 20 |
-
# Start the Rasa server in a separate thread
|
| 21 |
-
threading.Thread(target=start_rasa_server).start()
|
| 22 |
-
|
| 23 |
-
# Function to send messages to Rasa and get responses
|
| 24 |
-
def chat_with_rasa(message, history):
|
| 25 |
-
try:
|
| 26 |
-
# Send message to Rasa server
|
| 27 |
-
response = requests.post(
|
| 28 |
-
"http://localhost:5005/webhooks/rest/webhook",
|
| 29 |
-
json={"sender": "user", "message": message}
|
| 30 |
-
)
|
| 31 |
-
rasa_response = response.json()
|
| 32 |
-
|
| 33 |
-
# Extract text from Rasa response
|
| 34 |
-
if rasa_response:
|
| 35 |
-
return "\n".join([msg.get("text", "") for msg in rasa_response])
|
| 36 |
-
else:
|
| 37 |
-
return "No response from the bot."
|
| 38 |
-
except Exception as e:
|
| 39 |
-
return f"Error: {str(e)}"
|
| 40 |
-
|
| 41 |
-
# Create Gradio interface
|
| 42 |
-
chatbot = gr.Chatbot() # Removed title parameter which is not supported
|
| 43 |
-
interface = gr.Interface(
|
| 44 |
-
fn=chat_with_rasa,
|
| 45 |
-
inputs=gr.Textbox(placeholder="Type your message here..."),
|
| 46 |
-
outputs="text",
|
| 47 |
-
title="Rasa Chatbot",
|
| 48 |
-
description="Chat with your Rasa assistant",
|
| 49 |
-
flagging_dir="/tmp/flagged" # Use a writable directory for flagging
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# Launch the interface
|
| 53 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import subprocess
|
| 3 |
import time
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
| 5 |
os.environ["RASA_USER_HOME"] = "/tmp/rasa"
|
| 6 |
os.environ["SQLALCHEMY_SILENCE_UBER_WARNING"] = "1"
|
| 7 |
|
| 8 |
+
print("Starting Rasa server...")
|
| 9 |
+
subprocess.run(["rasa", "run", "--enable-api", "--cors", "*", "--port", "7860"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|