-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
157 lines (117 loc) · 4.38 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from flask import Flask, render_template, request, jsonify
import re
import requests
from flask_cors import CORS # Import CORS
app = Flask(__name__, static_folder='static') # Configure static folder
CORS(app) # Enable CORS for all routes
# Mira API endpoint and your API key
MIRA_API_URL = "https://api.mira.com/v1/chat" # Replace with the actual Mira API endpoint
MIRA_API_KEY = "sb-d1da1684b815b93eb68400e986c39022" # Replace with your actual key
def get_answer(message):
message = message.lower()
message = re.sub(r'[^\w\s]', '', message)
payload = {"text": message} # Mira expects "text" key
try:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {MIRA_API_KEY}" # Use Bearer token in header
}
response = requests.post(MIRA_API_URL, json=payload, headers=headers)
response.raise_for_status()
mira_data = response.json() # Parse JSON response
bot_response = mira_data.get("response", "I'm sorry, I couldn't understand that.")
except requests.exceptions.RequestException as e:
print(f"Error communicating with Mira API: {e}")
if response: # Print response details for debugging
print(f"Response text: {response.text}")
print(f"Response status code: {response.status_code}")
bot_response = "Sorry, I'm having trouble connecting to the server. Please try again later."
except (KeyError, TypeError) as e: # Handle JSON parsing errors
print(f"Error parsing Mira response: {e}")
bot_response = "Error processing the response from Mira."
if mira_data:
print(f"Mira Data: {mira_data}")
return bot_response
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
user_message = data['message']
bot_response = get_answer(user_message)
return jsonify({'response': bot_response})
@app.route('/')
def index():
return render_template('index.html') # Use render_template
if __name__ == '__main__':
app.run(debug=True)
from mira_sdk import MiraClient, Flow
# Initialize the client
client = MiraClient(config={"API_KEY": "YOUR_API_KEY"})
version = "1.0.0"
input_data = {}
# If no version is provided, latest version is used by default
if version:
flow_name = f"@aayushalways/compound/{version}"
else:
flow_name = "@aayushalways/compound"
result = client.flow.execute(flow_name, input_data)
print(result)
from mira_sdk import MiraClient, Flow
# Initialize the client
client = MiraClient(config={"API_KEY": "YOUR_API_KEY"})
version = "1.0.0"
input_data = {}
# If no version is provided, latest version is used by default
if version:
flow_name = f"@aayushalways/intent-recognition-flow/{version}"
else:
flow_name = "@aayushalways/intent-recognition-flow"
result = client.flow.execute(flow_name, input_data)
print(result)
from mira_sdk import MiraClient, Flow
# Initialize the client
client = MiraClient(config={"API_KEY": "YOUR_API_KEY"})
version = "1.0.0"
input_data = {}
# If no version is provided, latest version is used by default
if version:
flow_name = f"@aayushalways/entity/{version}"
else:
flow_name = "@aayushalways/entity"
result = client.flow.execute(flow_name, input_data)
print(result)
from mira_sdk import MiraClient, Flow
# Initialize the client
client = MiraClient(config={"API_KEY": "YOUR_API_KEY"})
version = "1.0.0"
input_data = {}
# If no version is provided, latest version is used by default
if version:
flow_name = f"@aayushalways/response/{version}"
else:
flow_name = "@aayushalways/response"
result = client.flow.execute(flow_name, input_data)
print(result)
from mira_sdk import MiraClient, Flow
# Initialize the client
client = MiraClient(config={"API_KEY": "YOUR_API_KEY"})
version = "1.0.0"
input_data = {}
# If no version is provided, latest version is used by default
if version:
flow_name = f"@aayushalways/data-r-f/{version}"
else:
flow_name = "@aayushalways/data-r-f"
result = client.flow.execute(flow_name, input_data)
print(result)
from mira_sdk import MiraClient, Flow
# Initialize the client
client = MiraClient(config={"API_KEY": "YOUR_API_KEY"})
version = "1.0.0"
input_data = {}
# If no version is provided, latest version is used by default
if version:
flow_name = f"@aayushalways/fallback/{version}"
else:
flow_name = "@aayushalways/fallback"
result = client.flow.execute(flow_name, input_data)
print(result)